R/modify.list.R

Defines functions modify.list

#' List object formatter
#'
#' @description Format ElasticSearch nested list object
#' @param df The name of the nested dataframe you want to manipulate
#' @return datatframe, tibble
#' @keywords ElasticSearch
#' @import dplyr, purrr
#' @export
#' @examples modify.list(df = df$list.object)

modify.list <- function(df) {

  df <- df %>%
    purrr::transpose() %>%
    dplyr::as_tibble() %>%
    purrr::modify(~replace(.x,lengths(.x)==0,list(NA))) %>% # replace empty elements by list(NA)
    purrr::modify_if(~all(lengths(.x)==1),unlist)           # unlist lists that contain only items of length 1

  return(df)
}
europace-privatkredit/tidyPkuData documentation built on Oct. 2, 2019, 3 a.m.