sum two columns in R

前端 未结 6 1671
情书的邮戳
情书的邮戳 2020-12-03 14:00

I feel a bit embarrassed as I am trying to add two columns in R to get the product.

I have tried

sum(col1,col2)

but this returns

6条回答
  •  醉话见心
    2020-12-03 14:38

    You can do this :

        df <- data.frame("a" = c(1,2,3,4), "b" = c(4,3,2,1), "x_ind" = c(1,0,1,1), "y_ind" = c(0,0,1,1), "z_ind" = c(0,1,1,1) )
    df %>% mutate( bi  = ifelse((df$x_ind + df$y_ind +df$z_ind)== 3, 1,0 ))
    

提交回复
热议问题