Dear R-helpers:
I am an R novice and would appreciate answer to the following question.
Want to delete many variables in a dataframe.
Am able to delete one variable by assigning it as NULL
Have a large number of variables and would like to delete them without using
a for loop.
Is there a command/function which does this job?
Many thanks in advance.
-Sean
#Small Example:
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
[[alternative HTML version deleted]]
markleeds at verizon.net
2009-Feb-24 20:18 UTC
[R] how to NULL multiple variables of a df efficiently?
One way is to use %in% like below:
df<-data.frame(var.a=rnorm(10), var.b=rnorm(10),var.c=rnorm(10))
print(df)
df <- df[,!(names(df) %in% c('var.a','var.b')),drop=FALSE]
print(df)
On Tue, Feb 24, 2009 at 3:10 PM, Sean Zhang wrote:
> Dear R-helpers:
>
> I am an R novice and would appreciate answer to the following
> question.
>
> Want to delete many variables in a dataframe.
> Am able to delete one variable by assigning it as NULL
> Have a large number of variables and would like to delete them without
> using
> a for loop.
>
> Is there a command/function which does this job?
>
> Many thanks in advance.
>
> -Sean
>
>
> #Small Example:
>
> 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
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
Christos Hatzis
2009-Feb-24 20:24 UTC
[R] how to NULL multiple variables of a df efficiently?
Setting to NULL works only if a single column is selected.
More generally,
df[, !(colnames(df) %in% c("var.b", "var.c")), drop=FALSE]
-Christos
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Sean Zhang
> Sent: Tuesday, February 24, 2009 3:10 PM
> To: r-help at r-project.org
> Subject: [R] how to NULL multiple variables of a df efficiently?
>
> Dear R-helpers:
>
> I am an R novice and would appreciate answer to the following
> question.
>
> Want to delete many variables in a dataframe.
> Am able to delete one variable by assigning it as NULL Have a
> large number of variables and would like to delete them
> without using a for loop.
>
> Is there a command/function which does this job?
>
> Many thanks in advance.
>
> -Sean
>
>
> #Small Example:
>
> 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
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>
>
Another way is:> subset(df, select=c(var.b, var.c))though I'd be willing to bet that using %in% is probably faster.[1] Tony [1] Unfortuantly I'm skint :-( On 24 Feb, 20:10, Sean Zhang <seane... at gmail.com> wrote:> Dear R-helpers: > > I am an R novice and would appreciate answer to the following question. > > Want to delete many variables in a dataframe. > Am able to delete one variable by assigning it as NULL > Have a large number of variables and would like to delete them without using > a for loop. > > Is there a command/function which does this job? > > Many thanks in advance. > > -Sean > > #Small Example: > > 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 > > ? ? ? ? [[alternative HTML version deleted]] > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.