> dataset<-data.frame(a=1:10,b=c(0,0,0,1,0,0,0,0,1,0),c=rep(0,10)) > apply(dataset,2,function(x) all(x==0)) a b c FALSE FALSE TRUE
> dataset[,!apply(dataset,2,function(x) all(x==0))] a b 1 1 0 2 2 0 3 3 0 4 4 1 5 5 0 6 6 0 7 7 0 8 8 0 9 9 1 10 10 0 On Tue, Jan 24, 2012 at 8:14 AM, Francisco <franciscororol...@google.com>wrote: > 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 > > ______________________________**________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.