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

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Feb 24 23:45:13 CET 2009


Stavros Macrakis wrote:
> 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)
>>     

actually, you need one NULL per variable, but it suffices to provide a
list of *one* NULL, and it will be recycled:
   
    df[,c("var.a","var.b")] <- list(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))
>>     

as above, this works as well:

    df[, vars] = list(NULL)

and this, simplest of them all, works too:

    df[vars] = list(NULL)

vQ



More information about the R-help mailing list