R - 将具有 NULL 类型的列表转换为数据帧

时间:2021-06-02 22:30:33

标签: r list dataframe

我有以下列表:

test <- list(author = list(name = "Yihui Xie", email = "BLINDED", 
    date = "2021-04-14T15:26:29Z"), committer = list(name = "Yihui Xie", 
    email = "xie@yihui.name", date = "2021-04-14T15:27:13Z"), 
    message = "start the next version", tree = list(sha = "f4032fb23c2e3c6005a76f4e117f6489d321c721", 
        url = "https://api.github.com/repos/rstudio/blogdown/git/trees/f4032fb23c2e3c6005a76f4e117f6489d321c721"), 
    url = "https://api.github.com/repos/rstudio/blogdown/git/commits/5aeb809c68cfa1a9e616bc9ed9878c3ea5d05300", 
    comment_count = 0L, verification = list(verified = FALSE, 
        reason = "unsigned", signature = NULL, payload = NULL))

我需要将其转换为数据帧,问题是我有两个元素是 NULL我需要保留列,并将内容切换为 NA 或空字符串。我是批处理,所以我可能有其他值 null。

常规更改不起作用:

> as.data.frame(test)
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 1, 0

预期输出:

以下是我能够正确转换的另一个列表。正如你所看到的,这个没有缺失值,但我在这里添加它,所以你可以看到我需要什么(这是转换的 dput):

structure(list(author.name = structure(1L, .Label = "Christophe Dervieux", class = "factor"), 
    author.email = structure(1L, .Label = "BLINDED", class = "factor"), 
    author.date = structure(1L, .Label = "2021-05-26T16:19:44Z", class = "factor"), 
    committer.name = structure(1L, .Label = "GitHub", class = "factor"), 
    committer.email = structure(1L, .Label = "noreply@github.com", class = "factor"), 
    committer.date = structure(1L, .Label = "2021-05-26T16:19:44Z", class = "factor"), 
    message = structure(1L, .Label = "clean_duplicates() is now aware of blogdown rendering method (#629)", class = "factor"), 
    tree.sha = structure(1L, .Label = "f1d056b93ce0d060501d5fd6b9e9df2d934059f6", class = "factor"), 
    tree.url = structure(1L, .Label = "https://api.github.com/repos/rstudio/blogdown/git/trees/f1d056b93ce0d060501d5fd6b9e9df2d934059f6", class = "factor"), 
    url = structure(1L, .Label = "https://api.github.com/repos/rstudio/blogdown/git/commits/00a20903f0b2953f8f350d69bffdcd9c50cda5b1", class = "factor"), 
    comment_count = 0L, verification.verified = TRUE, verification.reason = structure(1L, .Label = "valid", class = "factor"), 
    verification.signature = structure(1L, .Label = "-----BEGIN PGP SIGNATURE-----\n\nwsBcBAABCAAQBQJgrnUgCRBK7hj4Ov3rIwAAJNMIAD1/pWaW/NYsefSLx5tvcTyl\nfG+Nst5dxAYz1jvZBsiy/zGsrk42EneA391svg6SkW8brf37tNUq3Ob1fXxrknCB\nDctR6X1v281KS9ziFOXMC67HKeqSqWqFD/QaQ3Q2+TDUSdV2Gos6TN6asaBfcwku\nwadow9ZOnzi6tvT7KqWeFD05M8cHnPpTrbPJ8BUjkuf5mQog0xJY40Sev9DFg33P\nux6jhBKJZeN72UxK1K9zs/OvHOLerHoq/pt+mxFnmsf/Kgps2/WX8sE2BLsU6zPg\nePZMyTfLulDXdhoMK6vU6Lj5faiWbLk/xE9zaBKGiRqKALtBsR75YnTal5Gb/qM=\n=bVRa\n-----END PGP SIGNATURE-----\n", class = "factor"), 
    verification.payload = structure(1L, .Label = "tree f1d056b93ce0d060501d5fd6b9e9df2d934059f6\nparent 20a8258b39f5cbda7911cc8c0cdb35a4bb31aa52\nauthor Christophe Dervieux <cderv@rstudio.com> 1622045984 +0200\ncommitter GitHub <noreply@github.com> 1622045984 +0200\n\nclean_duplicates() is now aware of blogdown rendering method (#629)\n\n", class = "factor")), class = "data.frame", row.names = c(NA, 
-1L))

1 个答案:

答案 0 :(得分:5)

我们可以递归地将 NULL 值替换为 NA,然后将 flattened 数据更改为 data.frame

library(rrapply)
library(dplyr)
out2 <- rrapply(test, f = function(x) replace(x, is.null(x), NA), 
        how = 'flatten') %>%
     as.data.frame.list %>%
     type.convert(as.is = TRUE)

检查尺寸

dim(out2)
#[1]  1 15