Search code examples
rlist

Transfer dataframe to list and not include NA value


How to transfer dataframe to list and not include NA value ? Thanks!

There is dataframe ori_df

library(tidyverse)
ori_df <- data.frame(category=c('a','b',NA),subcategory=c('w',NA,'Z'))

Below code can transfer ori_df to list fin_list,but the result include NA

fin_list <- as.list(ori_df)

How to delete NA value in fin_list , the wished result as below fin_list_wished ?

fin_list_wished <- list(category=c('a','b'),subcategory=c('w','Z'))

Solution

  • Same idea but with purrr:

    library(purrr) 
    my_purrr_list <- map(as.list(my_df), discard, is.na)
    
    my_purrr_list
    #> $category
    #> [1] "a" "b"
    #> 
    #> $subcategory
    #> [1] "w" "Z"
    

    Or without map/apply:

    library(tidyverse) # tidyr, dplyr
    library(magrittr)  # %$% pipe
    
    my_dplyr_list <- my_df %>% 
      pivot_longer(everything()) %>% # columns must be the same type
      filter(!is.na(value)) %$% 
      split(value, .$name)
            
    my_dplyr_list
    #> $category
    #> [1] "a" "b"
    #> 
    #> $subcategory
    #> [1] "w" "Z"
    

    Toy data:

    my_df <- data.frame(category = c('a', 'b', NA), subcategory = c('w', NA, 'Z'))
    

    Created on 2024-07-19 with reprex v2.1.0