Another way would be a which statement.
good_dataset=data[,which(colSums(data)!=0)]
I believe this depends on how the data are structured though.
--
View this message in context:
http://r.789695.n4.nabble.com/drop-columns-whose-rows-are-all-0-tp4324231p4325474.html
Sent from the R help maili
On 25/01/12 05:14, Francisco 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_co
dataset.1 <- dataset[, apply(dataset, 2, sum)>0]
Gregorio R. Serrano
2012/1/24 Francisco
> 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.
Try also
dataset[, colSums(dataset == 0) != nrow(dataset)]
HTH,
Jorge.-
On Tue, Jan 24, 2012 at 11:14 AM, Francisco <> 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
> 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, Ja
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) {
6 matches
Mail list logo