【问题标题】:unnesting list inside of a list, inside of a list, inside of a list... while preserving id in R在列表内、列表内、列表内取消嵌套列表...同时在 R 中保留 id
【发布时间】:2020-07-28 08:08:39
【问题描述】:

我导入了一个具有以下结构的 JSON 文件: link

我想将其转换为具有 3 列的数据框:IDgroup_namedate_joined, 其中 ID 是“数据”列表中的元素编号。

应该是这样的:

ID  group_name  date_joined
1   aaa     dttm
1   bbb     dttm
1   ccc     dttm
1   ddd     dttm
2   eee     dttm
2   aaa     dttm
2   bbb     dttm
2   fff     dttm
2   ggg     dttm
3   bbb     dttm
3   ccc     dttm
3   ggg     dttm
3   mmm     dttm

使用下面的代码几次,我得到一个只有 2 列的数据框:group_namedate_joined

train2 <- do.call("rbind", train2)

示例文件link

【问题讨论】:

  • 您应该提供一些示例数据,以便人们可以更好地帮助您。您可以使用dput() 命令提供帮助。
  • 您可以尝试rapply(train2,rbind),但同样,如果没有示例数据,它很难提供帮助。
  • 添加示例文件
  • 您应该分享dput(train2) 的输出或包含您用来将json 文件转换为R 对象的库和代码。这样我们就可以完全按照您的方式复制您的数据。
  • train2 &lt;- fromJSON("train.json") train2 &lt;- do.call("rbind", train2) train2 &lt;- train2[1:5,] train2 &lt;- list(train2) exportJSON &lt;- toJSON(train2) write(exportJSON, "sample.json")

标签: r json list


【解决方案1】:

以下应该有效:

library(jsonlite)

train2 <- fromJSON("sample.json") 
train2 <- train2[[1]]$groups$data    

df <- data.frame(
      ID = unlist(lapply(1:length(train2), function(x) rep.int(x,length(train2[[x]]$group_name)))),
      group_name = unlist(lapply(1:length(train2),function(x) train2[[x]]$group_name)),
      date_joined = unlist(lapply(1:length(train2),function(x) train2[[x]]$date_joined)))

输出:

> df
   ID                                                                            group_name                date_joined
1   1 Let's excercise together and lose a few kilo quicker - everyone is welcome! (Piastow) 2008-09-05 09:55:18.730066
2   1                                                                 Strongman competition 2008-05-22 21:25:22.572365
3   1                                                                      Fast food 4 life 2012-02-02 05:26:01.293628
4   1                                 alternative medicine - Hypnosis and bioenergotheraphy 2008-07-05 05:47:12.254848
5   2                                                                      Tom Cruise group 2009-06-14 16:48:28.606142
6   2                                                                  Babysitters (Sokoka) 2010-09-25 03:21:01.944684
7   2           Work abroad - join to find well paid work and enjoy the experience (Sokoka) 2010-09-21 23:44:39.499240
8   2       Tennis, Squash, Badminton, table tennis - looking for sparring partner (Sokoka) 2007-10-09 17:15:13.896508
9   2                                                                  Lost&Found  (Sokoka) 2007-01-03 04:49:01.499555
10  3                                                         Polish wildlife - best places 2007-07-29 18:15:49.603727
11  3                                                              Politics and politicians 2010-10-03 21:00:27.154597
12  3                                                                 Pizza ! Best recipes  2010-08-25 22:26:48.331266
13  3                                            Animal rights group - join us if you care! 2010-11-02 12:41:37.753989
14  4                                                                   The Aspiring Writer 2009-09-08 15:49:57.132171
15  4                                                              Nutrition & food advices 2010-12-02 18:19:30.887307
16  4                                                                       Game of thrones 2009-09-18 10:00:16.190795
17  5                                                  The ultimate house and electro group 2008-01-02 14:57:39.269135
18  5                                                              Pirates of the Carribean 2012-03-05 03:28:37.972484
19  5                                                 Musicians Available Poland (Osieczna) 2009-12-21 13:48:10.887986
20  5                 Housekeeping - looking for a housekeeper ? Join the group! (Osieczna) 2008-10-28 23:22:26.159789
21  5                                                             Rooms for rent (Osieczna) 2012-08-09 12:14:34.190438
22  5                                                   Counter strike - global ladderboard 2008-11-28 03:33:43.272435
23  5                                                              Nutrition & food advices 2011-02-08 19:38:58.932003

【讨论】:

  • train2 &lt;- train2[[1]]$groups$data 之后我们有一个列表,其中每个元素都是一个用户。我想将元素编号保留为 id,以便将 group_names 与用户匹配。
  • @Szym_Szym 我在新的df 中添加了另一列,用于处理ID。如果这对您有用,请考虑接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
相关资源
最近更新 更多