Reputation: 73
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
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 paste
ing with mget
(as paste
is vectorized)
lst1 <- mget(paste0(c("US", "UK"), "_SA"))
Upvotes: 3