MCS
MCS

Reputation: 1101

Why does dplyr fails to produce by line results?

both sum and + fail to give a result for each single line

df <- data.frame(x1=c(1,1),x2=c(2,3),y1=c(NA,1),y2=c(NA,1))
df <- mutate(df, cost = prod(x1,x2,na.rm = T)+prod(y1,y2,na.rm = T),na.rm=T)

result:

  x1 x2 y1 y2 cost
1  1  2 NA NA    7
2  1  3  1  1    7

expected:

  x1 x2 y1 y2 cost
1  1  2 NA NA    3
2  1  3  1  1    4

Upvotes: 2

Views: 19

Answers (1)

akrun
akrun

Reputation: 887391

We can add rowwise otherwise prod does the multiplication on the whole column whereas we need the product of sum of each row

library(dplyr)
df %>%
    rowwise %>%
    mutate(cost = prod(x1,x2,na.rm = TRUE) + prod(y1,y2,na.rm = TRUE)) %>%
    ungroup
# A tibble: 2 x 5
#     x1    x2    y1    y2  cost
#  <dbl> <dbl> <dbl> <dbl> <dbl>
#1     1     2    NA    NA     3
#2     1     3     1     1     4

Upvotes: 2

Related Questions