[R] drop columns whose rows are all 0

Francisco franciscororolaio at google.com
Tue Jan 24 17:14:15 CET 2012


Hello,
I have a dataset with 40 variables, some of them are always 0 (each 
row). I would like to make a subset containing only the columns which 
values are not all 0, but I don't know how to do it.

I tried:

for(cut_column in 1:40) {

if(sum(dataset[,cut_column])!=0) {
				columns_useful<-c(columns_useful,dataset[cut_column])

}
}

sorted_dataset<-subset(dataset, select=columns_useful)

But it doesn't work.
Thank you

Francisco



More information about the R-help mailing list