Re: [R] quote a column of a dataframe by its name
On Oct 5, 2013, at 00:47 , Rolf Turner wrote: On 10/05/13 05:15, John Kane wrote: X[,names(X)[4]] works fine for me. I had never thought of doing this. Neat idea. Perhaps I am being obtuse, but how would X[,names(X)[4]] differ from X[,4]? By answering the question in the Subject field? (No-one intended to use it in production code. The mystery is that it - reportedly - threw an error for the original poster, but not for anyone else.) -pd -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ 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.
[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 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, __ 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.
Re: [R] quote a column of a dataframe by its name
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 jimmycl...@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, __ 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.
Re: [R] quote a column of a dataframe by its name
Hello, I had no problems, and it shouldn't. What exactly do you mean by not working? Hope this helps, Rui Barradas Em 04-10-2013 17:06, Jie escreveu: 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, __ 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. __ 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.
Re: [R] quote a column of a dataframe by its name
X[,names(X)[4]] works fine for me. I had never thought of doing this. Neat idea. John Kane Kingston ON Canada -Original Message- From: jimmycl...@gmail.com Sent: Fri, 4 Oct 2013 12:06:50 -0400 To: r-help@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 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, __ 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. FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks orcas on your desktop! __ 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.
Re: [R] quote a column of a dataframe by its name
Is there ever a case that X[,names(X)[4]] would give a different result than X[,4]? Or is this just a case of the the longest distance between any 2 points is a shortcut? Well I guess if X has non-unique names then you might see a difference, but having a data frame with non-unique names and using the longer version above is more likely to be asking for trouble than doing anything useful. On Fri, Oct 4, 2013 at 10:15 AM, John Kane jrkrid...@inbox.com wrote: X[,names(X)[4]] works fine for me. I had never thought of doing this. Neat idea. John Kane Kingston ON Canada -Original Message- From: jimmycl...@gmail.com Sent: Fri, 4 Oct 2013 12:06:50 -0400 To: r-help@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 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, __ 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. FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks orcas on your desktop! __ 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. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com [[alternative HTML version deleted]] __ 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.
Re: [R] quote a column of a dataframe by its name
On 10/05/13 05:15, John Kane wrote: X[,names(X)[4]] works fine for me. I had never thought of doing this. Neat idea. Perhaps I am being obtuse, but how would X[,names(X)[4]] differ from X[,4]? cheers, Rolf Turner __ 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.