Home > Mobile >  drop columns if specific row contains given value
drop columns if specific row contains given value

Time:12-09

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 ?

CodePudding user response:

This is a fairly simple solution.

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

CodePudding user response:

subset(df, select=which(df[4,]!=0))
  •  Tags:  
  • r
  • Related