抱歉这个不善言辞的标题!
某些货币由多个国家/地区使用
我有一个表格,将一个国家/地区组合在一起
df<- data.frame(code=c("DKK","DZD","EGP"), country=c("Denmark, Faroe Islands, Greenland",
"Algeria","Egypt,Palestinian territories"))
code country
1 DKK Denmark, Faroe Islands, Greenland
2 DZD Algeria
3 EGP Egypt,Palestinian territories
我想将这个组合字段分开,以便我最终得到
code country
1 DKK Denmark
2 DKK Faroe Islands
3 DKK Greenland
4 DZD Algeria
5 EGP Egypt
6 EGP Palestinian territories
TIA
答案 0 :(得分:0)
这里有3种可能性。一个只使用原生R:
endresult = do.call("rbind",by(df,df$code,
function(x) { data.frame(cbind(as.character(x$code),
unlist(strsplit(paste(x$country,collapse=","),",")[[1]]))) } ))
rownames(endresult) = NULL
或:
tf = by(df$country,df$code,function(x) strsplit(paste(x,collapse=","),",")[[1]] )
endresult = do.call("rbind",lapply(names(tf),function(x) { vals=tf[x][[1]]; data.frame("code"=rep(x,length(vals)),"country"=vals ) } ))
另一个使用plyr:
require(plyr)
endresult = ddply(df, "code",
function(x) { cbind(as.character(x$code),
unlist(strsplit(paste(x$country,collapse=","),",")[[1]])) } )
我相信还有很多方法可以找到。