col1 <- c(1, 2, 3)
col2 <- c(4, 5, 6)
col3 <- c(7, 8, 9)
data.frame(col1, col2, col3)
gives<
If we already have a string that have the objects pasted together, we can use strsplit
to split the string and get the values with mget
. This will return a list
output. Then wrap it with data.frame
to convert it to 'data.frame`
data.frame(mget(strsplit(str1, ', ')[[1]]))
str1 <- paste0("col", 1:3, collapse=", ")
You may want to use mget
do.call(cbind, mget(paste0("col", 1:3)))
Where the paste0
generates the variable names, mget
gets the associated values, and cbind
puts them together into a data frame.