R: Convert Matrix to YES, NO dataframe -
i have matrix this
df <- matrix(c(rep(1,3),rep(2,3)),nrow=3,ncol=2) df: [,1] [,2] [1,] 1 2 [2,] 1 2 [3,] 1 2
i want convert every cell value yes
, if greater 0, else no
i understand can using
apply(df, 2, function(x) ifelse(x > 0, "yes","no"))
however matrix huge (million * 5000), , hence using apply takes insanely large time
i have tried
df <- ifelse(df > 0, "yes","no")
however takes lot of time
can achieve better performance this?
here's 1 way create matrix:
df[] <- c("no", "yes")[(df > 0) + 1]
the result:
[,1] [,2] [1,] "yes" "yes" [2,] "yes" "yes" [3,] "yes" "yes"
Comments
Post a Comment