连接R中的列表元素

时间:2014-03-13 11:50:03

标签: string r list

我有一个列表,其中每个元素包含如下字符串:

[[1]] 
 [1] 
 "diane"    "bradbury"

[[2]] 
 [1] "diane"    "bradbury"

 [[3]] 
  [1] "derek" "legg"

我需要获得的是这样的单列数据框:

"diane bradbury" 
"diane bradbury" 
"derek legg"
你能告诉我吗? 感谢。

1 个答案:

答案 0 :(得分:4)

只需将pastesapply

一起使用即可
myList <- list(c("A", "B"), c("C", "D"), c("E", "F"))

myList
# [[1]]
# [1] "A" "B"
# 
# [[2]]
# [1] "C" "D"
# 
# [[3]]
# [1] "E" "F"

sapply(myList, paste, collapse = " ")
# [1] "A B" "C D" "E F"