R-将具有NULL类型的列表转换为数据帧
我有以下清单:
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-----nnwsBcBAABCAAQBQJgrnUgCRBK7hj4Ov3rIwAAJNMIAD1/pWaW/NYsefSLx5tvcTylnfG+Nst5dxAYz1jvZBsiy/zGsrk42EneA391svg6SkW8brf37tNUq3Ob1fXxrknCBnDctR6X1v281KS9ziFOXMC67HKeqSqWqFD/QaQ3Q2+TDUSdV2Gos6TN6asaBfcwkunwadow9ZOnzi6tvT7KqWeFD05M8cHnPpTrbPJ8BUjkuf5mQog0xJY40Sev9DFg33Pnux6jhBKJZeN72UxK1K9zs/OvHOLerHoq/pt+mxFnmsf/Kgps2/WX8sE2BLsU6zPgnePZMyTfLulDXdhoMK6vU6Lj5faiWbLk/xE9zaBKGiRqKALtBsR75YnTal5Gb/qM=n=bVRan-----END PGP SIGNATURE-----n", class = "factor"),
verification.payload = structure(1L, .Label = "tree f1d056b93ce0d060501d5fd6b9e9df2d934059f6nparent 20a8258b39f5cbda7911cc8c0cdb35a4bb31aa52nauthor Christophe Dervieux <cderv@rstudio.com> 1622045984 +0200ncommitter GitHub <noreply@github.com> 1622045984 +0200nnclean_duplicates() is now aware of blogdown rendering method (#629)nn", class = "factor")), class = "data.frame", row.names = c(NA,
-1L))
回答
我们可以将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