R: constructing a data frame with many columns using paste() -
col1 <- c(1, 2, 3) col2 <- c(4, 5, 6) col3 <- c(7, 8, 9)
- on 1 hand
data.frame(col1, col2, col3)
gives
col1 col2 col3 1 1 4 7 2 2 5 8 3 3 6 9
- on other hand
paste0("col", 1:3, collapse=", ")
gives
[1] "col1, col2, col3"
- question: possible construct data frame using
data.frame(paste0("col", 1:3, collapse=", "))
?
if have string have objects pasted together, can use strsplit
split string , values mget
. return list
output. wrap data.frame
convert 'data.frame`
data.frame(mget(strsplit(str1, ', ')[[1]]))
data
str1 <- paste0("col", 1:3, collapse=", ")
Comments
Post a Comment