[R] subsetting a dataframe
dgrove at fhcrc.org
Mon Apr 7 20:47:53 CEST 2003
> How does one remove a column from a data frame when the name of
> the column to remove is stored in a variable?
> For Example:
> colname <- "LOT"
> newdf <- subset(olddf,select = - colname)
This is how I do it. There are variations on this
and I'm sure totally different ways as well.
newdf <- newdf[, names(newdf) != colname]
If 'colnames' is a vector (i.e. if you have multiple columns
to delete) then do:
newdf <- newdf[, !(names(newdf) %in% colname)]
This command will work even if 'colname' is not a vector
> The above statement will give an error, but thats what I'm trying to
> If I had used:
> newdf <- subset(olddf,select = - LOT)
> then it would have worked, but as I said the column name is stored in a
> so I can't just enter it explicity.
> R-help at stat.math.ethz.ch mailing list
More information about the R-help