Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a data frame with list-columns as csv file

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

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]>

I wanted to output it to as csv file. It has come to my attention that columns of data frames shouldn't be lists in R. So I did some google and found this save data.frames with list-column so I tried it out :

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")

But I got an error , am I missing something here?

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

.

like image 250
Ann Avatar asked Dec 29 '17 14:12

Ann


People also ask

How do I save a DataFrame to a CSV file?

The Pandas to_csv() function is used to convert the DataFrame into CSV data. To write the CSV data into a file, we can simply pass a file object to the function. Otherwise, the CSV data is returned in a string format.

How do I convert a list into a CSV file in Python?

To convert a list of lists to csv in python, we can use the csv. writer() method along with the csv. writerow() method.

How do I convert a DataFrame to a CSV file in Python?

By using pandas. DataFrame. to_csv() method you can write/save/export a pandas DataFrame to CSV File. By default to_csv() method export DataFrame to a CSV file with comma delimiter and row index as the first column.

Can you export data frames from R?

R base functions provide a write. csv() to export the DataFrame to a CSV file. By default, the exported CSV file contains headers, row index, missing data as NA values, and columns separated by comma delimiter.


2 Answers

Here's another option that may be a little simpler.

Depending on the data, comma separated values could get complicated, so I'm using a bar | for separating values in list columns:

library(tidyverse)

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

starwars is one of the dplyr sample dataframes.

like image 142
sbha Avatar answered Oct 20 '22 13:10

sbha


Create a tibble containing list columns:

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)

enter image description here

Write a general-purpose function that converts any list columns to character type:

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

Apply function to tibble with list columns:

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

new_frame

enter image description here

Write newly formatted dataframe as csv file:

write.csv(new_frame, file='Desktop/clinics.csv')

enter image description here

This is a csv file with the list columns expanded as regular strings.

Here is an all-encompassing function. Just pass in your tibble and a filename:

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)
}

Usage:

tibble_with_lists_to_csv(tibb, '~/Desktop/tibb.csv')
like image 20
Cybernetic Avatar answered Oct 20 '22 15:10

Cybernetic