Loc Time(h)
Paris, Luxembourg 10,15
Paris, Lyon, Berlin 9,12,11
改造这个data.frame这种
Loc Time(h)
Paris 10
Luxembourg 15
Paris 9
Lyon 12
Berlin 11
Loc Time(h)
Paris, Luxembourg 10,15
Paris, Lyon, Berlin 9,12,11
改造这个data.frame这种
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))})
我想我们还需要扁平化的结果
dim(newLoc)<-NULL
dim(newTime)<-NULL
然后合并回一个DF
data.frame(cbind(Loc=newLoc, `Time(h)`=newTime))
你可以使用阿难Mahto的cSplit
function,只要你有data.table
安装。
如果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
我只是发布相同的东西:-) – A5C1D2H2I1M1N2O1R2T1 2014-10-09 17:32:52
如果你想添加明显的'list'变体(因为问题不清楚),我还提出了'as.data.frame(lapply(df2,unlist))' 。 – A5C1D2H2I1M1N2O1R2T1 2014-10-09 17:33:55
@AnandaMahto - 和as.data.frame(as.list(df2))'不一样吗? – 2014-10-09 17:58:54
请'dput(yourdata)'的输出增加的问题。 – Roland 2014-10-09 17:23:07