wanderzen
wanderzen

Reputation: 119

drop columns if specific row contains given value

I would like to drop columns if their fourth row contains "0"

dummy dataframe:

df <- data.frame(a=c(1,2,3,4,5,6), b=(c(0,1,2,0,3,4)), c=c(1,2,0,4,5,6), d=c(1,7,9,4,5,6))

I've already used this to remove columns when any rows was containing a zero value :

b <- df[,-grep("0", df)]

but I can't find a way to do it with a specific row...

Any ideas ?

Upvotes: 0

Views: 98

Answers (2)

Rfanatic
Rfanatic

Reputation: 2280

subset(df, select=which(df[4,]!=0))

Upvotes: 1

Quixotic22
Quixotic22

Reputation: 2924

This is a fairly simple solution.

df[,which(df[4,]!=0)]

Upvotes: 1

Related Questions