R-将空类型的列表转换为数据帧
我有以下清单:R-将空类型的列表转换为数据帧,r,list,dataframe,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-
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))
预期输出: 下面是我能够正确转换的另一个列表。正如您所看到的,这一个没有缺少的值,但我在这里添加它,以便您可以看到我需要什么(这是转换的dput): 结构(列表(author.name=structure(1L,.Label=“Christophe Dervieux”,class=“factor”), author.email=结构(1L,.Label=“盲”,class=“factor”), author.date=结构(1L,.Label=“2021-05-26T16:19:44Z”,class=“factor”), committer.name=结构(1L,.Label=“GitHub”,class=“factor”), committer.email=结构(1L,.Label=”noreply@github.com“,class=“factor”), 提交人日期=结构(1L,.Label=“2021-05-26T16:19:44Z”,class=“factor”), message=structure(1L,.Label=“clean_duplicates()现在知道blogdown呈现方法(#629)”,class=“factor”), tree.sha=结构(1L,.Label=“f1d056b93ce0d06051d5fd6b9e9df2d934059f6”,class=“factor”), tree.url=结构(1L,.Label=”https://api.github.com/repos/rstudio/blogdown/git/trees/f1d056b93ce0d060501d5fd6b9e9df2d934059f6“,class=“factor”), url=结构(1L,.Label=”https://api.github.com/repos/rstudio/blogdown/git/commits/00a20903f0b2953f8f350d69bffdcd9c50cda5b1“,class=“factor”), 注释计数=0L,verification.verified=TRUE,verification.reason=structure(1L,.Label=“valid”,class=“factor”), verification.signature=结构(1L,.标签="-----2)美国,纽约市,纽约市,纽约市,纽约市,纽约市,纽约市,纽约市,纽约市,纽约市,纽约市,纽约市,纽约市,纽约市,vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvB/WX8sE2BLsU6zPg\nePZMyTfLulDXdhoMK6vU6Lj5faiWbLk/xE9zaBKGiRqKALtBsR75YnTal5Gb/qM=\n=bVRa\n------结束PGP签名------\n“,class=“factor”), verification.payload=structure(1L,.Label=“tree f1d056b93ce0d060501d5fd6b9e9df2d934059f6\nPart 20A58B39F5CBDA7911C8C0CDB35A4BB31AA52\nauthor Christophe Dervieux 1622045984+0200\ncommitter GitHub 1622045984+0200\n\n\nclean\n重复项()现在知道blogdown呈现方法(#629)\n\n\n“,class=“factor”)、class=“data.frame,row.names=c(NA, -1L)
我们可以递归地将
NULL
值替换为NA
,然后将flatten
ed数据更改为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)
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))
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