[R] quote a column of a dataframe by its name
jrkrideau at inbox.com
Fri Oct 4 18:15:02 CEST 2013
X[,names(X)] works fine for me. I had never thought of doing this. Neat idea.
Kingston ON Canada
> -----Original Message-----
> From: jimmycloud at gmail.com
> Sent: Fri, 4 Oct 2013 12:06:50 -0400
> To: r-help at r-project.org
> Subject: [R] quote a column of a dataframe by its name
> Dear All,
> I have a question, suppose X is a dataframe, with column names as
> "x1", "x2", "x3", ..... And I would like to use the i-th column by
> But it seems the single quote and double quote are different.
> So if I run X[, names(X)[i]], it has some error.
> Please use the below example code
> X = matrix(rnorm(50),ncol = 5)
> X = data.frame(X)
> #pick the 4-th column
> X[,'x4'] #working
> X[,names(X)] # not working , so how to modify this line?
> names(X) # returns "x4"
> sQuote(names(X)) # returns "'x4'"
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
More information about the R-help