根据R中相同列表中的字符串创建新列

时间:2018-12-12 05:28:02

标签: r json string list data.table

对不起,如果重复的话。经过三十分钟的搜索,我找不到类似的东西。

dput:

structure(list(i = c("KHH", "KHH", "KHH", "KHH", "KHH", "KHH", 
"KHH", "KHH", "KHH", "KHH", "KHH", "KHH", "KHH", "KHH", "KHH", 
"KHH", "KHH", "KHH", "YYR", "YYR", "YYR", "YYR", "YYR", "YYR", 
"YYR", "YYR"), n = c("Changzhi", "Chaochou", "Chaozhou", "Checheng", 
"Donggang", "Fangliao", "Fangshan", "Fengshan", "Gaoshu", "Hengchun Old Town", 
"Jiuru", "Linbian", "Liuqiu", "Manzhou", "Pingtung", "Tungkang", 
"Wanluan", "Yanpu", "Cartwright", "Happy Valley - Goose Bay", 
"Hopedale", "Makkovik", "Nain", "Natuashish", "Postville", "Rigolet"
), c = c("Taiwan", "Taiwan", "Taiwan", "Taiwan", "Taiwan", "Taiwan", 
"Taiwan", "Taiwan", "Taiwan", "Taiwan", "Taiwan", "Taiwan", "Taiwan", 
"Taiwan", "Taiwan", "Taiwan", "Taiwan", "Taiwan", "Canada", "Canada", 
"Canada", "Canada", "Canada", "Canada", "Canada", "Canada"), 
    C = c("TW", "TW", "TW", "TW", "TW", "TW", "TW", "TW", "TW", 
    "TW", "TW", "TW", "TW", "TW", "TW", "TW", "TW", "TW", "CA", 
    "CA", "CA", "CA", "CA", "CA", "CA", "CA"), r = c("Eastern Asia", 
    "Eastern Asia", "Eastern Asia", "Eastern Asia", "Eastern Asia", 
    "Eastern Asia", "Eastern Asia", "Eastern Asia", "Eastern Asia", 
    "Eastern Asia", "Eastern Asia", "Eastern Asia", "Eastern Asia", 
    "Eastern Asia", "Eastern Asia", "Eastern Asia", "Eastern Asia", 
    "Eastern Asia", "Northern America", "Northern America", "Northern America", 
    "Northern America", "Northern America", "Northern America", 
    "Northern America", "Northern America"), L = c(120.576, 120.539, 
    120.533, 120.717, 120.482, 120.593, 120.657, 120.35, 120.625, 
    120.744, 120.487, 120.488, 120.383, 120.848, 120.488, 120.449, 
    120.625, 120.574, -57.042, -60.377, -60.229, -59.186, -61.68, 
    -61.184, -59.785, -58.458), F = c(22.701, 22.55, 22.552, 
    22.06, 22.459, 22.368, 22.249, 22.633, 22.737, 22.005, 22.725, 
    22.428, 22.352, 21.991, 22.672, 22.465, 22.612, 22.76, 53.683, 
    53.31, 55.448, 55.077, 56.549, 55.914, 54.91, 54.18)), sorted = "i", class = c("data.table", 
"data.frame"), row.names = c(NA, -26L))

dput无法与list / data.table配合使用,因此我将其放置为数据框。如果可以帮助您将其放入列表,则可以运行split(df[, -1],df$i)

我想取每个列表中每一行的名称,并将其放入每行中的新列表中。

我有一个很大的列表,看起来像这样:

    i                        n      c  C                r       L      F
1 YYR               Cartwright Canada CA Northern America -57.042 53.683
2 YYR Happy Valley - Goose Bay Canada CA Northern America -60.377 53.310
3 YYR                 Hopedale Canada CA Northern America -60.229 55.448
4 YYR                 Makkovik Canada CA Northern America -59.186 55.077
5 YYR                     Nain Canada CA Northern America -61.680 56.549   <-- this row is used in the example
6 YYR               Natuashish Canada CA Northern America -61.184 55.914
...

我希望输出看起来像这样:

df $ YYR

             n      c  C                r       L      F x
1:  Cartwright Canada CA Northern America -59.785 54.910 list()
...

df $ YYR [“ n”] [5,]

[1] "Nain"
...

df $ YYR [[“ x”]] [5] <-这是必需的结构

[[5]]
                         d n      index
1                      YYR Nain   0
2               Cartwright Nain   1
3 Happy Valley - Goose Bay Nain   2
4                 Hopedale Nain   3
5                 Makkovik Nain   4
6               Natuashish Nain   6

...

toJSON(df $ YYR [“ x”] [5,]) <-希望这是有道理的,因为它也使我感到困惑

[
  {
    "d": "YYR",
    "n": "Nain",
    "index": 0
  },
  {
    "d": "Cartwright",
    "n": "Nain",
    "index": 1
  },
  {
    "d": "Happy Valley - Goose Bay",
    "n": "Nain",
    "index": 2
  },
  {
    "d": "Hopedale",
    "n": "Nain",
    "index": 3
  },
  {
    "d": "Makkovik",
    "n": "Nain",
    "index": 4
  },
...
]

类似于每个列表元素中的expand.grid或笛卡尔联接,但还包括将父列表元素的引用作为0索引值

1 个答案:

答案 0 :(得分:1)

我不完全了解您想要什么,但这会产生示例结果:

res = df[, .(d = c(i, n), idx = 0:.N), by = i]
res[res[idx > 0], on = .(i), allow = T, .(d, n = i.d, idx)][d != n]
#              d        n idx
#  1:        KHH Changzhi   0
#  2:   Chaochou Changzhi   2
#  3:   Chaozhou Changzhi   3
#  4:   Checheng Changzhi   4
#  5:   Donggang Changzhi   5
# ---                        
#384:   Hopedale  Rigolet   3
#385:   Makkovik  Rigolet   4
#386:       Nain  Rigolet   5
#387: Natuashish  Rigolet   6
#388:  Postville  Rigolet   7