Save a data frame with list-columns as csv file

前端 未结 5 759
清歌不尽
清歌不尽 2021-02-09 06:01

I have the following data frame that looks like this (3 columns as list).

A tibble: 14 x 4
                                                    clinic_name drop_         


        
5条回答
  •  隐瞒了意图╮
    2021-02-09 06:41

    exploratory::list_to_text() will convert a list column to a character column. The default is sep = ", ", which I recommend changing to something else if writing to a .csv.

    devtools::install_github("exploratory-io/exploratory_func")

    list_to_text <- function(column, sep = ", "){
      loadNamespace("stringr")
      ret <- sapply(column, function(x) {
        ret <- stringr::str_c(x, collapse = sep)
        if(identical(ret, character(0))){
          # if it's character(0)
          NA
        } else {
          ret
        }
      })
      as.character(ret)
    }
    

    https://github.com/exploratory-io/exploratory_func/blob/master/LICENSE.md

提交回复
热议问题