An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101202/46c93a1a/attachment.pl>
colname refered by a variable
5 messages · Yuan Jian, Santosh Srinivas, Ivan Calandra +2 more
try this ..
df[,colnames(df)==paste("A","C",sep="")]
On Fri, Dec 3, 2010 at 12:05 PM, Yuan Jian <jayuan2008 at yahoo.com> wrote:
Hello, I tried to use a?variable to refer colname, but I got error, could anyone give me advice?
df=data.frame(cbind(AB=1:3,AC=3:5)) df$AC
[1] 3 4 5
df$paste("A","C",sep="")
Error: attempt to apply non-function thanks Jian ? ? ? ?[[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.
Or simpler:
df[, paste("A","C",sep="")]
df[[paste("A","C",sep="")]]
Or:
x <- paste("A","C",sep="")
df[,x]
df[[x]]
Btw, you don't need to use cbind(), data.frame() does it already
Ivan
Le 12/3/2010 08:21, Santosh Srinivas a ?crit :
try this ..
df[,colnames(df)==paste("A","C",sep="")]
On Fri, Dec 3, 2010 at 12:05 PM, Yuan Jian<jayuan2008 at yahoo.com> wrote:
Hello, I tried to use a variable to refer colname, but I got error, could anyone give me advice?
df=data.frame(cbind(AB=1:3,AC=3:5)) df$AC
[1] 3 4 5
df$paste("A","C",sep="")
Error: attempt to apply non-function
thanks
Jian
[[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.
______________________________________________ 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.
Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
or even shorter
df[,paste("A","C",sep="")]
"Santosh Srinivas" <santosh.srinivas at gmail.com> wrote in message
news:AANLkTikcJy7BVyfBwUwmRQ4dHg4pBdau+Qh_7+K+bRt2 at mail.gmail.com...
try this ..
df[,colnames(df)==paste("A","C",sep="")]
On Fri, Dec 3, 2010 at 12:05 PM, Yuan Jian <jayuan2008 at yahoo.com> wrote:
Hello, I tried to use a variable to refer colname, but I got error, could anyone give me advice?
df=data.frame(cbind(AB=1:3,AC=3:5)) df$AC
[1] 3 4 5
df$paste("A","C",sep="")
Error: attempt to apply non-function thanks Jian [[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.
On Dec 3, 2010, at 4:14 AM, Keith Jewell wrote:
or even shorter
df[,paste("A","C",sep="")]
Other grepping methods that generalize better to partial matches:
df[ , grep("^AC$", colnames(df))]
df[ grep("^AC$", colnames(df)) ]
--
David.
"Santosh Srinivas" <santosh.srinivas at gmail.com> wrote in message
news:AANLkTikcJy7BVyfBwUwmRQ4dHg4pBdau+Qh_7+K+bRt2 at mail.gmail.com...
try this ..
df[,colnames(df)==paste("A","C",sep="")]
On Fri, Dec 3, 2010 at 12:05 PM, Yuan Jian <jayuan2008 at yahoo.com>
wrote:
Hello, I tried to use a variable to refer colname, but I got error, could anyone give me advice?
df=data.frame(cbind(AB=1:3,AC=3:5)) df$AC
[1] 3 4 5
df$paste("A","C",sep="")
Error: attempt to apply non-function thanks Jian
David Winsemius, MD West Hartford, CT