将包含NULL类型的列表转换为数据框

4

我有以下列表:

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或空字符串。我正在批量处理,因此可能会有其他值为空。

普通更改无效:

> 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个回答

5
我们可以递归地将NULL值替换为NA,然后将flatten的数据转换为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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接