Weird Error: dim(X) must have positive value While is.data.frame(x) is TRUE
1
0
Entering edit mode
5.5 years ago
>  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 • 3.2k views
ADD COMMENT
2
Entering edit mode
5.5 years ago
a = lapply(STEM_profiles, function(x) {
    v = rowSums(x)
    x[v != 0,,drop=F]
    })
ADD COMMENT
0
Entering edit mode

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 !

ADD REPLY

Login before adding your answer.

Traffic: 3734 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6