0

I apologize since I'm sure this is an obvious issue, but I just can't seem to find the correct search terms to come up with the answer.

If I have a dataframe like this:

example<-cbind(rbind(1,2,3),rbind("a","b","c"))
colnames(example)<-c("a","b")
example<-as.data.frame(example)

And I want to extract the values from column a using a variable x,

x<-a

How do I go about this? When I try:

 example$x

I get a null. How do I make this work?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Brian M
  • 11
  • 1
  • 1
    I am not really sure what you are trying to do... what do you expect as an output? If you want to get column `a` just use `example$a`. Is this what you are looking for? – nico Sep 03 '14 at 20:43
  • `$` can't evaluate RHS. You can only use the actual name (not variable) when using `$`. You need to do `example[, x]` instead – David Arenburg Sep 03 '14 at 20:50
  • or `[[`. There is a canonical duplicate for this but I can't find it right now. – Ben Bolker Sep 03 '14 at 21:17
  • Thanks for the help, I figured that this would be a duplicate, but I couldn't find the right keywords I guess. Sorry about that! – Brian M Sep 12 '14 at 22:00

1 Answers1

0

I'm assuming a is a character:

x <- "a"
example[,x]
Señor O
  • 17,049
  • 2
  • 45
  • 47