[R] quote a column of a dataframe by its name

Jie jimmycloud at gmail.com
Fri Oct 4 18:10:21 CEST 2013


Sorry, this sample code seems to be OK.
I will look into my original problem and update it soon.

Best wishes,


On Fri, Oct 4, 2013 at 12:06 PM, Jie <jimmycloud at gmail.com> wrote:
> 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 X[,'xi'].
> 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)
> names(X)=c("x1","x2","x3","x4","x5")
>
> #pick the 4-th column
> X[,'x4']  #working
> X[,names(X)[4]]  # not working , so how to modify this line?
>
> names(X)[4] #  returns "x4"
> sQuote(names(X)[4])  # returns "'x4'"
>
>
> Best,



More information about the R-help mailing list