[R] how to NULL multiple variables of a df efficiently?

Stavros Macrakis macrakis at alum.mit.edu
Tue Feb 24 22:47:15 CET 2009


On Tue, Feb 24, 2009 at  3:10 PM, Sean Zhang wrote:
> ...Want to delete many variables in a dataframe....
> df<-data.frame(var.a=rnorm(10), var.b=rnorm(10),var.c=rnorm(10))
> df[,'var.a']<-NULL   #this works for one single variable
> df[,c('var.a','var.b')]<-NULL  #does not work for multiple variables

Actually, setting to NULL works fine for multiple variables, but you
need one NULL per variable:

> df[,c("var.a","var.b")] <- list(NULL,NULL)
> df
        var.c
1   1.2470314
2  -0.7075917
3  -1.3959612

If the variable list is in a variable:

> vars <- c("var.a","var.c")

Careful, rep requires a *list* of NULL, not an element:

> df[,vars] <- rep(list(NULL),length(vars))

Hope this helps,

             -s




More information about the R-help mailing list