1
我HACE以下数据框:转换列表中的所有数据帧行的字符串
json = '[
{"id":"1","list":["A","B"]},
{"id":"2","list":["C","D"]}
]'
df <- fromJSON(json)
df
输出:
id list
1 1 c("A", "B")
2 2 c("C", "D")
现在,我想要列表是这样的字符串:
id list
1 1 "A, B"
2 2 "C, D"
所以,我试过以下但没有变化:
df$list <- paste(df$list, sep = ", ")
我也试过以下,但它concats两个列表中每一行中:
df$list <- toString(df$list)
# Output
id list
1 1 c("A", "B"), c("C", "D")
2 2 c("A", "B"), c("C", "D")
有没有办法单独更改每一行?
另一种解决方案是将JSON数组直接导入给定的格式,这可能吗?
谢谢!
如果使用粘贴,你有超过你如何结合个人列表元素,例如多一点控制sapply(df $ list,paste0,collapse =“ - ”) – Martin
@Martin是的,那是对的 – akrun