R: constructing a data frame with many columns using paste()

前端 未结 2 974
萌比男神i
萌比男神i 2021-01-28 02:02
col1 <- c(1, 2, 3)
col2 <- c(4, 5, 6)
col3 <- c(7, 8, 9)
  • On the one hand data.frame(col1, col2, col3)

gives<

2条回答
  •  时光取名叫无心
    2021-01-28 02:11

    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]])) 
    

    data

    str1 <- paste0("col", 1:3, collapse=", ")
    

提交回复
热议问题