Convert numeric values into binary (0/1)

后端 未结 5 1763
余生分开走
余生分开走 2021-02-06 08:15

I have a data frame with counts of different kinds of fruits of different people. Like below

    apple  banana  orange
Tim     3       0       2
Tom     0                


        
5条回答
  •  佛祖请我去吃肉
    2021-02-06 09:10

    > pippo
      person apple banana orange
    1    Tim     1      0      2
    2    Tom     0      1      1
    3    Bob     1      2      2
    > cols <- c("apple", "banana", "orange")
    > lapply(cols, function(x) {pippo[,x] <<- as.numeric(pippo[,x] >= 1)})
    

提交回复
热议问题