I have a dataframe called reshapedwcw as follows. I want to use the apply function to convert the data to binary data where the value is present reshapewcw
D009325 D009357 D009369 D009373 D009404 D009437 D009442 D009447
r1 1 0 0 2 0 0 44 78
r2 0 3 4 0 2 1 2 2
r3 1 2 1 2 3 87 99 2
Desired Output
D009325 D009357 D009369 D009373 D009404 D009437 D009442 D009447
r1 1 0 0 1 0 0 1 1
r2 0 1 1 0 1 1 1 1
r3 1 1 1 1 1 1 1 1
In addition, kindly let me what is wrong with this approach and is there a better alternative
indices <- which(apply(reshapedwcw,2,function(x) x>1))
reshapedwcw[indices]<-1
vector, but also valid formatrixanddata frame. There is no need to useapplyfunction.temp_df <- reshapedwcw[, 3:8],temp_df[temp_df > 0] <- 1,reshapedwcw[, 3:8] <- temp_df