将列表中的列表处理为R中的数据帧
我是新来的,在处理列表和转换到数据帧时遇到了一些问题 我有一份“滴滴涕”清单 我需要这个结果将列表中的列表处理为R中的数据帧,r,list,dataframe,R,List,Dataframe,我是新来的,在处理列表和转换到数据帧时遇到了一些问题 我有一份“滴滴涕”清单 我需要这个结果 id timestamp text id2 1 18136 2020-05-25T16:17:32+0000 Mocaaa 18096 2 18136 2020-05-25T16:00:00+0000 Capucchino 17846 3 18136 2020-05-25T14:42:53+0000
id timestamp text id2
1 18136 2020-05-25T16:17:32+0000 Mocaaa 18096
2 18136 2020-05-25T16:00:00+0000 Capucchino 17846
3 18136 2020-05-25T14:42:53+0000 Mocachino 18037
4 17920 2020-05-24T15:31:30+0000 Hello 18054
我认为使用
data.table
可以很好地实现这一点
set.seed(42)
df该结构看起来就像一个java脚本对象
你可以做:
library(jsonlite)
library(tidyr)
unnest(unnest(fromJSON(toJSON(df))))
# A tibble: 6 x 4
id tm text id1
<int> <chr> <chr> <int>
1 92345 2020-05-26 14:53:53 X 6730
2 92345 2020-05-26 14:53:56 Q 92812
3 92345 2020-05-26 14:53:56 D 25304
4 9847 2020-05-26 14:53:56 E 82734
5 9847 2020-05-26 14:54:01 I 75079
6 9847 2020-05-26 14:54:02 H 89373
library(jsonlite)
图书馆(tidyr)
unnest(unnest(fromJSON(toJSON(df)))
#一个tibble:6x4
IDTM文本id1
1923452020-05-26 14:53:53 X 6730
292345 2020-05-26 14:53:56 Q 92812
392345 2020-05-26 14:53:56 D 25304
49847200-05-2614:53:56 E 82734
59847200-05-2614:54:01I75079
69847200-05-2614:54:02H89373
请使用dput
演示测试示例,谢谢Shello Javier!,只需在控制台中键入以下代码:dput(ddt)
,然后将结果复制到您的问题中。我考虑过这一点,虽然我通常对递归函数很熟悉,但它们可能会失控。我希望这个固定深度的例子不需要递归,尽管看到一个稳定的递归方法会很有趣!是的,我应该考虑尝试双json双嵌套视图。很好:-)
library(jsonlite)
library(tidyr)
unnest(unnest(fromJSON(toJSON(df))))
# A tibble: 6 x 4
id tm text id1
<int> <chr> <chr> <int>
1 92345 2020-05-26 14:53:53 X 6730
2 92345 2020-05-26 14:53:56 Q 92812
3 92345 2020-05-26 14:53:56 D 25304
4 9847 2020-05-26 14:53:56 E 82734
5 9847 2020-05-26 14:54:01 I 75079
6 9847 2020-05-26 14:54:02 H 89373