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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to