Clean a list of lists from GoogleSheets in R

Manu

I have retrieved a sheet from google sheets and returned me a list of lists:

sheet <- list(var1 = list(Sys.time(), Sys.time(),NULL),
             var2 = list(NULL,1,2),
             var3 = list("a",NULL,"b"))

I have to convert all members of the list that are NULL into NAs. I was kindly explained how to do it list by list using for example sheet$var1[sapply(sheet$var1,is.null)] <- NA, but since I have many variables, is it possible to code it in just one line? Any help will be greatly appreciated.

akrun

It is a nested list. So we can do

library(purrr)
map(sheet, ~ map(.x, ~ replace(.x, is.null(.x), NA)))

Or with base R

lapply(sheet, function(x) lapply(x, function(y) replace(y, is.null(y), NA)))
#$var1
#$var1[[1]]
#[1] "2020-09-13 16:44:56 CDT"

#$var1[[2]]
#[1] "2020-09-13 16:44:56 CDT"

#$var1[[3]]
#[1] NA


#$var2
#$var2[[1]]
#[1] NA

#$var2[[2]]
#[1] 1

#$var2[[3]]
#[1] 2


#$var3
#$var3[[1]]
#[1] "a"

#$var3[[2]]
#[1] NA

#$var3[[3]]
#[1] "b"

Or do this recursively with rrapply

library(rrapply)
rrapply(sheet, f = function(x) replace(x, is.null(x), NA), how = "list")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Importing date columns as lists from googledoc via googlesheets4 in R

When importing from Googlesheets, returns a list I can't convert to date in R

Extracting element from list of lists in R?

Create list of lists from tibble in R (tidyverse)

Extract values from list of lists with R

Create dataframe from list of lists in R

Remove element from list of lists in R

Removing NA from list of lists in R in function

calling an index case from a list of lists R

Clean list from stopwords

In R, extract lists with same name from lists of list

Select subset of lists from list of lists by names() position in R

Create clean data frame in R from list generated by ocr

Clean way to combine list of lists into a dictionary?

Python Clean Specific Elements in List of Lists

From 'list of lists of lists' to 'list of lists' - Python

List of list of lists (from API call) into data frame in R

Clean a list of tuples from the permutations

Removing item from a list once chosen Dropdown Googlesheets Script

Plotting a list of lists in R

merge list of lists in R

How to create new list from nested lists in R

Printing flextables from a list of lists in R-markdown

Pull elements from a list of lists in R using tidy principles

deleting an element from all embedded lists in a list in R

R creating new list from other lists/dataframes

R - iterate to create list from condition based on multiple lists/vectors

imap_dfr to bind_rows from list of lists in R

extract elements from list of lists with variable number of elements and NAs R