0

I want to refer to a data frame by using a character vector.

I believe the simple example below illustrates the problem.

# I have a data frame called A
A <- c(1, 2, 3, 4)

# I have a character vector called B, containing the character "A"
B <- "A"

# Now I want a third vector (C) to get the content of A, simply by referring to vector B 
# Obviously, I cannot write
C <- B 
# ... as this would give me 
[1] "A"

# ... and NOT what I want:
[1] 1 2 3 4

How do I use a character vector to refer to the name and thus the content of an existing data frame?

PS. I have been made aware of my questiong being a duplicate. But since the wordings are different, I didn't find the other post when searching online: Access variable value where the name of variable is stored in a string I keep my post, as others too might fail to find the earlier one.

cibr
  • 453
  • 5
  • 16

1 Answers1

3

It would be get to return the value of the object name as string

C <- get(B)

If there are more than objects, use mget to return the values in a list

akrun
  • 874,273
  • 37
  • 540
  • 662