将列表的data.frame转换为data.frame
我想转换这个data.frame将列表的data.frame转换为data.frame,r,list,dataframe,R,List,Dataframe,我想转换这个data.frame Loc Time(h) Paris, Luxembourg 10,15 Paris, Lyon, Berlin 9,12,11 对此 Loc Time(h) Paris 10 Luxembourg 15 Paris
Loc Time(h)
Paris, Luxembourg 10,15
Paris, Lyon, Berlin 9,12,11
对此
Loc Time(h)
Paris 10
Luxembourg 15
Paris 9
Lyon 12
Berlin 11
假设数据帧中的每个条目都是一个字符串,其形式与上面所说的相同,则可以执行以下操作
#notice the space in ", " for the first line
newLoc<-sapply(df$Loc, function(entry) {unlist(strsplit(entry,", ", fixed=TRUE))})
#and the lack there of in the second
newTime<-sapply(df$`Time(h)`, function(entry) {unlist(strsplit(entry, ",", fixed=TRUE))})
如果安装了data.table,您可以使用Ananda Mahto的 如果dat是您的数据
devtools::source_gist(11380733)
cSplit(dat, c("Loc", "Time"), direction = "long")
# Loc Time
# 1: Paris 10
# 2: Luxembourg 15
# 3: Paris 9
# 4: Lyon 12
# 5: Berlin 11
请将dputyourdata的输出添加到问题中。我刚刚发布了同样的内容:-如果你想添加明显的列表变量,因为问题不清楚,我也建议使用as.data.framelapplydf2,unlist。@AnandaMahto-这与.data.frameas.listdf2不一样吗?也许我只是累了,但我认为它们不一样。。。。我对df2的测试用例是:df2嗯,也许我会删除它,这样你就可以在你的答案中更好地解释它。
data.frame(cbind(Loc=newLoc, `Time(h)`=newTime))
devtools::source_gist(11380733)
cSplit(dat, c("Loc", "Time"), direction = "long")
# Loc Time
# 1: Paris 10
# 2: Luxembourg 15
# 3: Paris 9
# 4: Lyon 12
# 5: Berlin 11