This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Weird Error: dim(X) must have positive value While is.data.frame(x) is TRUE
>  is.data.frame(STEM_profiles$p11)
> [1] TRUE

#filter out rows whose rowsums ==0
> STEM_profiles$p11 <- STEM_profiles$p11[!apply(STEM_profiles$p11,1,sum)==0,]
# AND it really did well 

> head(STEM_profiles$p11,1)
      H.A5.12  H.A7.12  H.A8.12  H.A9.12 H.E1.12  H.E2.12  
DOCK5 1258.93 2002.866 1180.872 1536.784 831.847

HOWEVER, when it put into a for loop, R throw a ERROR: Error in apply(STEM_profiles$i, 1, sum) : dim(X) must have a positive length?

> a <- for (i in names(STEM_profiles)){
+     STEM_profiles$i <- STEM_profiles$i[!apply(STEM_profiles$i,1,sum)==0,]}

>Error in apply(STEM_profiles$i, 1, sum) : dim(X) must have a positive

How can I prevent this error? And why did this happen? Any suggestions welcomed! Thanks advance!

r

1 answer

a = lapply(STEM_profiles, function(x) {
    v = rowSums(x)
    x[v != 0,,drop=F]
    })

It works very well!And I have also find where may account for this error: it's not advisable to use "$" to quote variate. Thanks for you concise and inspiring command !

Log in to answer this question.