I have a variable (x) containing a character string (“b”). This string gives the name of a variable which is contained in a dataframe (ABC). When trying to use the function get() to retrieve the value of the variable in the dataframe, I get an “object not found” error.
a <- 1
b <- 2
c <- 3
ABC <- data.frame(a,b,c)
x <- "b"
This works:
get(x)
[1] 2
While this gives an error:
get(paste("ABC$",x,sep=""))
Error in get(paste("ABC$", x, sep = "")) : object 'ABC$b' not foundhere
Although this works:
ABC$b
[1] 2
How can I use the variable x to see the value of variable b?
Thanks in advance!
You are making things way too complex.
or
should work fine.