如何将不等长的嵌套列表转换为数据框?

时间:2019-06-27 18:52:08

标签: r list dataframe

我嵌套了不等长的列表,并希望将其转换为数据帧。

我的列表看起来像这样。它包含两个列表。在第一个列表中,它具有5个变量(时间,用户,位置,配置文件,URL)。在第二个列表中,它具有3个变量(时间,用户,配置文件)。其中一些变量是列表,其中进一步包含不同数量的变量。

listname$results[[1]]$time
listname$results[[1]]$user$name
listname$results[[1]]$user$id
listname$results[[1]]$user$position
listname$results[[1]]$location$lat
listname$results[[1]]$location$long
listname$results[[1]]$profile
listname$results[[1]]$url

listname$results[[2]]$time
listname$results[[2]]$user$name
listname$results[[2]]$user$id 
listname$results[[2]]$location$lat
listname$results[[2]]$profile

我的输出应如下所示:

time    user_name    user_id    user_position    location_lat    location_long    profile    url
12:00   abc          001        manager          24.22           56.34            Mark       abc.com
21:50   xyz          002        NA               45.93           NA               John       NA

我尝试了此操作,但出现错误“矩阵上的下标数量不正确”。

plyr::ldply(listname$results[[1]], rbind)

上面的代码在没有嵌套列表(例如“用户”)的情况下有效

plyr::ldply(listname$results[[1]]$user, rbind)

0 个答案:

没有答案
相关问题