R将嵌套值转换为新的列和行

时间:2018-04-24 09:08:04

标签: r

使用如下数据框:

currencyDetails = c("Dollar:40, Euro:80, valid",
               "Yen:400, Pound:50",
               "Dollar:40, Pound:50, currency",
               NA)

有人可以推荐一种方法来附加到该数据帧,新的列和行对应于每行中的对和值吗?每个字符串中的对或值的数量不固定,也没有排序。

样本输出:

Dollar = c(40,NA,NA,NA)
Pound = c(NA,50,50,NA)
Euro = c(80,NA,NA,NA)
Yen = c(NA,400,NA,NA)
valid = c(1,0,0,0)
currency = c(0,0,1,0)
df = data.frame(Dollar, Pound, Euro,Yen,valid, currency)
cbind(currencyDetails, df)

                currencyDetails Dollar Pound Euro Yen valid currency
1     Dollar:40, Euro:80, valid     40    NA   80  NA     1        0
2             Yen:400, Pound:50     NA    50   NA 400     0        0
3 Dollar:40, Pound:50, currency     NA    50   NA  NA     0        1
4                          <NA>     NA    NA   NA  NA     0        0

我认为它与之前提供的答案不同,因为分割出密钥的额外复杂性:值对,并不是每个元素都按原样转换为列名。例如,Pound:50不是列,Pound是50,因为它是值。

1 个答案:

答案 0 :(得分:0)

试试这个解决方案:

splitted<-trimws(unlist(strsplit(aDataFrame,",")))

t(bind_rows(sapply(splitted[grep(":",splitted)],strsplit,split=":")))
          [,1]     [,2] 
Dollar:40 "Dollar" "40" 
Euro:80   "Euro"   "80" 
Yen:400   "Yen"    "400"
Pound:50  "Pound"  "50"

更新

df<-data.frame(t(bind_rows(sapply(splitted[grep(":",splitted)],strsplit,split=":"))))
> library(reshape2)
> acast(df, X2 ~ X1)
Using X2 as value column: use value.var to override.
    Dollar Euro Pound Yen 
40  40     <NA> <NA>  <NA>
400 <NA>   <NA> <NA>  400 
50  <NA>   <NA> 50    <NA>
80  <NA>   80   <NA>  <NA>
Levels: 40 400 50 80