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
use can use ifelse
. It should work on both matrix as well as dataframe however, resultant value will be matrix
> df <- cbind(aaple = c(3, 0 , 1), banana = c(0, 1, 2), orange = c(2, 1, 2))
> df
aaple banana orange
[1,] 3 0 2
[2,] 0 1 1
[3,] 1 2 2
> ifelse(df>0, 1, 0)
aaple banana orange
[1,] 1 0 1
[2,] 0 1 1
[3,] 1 1 1