I have a problem with my loop. It just delete some rows that have 0 or NA values in my desire column and I don’t know why:
for (i in 1:105) {
for (j in 1:l[i+1]){
if(m[[i]][j,12]==0 | is.na(m[[i]][j,12])) {
m[[i]]=m[[i]][-j,]
}
}
}
Searching on the web I saw that maybe I could use apply function… something like:
for( i in 1:105){m[[i]]<-m[[i]][!apply(is.na(m[[i]]), 1, any),]}
for( i in 1:105){
as.null(0)
m[[i]]<-m[[i]][!apply(is.null(m[[i]]), 1, any),]
}
This throws me a dim(x) error… I want to set Zero number as NULL
I was thinking something as follows but clearly it isn’t good… it just the idea…. I really don’t know how to use apply function well
for( i in 1:105){as.null(0) m[[i]]<-!apply(m[[i]],1,is.null(m[[i]])) }
Thanks a lot for your useful help !
You use
applyto apply a function over a margin of an array, but I think is not the best idea here, since you only need to subset the matrix properly. Let’s focus in just one matrixm.indwill haveTRUEwhere appropiate and the you can doto remove the rows. You can put this inside the loop, or use
lapply(to apply a function over a list), but first you need a function to be applied to every element in the list (all your 105 matrix), soThat should work.