ozlem
ozlem

Reputation: 73

Referring to a column in data frame with paste0 name

I have a data frame with name paste0(i, "_SA") which is created in a loop.I want to print the 4th column but neither $ nor [,4] works. paste0(i, "_SA")[,4] gives error "incorrect number of dimensions" even though if I use this outside of the loop like US_SA[,4] it works. How do you refer to a column of paste0 named data frame?

Upvotes: 3

Views: 2451

Answers (1)

akrun
akrun

Reputation: 887911

We can use get to get the values of the object

for(i in c("US", "UK")) {
       print(get(paste0(i, "_SA"))[, 4])
  }

It can be also loaded into a list after pasteing with mget (as paste is vectorized)

lst1 <- mget(paste0(c("US", "UK"), "_SA"))

Upvotes: 3

Related Questions