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