4

I have the below list:

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))

I need to convert this into a dataframe, the problem is that I have two elements that are NULL. I need to keep the columns, and switch the content to NA or to an empty string. I am batch processing, so I may have other values null.

A regular change doesn't work:

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

Expected Output:

The below is another list that I was able to transform properly. As you can see, this one doesn't have missing values, but I am adding it here so you can see what I need (this is a dput of the transformation):

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 Answer 1

5

We could replace the NULL values to NA recursively and then change the flattened data to 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)

checking the dimensions

dim(out2)
#[1]  1 15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.