将带有列表列的数据框另存为csv文件

Ann*_*Ann 5 r list dplyr tidyverse

我有以下数据框,看起来像这样(3列作为列表)。

A tibble: 14 x 4
                                                    clinic_name drop_in_hours appointment_hours   services
                                                          <chr>        <list>            <list>     <list>
     1                   Birth Control and Sexual Health Centre    <list [1]>        <list [1]> <list [1]>
     2 Black Creek Community Health Centre (Sheridan Mall Site)    <list [1]>        <list [1]> <list [1]>
     3 Black Creek Community Health Centre (Yorkgate mall Site)    <list [1]>        <list [1]> <list [1]>
     4                                         Crossways Clinic    <list [1]>        <list [1]> <list [1]>
     5                                       Hassle Free Clinic    <list [1]>        <list [1]> <list [1]>
     6                          Immigrant Women's Health Center    <list [1]>        <list [1]> <list [1]>
     7                          Rexdale Community Health Center    <list [1]>        <list [1]> <list [1]>
     8                            Rexdale Youth Resource Center    <list [1]>        <list [1]> <list [1]>
     9                         Scarborough Sexual Health Clinic    <list [1]>        <list [1]> <list [1]>
    10                                 Special Treatment Clinic    <list [1]>        <list [1]> <list [1]>
    11                            Taibu Community Health Center    <list [1]>        <list [1]> <list [1]>
    12                                                 The Gate    <list [1]>        <list [1]> <list [1]>
    13                                   The Jane Street Clinic    <list [1]>        <list [1]> <list [1]>
    14                                            The Talk Shop    <list [1]>        <list [1]> <list [1]>
Run Code Online (Sandbox Code Playgroud)

我想将其输出为csv文件。引起我注意的是,数据帧的列不应该是R中的列表。所以我做了一些google,发现它用list-column保存了data.frames,所以我尝试了一下:

library(tidyverse)

df %>% 
  mutate(drop_in_hours = map_chr(drop_in_hours, ~ capture.output(dput(.))),
         appointment_hours = map_chr(appointment_hours, ~ capture.output(dput(.))),
         services = map_chr(services, ~ capture.output(dput(.)))     ) %>% 
  write_csv("health.csv")
Run Code Online (Sandbox Code Playgroud)

但是我出错了,我在这里错过了什么吗?

Error in mutate_impl(.data, dots) : 
  Evaluation error: Result 4 is not a length 1 atomic vector
Run Code Online (Sandbox Code Playgroud)

sbh*_*bha 14

这是另一个可能更简单的选项。

根据数据的不同,逗号分隔的值可能会变得复杂,因此我使用栏|来分隔列表列中的值:

library(tidyverse)

starwars %>% 
  rowwise() %>% 
  mutate_if(is.list, ~paste(unlist(.), collapse = '|')) %>% 
  write.csv('df_starwars.csv', row.names = FALSE)
Run Code Online (Sandbox Code Playgroud)

starwars是示例数据帧之一dplyr


Cyb*_*tic 7

创建一个包含列表列的标题:

library(tibble)

clinic_name <- c('bobo center', 'yoyo plaza', 'lolo market')
drop_in_hours <- list(c("Monday: 2 pm - 5 pm", "Tuesday: 4 pm - 7 pm")) 
appointment_hours <- list(c("Monday: 1 pm - 2 pm", "Tuesday: 2 pm - 3 pm")) 
services <- list(c("skin graft", "chicken heart replacement"))

tibb <- data_frame(clinic_name, drop_in_hours, appointment_hours, services)

print(tibb)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

编写将所有列表列转换为字符类型的通用函数:

set_lists_to_chars <- function(x) {
    if(class(x) == 'list') {
    y <- paste(unlist(x[1]), sep='', collapse=', ')
    } else {
    y <- x 
    }
    return(y)
}
Run Code Online (Sandbox Code Playgroud)

将函数应用于带有列表列的小标题:

new_frame <- data.frame(lapply(tibb, set_lists_to_chars), stringsAsFactors = F)

new_frame
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

将新格式化的数据帧写入csv文件:

write.csv(new_frame, file='Desktop/clinics.csv')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这是一个csv文件,列表列扩展为常规字符串。

这是一个无所不包的功能。只需传递您的小标题和文件名即可:

tibble_with_lists_to_csv <- function(tibble_object, file_path_name) {
    set_lists_to_chars <- function(x) { 
        if(class(x) == 'list') { y <- paste(unlist(x[1]), sep='', collapse=', ') } else { y <- x  } 
        return(y) }
    new_frame <- data.frame(lapply(tibble_object, set_lists_to_chars), stringsAsFactors = F)
    write.csv(new_frame, file=file_path_name)
}
Run Code Online (Sandbox Code Playgroud)

用法:

tibble_with_lists_to_csv(tibb, '~/Desktop/tibb.csv')
Run Code Online (Sandbox Code Playgroud)