R 需要帮助将4个嵌套列表提取到数据框中吗
所讨论的数据是我转换为列表的JSON的结果。数据如下所示:R 需要帮助将4个嵌套列表提取到数据框中吗,r,R,所讨论的数据是我转换为列表的JSON的结果。数据如下所示: data <- list(query_day = c('Monday'), requester = c('John'), detail = list(list(ID = 1, weight = 200), list(ID = 2, weight = 300),
data <- list(query_day = c('Monday'),
requester = c('John'),
detail =
list(list(ID = 1, weight = 200),
list(ID = 2, weight = 300),
list(ID = 3, weight = 400,
detail2 = list(height = 6.5,
gender = 'M',
name = 'John')),
list(ID = 4, weight = 500),
list(ID = 5, weight = 600,
detail2 = list(height = 5.5,
gender = 'F',
name = 'Jane'))))
这适用于示例:结果是dataframe,而每列不是列表
最大的问题是,当我对数据集应用相同的代码时,我得到了
函数中出现错误…,row.names=NULL,check.rows=FALSE,check.names=TRUE,:参数表示行数不同:1,0
我认为不同的列不应该是一个问题,因为我在将它们转换为数据帧之前将它们分开
在第一个方法中,我希望输出的ID和weight列不是列表
感谢您提供的高级功能。要获得快速而肮脏的修复,您可以尝试使用rlist库
您将得到一个TIBLE,您可以在其上应用dplyr::selectcontainsdetail来提取所需的列。下面是另一种快速而肮脏的方法来检查数据结构:
library(dplyr)
library(jsonlite)
library(tidyverse)
> z <- data %>% toJSON() %>% fromJSON()
> z$detail
ID weight detail2.height detail2.gender detail2.name
1 1 200 NULL NULL NULL
2 2 300 NULL NULL NULL
3 3 400 6.5 M John
4 4 500 NULL NULL NULL
5 5 600 5.5 F Jane
> z.df <- as.data.frame(z$detail)
> z$detail$detail2
height gender name
1 NULL NULL NULL
2 NULL NULL NULL
3 6.5 M John
4 NULL NULL NULL
5 5.5 F Jane
下面是如何让它像一个数据帧乱七八糟但又有效的:
> zzz <- read.table(textConnection(captureOutput(print(z.df))),
stringsAsFactors=FALSE)
> zzz
ID weight detail2.height detail2.gender detail2.name
1 1 200 NULL NULL NULL
2 2 300 NULL NULL NULL
3 3 400 6.5 M John
4 4 500 NULL NULL NULL
5 5 600 5.5 F Jane
> str(zzz)
'data.frame': 5 obs. of 5 variables:
$ ID : int 1 2 3 4 5
$ weight : int 200 300 400 500 600
$ detail2.height: chr "NULL" "NULL" "6.5" "NULL" ...
$ detail2.gender: chr "NULL" "NULL" "M" "NULL" ...
$ detail2.name : chr "NULL" "NULL" "John" "NULL" ...
现在,您可以在发布的图像中精确地获得所需的数据帧:
> final.df <- zzz %>% replace(.=="NULL", NA) %>%
filter(!complete.cases(.)) %>% select(ID,weight) %>%
as.tibble()
> final.df
# A tibble: 3 x 2
ID weight
<int> <int>
1 1 200
2 2 300
3 4 500
是的,我刚刚添加了图像。那么您对从长度为2的细节中提取数据感兴趣吗?此外,您还需要向我们提供代码不适用的数据,以验证我们提供的答案是否适用于您的真实数据,而不仅仅是您共享的样本。
library(rlist)
rlist::list.flatten(data) %>% dplyr::bind_rows()
library(dplyr)
library(jsonlite)
library(tidyverse)
> z <- data %>% toJSON() %>% fromJSON()
> z$detail
ID weight detail2.height detail2.gender detail2.name
1 1 200 NULL NULL NULL
2 2 300 NULL NULL NULL
3 3 400 6.5 M John
4 4 500 NULL NULL NULL
5 5 600 5.5 F Jane
> z.df <- as.data.frame(z$detail)
> z$detail$detail2
height gender name
1 NULL NULL NULL
2 NULL NULL NULL
3 6.5 M John
4 NULL NULL NULL
5 5.5 F Jane
> zzz <- read.table(textConnection(captureOutput(print(z.df))),
stringsAsFactors=FALSE)
> zzz
ID weight detail2.height detail2.gender detail2.name
1 1 200 NULL NULL NULL
2 2 300 NULL NULL NULL
3 3 400 6.5 M John
4 4 500 NULL NULL NULL
5 5 600 5.5 F Jane
> str(zzz)
'data.frame': 5 obs. of 5 variables:
$ ID : int 1 2 3 4 5
$ weight : int 200 300 400 500 600
$ detail2.height: chr "NULL" "NULL" "6.5" "NULL" ...
$ detail2.gender: chr "NULL" "NULL" "M" "NULL" ...
$ detail2.name : chr "NULL" "NULL" "John" "NULL" ...
> final.df <- zzz %>% replace(.=="NULL", NA) %>%
filter(!complete.cases(.)) %>% select(ID,weight) %>%
as.tibble()
> final.df
# A tibble: 3 x 2
ID weight
<int> <int>
1 1 200
2 2 300
3 4 500