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) The above statement will give an error, but thats what I'm trying to accomplish. 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 variable so I can't just enter it explicity. Thanks.
> 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 Doug> The above statement will give an error, but thats what I'm trying to > accomplish. > > 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 > variable > so I can't just enter it explicity. > > Thanks. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
Dear Chris, The following should work for you: olddf[[colname]] <- NULL I hope this helps, John At 11:28 AM 4/7/2003 -0700, Chris Handorf wrote:>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) > >The above statement will give an error, but thats what I'm trying to >accomplish. > >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 >variable >so I can't just enter it explicity.____________________________ John Fox Department of Sociology McMaster University email: jfox at mcmaster.ca web: http://www.socsci.mcmaster.ca/jfox
Chris Handorf wrote:> > 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) > > The above statement will give an error, but thats what I'm trying to > accomplish. > > 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 > variable > so I can't just enter it explicity.What about subset(olddf, select= -get(colname)) or olddf[, names(olddf) != colname] Uwe Ligges
> df <- data.frame(a=1, b=2, c=3)> df1 <- data.frame(a=1, b=2, c=3) > df1[,!is.element(names(df1), "b")] a c 1 1 3 Spencer Graves Chris Handorf wrote:> 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) > > The above statement will give an error, but thats what I'm trying to > accomplish. > > 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 > variable > so I can't just enter it explicity. > > Thanks. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help