Reputation: 924
I'm attempting to remove all colSums !=0
columns from my matrix. Using this post I have gotten close.
data2<-subset(data, Gear == 20) #subset off larger data matrix
i <- (colSums(data2[,3:277], na.rm=T) != 0) #column selection in this line limits to numeric columns only
data3<- data2[, i] #select only columns with non-zero colSums
This creates a vector, i, that properly identifies the columns to be removed using a logical True/False. However, the final line is removing columns by some logic other than my intentional if true then include if false then exclude.
The goal: remove all columns in that range that have a colSums == 0
.
The problem: my current code does not seem to properly identify said columns
Suggestion as to what I'm missing?
Update: Adding dummy data to use:
a<-matrix(1:10, ncol = 10,nrow=10)
a
a[,c(3,5,8)]<-0
a
i <- (colSums(a, na.rm=T) != 0)
b<- a[, i]
it works well here, so I'm not sure why it won't work above on real data.
Upvotes: 1
Views: 83
Reputation: 887851
We can get the column names from the colSums
and concatenate with the first two column names, which was not used for creating the condition with colSums
to select the columns of interest
data[c(names(data)[1:2], names(which(i1)))]
Upvotes: 1