I have a data frame which contains some values. And now I would like to keep only the max value in each row and give 0 to the rest column, like this:
df <- data_frame(a= c(1,2,3,4,5),b= c(2,5,3,9,7),c= c(40,6,2,1,7))
df$rowmax <- apply(df,1,max)
#
a b c rowmax
<dbl> <dbl> <dbl> <dbl>
1 2 40 40
2 5 6 6
3 3 2 3
4 9 1 9
5 7 7 7
#ideal out put
a b c rowmax
0 0 40 40
0 0 6 6
3 3 0 3
0 9 0 9
0 7 7 7
Could any one help me out here? thanks ; )