0
votes

I have multiple csv files to read into R and I want to do it in the following way:

read data into R, using read_csv(), starting with list_files() creating a vector with full names of files, then set a tibble to NULL & loop over the entries in the vector I created using list_files, each time reading a file and attaching, with bind_rows(), the resulting tibble to the tibble set to NULL containing the data already read.

set the tibble to NULL before starting the loop, so that it can be added to each tibble that is read wihtin the loop.

I need to do this using this exact method, not a quicker way even if there is one

2
exactly what is it you want set to NULL? - Sirius
Does this answer your question? read data into R, using read_cvs() - Dave2e
@Sirius i want to set a tibble to NULL so it does not contain anything. Say x <- NULL then c(x, 1, 2, 3) creates a vector by linking x and the numbers 1, 2, 3. But since x is NULL i'll just have the vector 1, 2, 3 - user15487873
you mean to initialize something that you will then pile data onto? well you don't really need it to achieve what you do. did you consider my proposed answer below? bind_rows doesn't really need something initialized to NULL - Sirius
@Liz, I am puzzled who advised you to use exactly this method because it is considered bad practice. See Patrick Burn's The R Inferno, e.g., in particular Circle 2 Growing Objects and Circle 3 Failing to Vectorize. - Uwe

2 Answers

0
votes

A solution where we replace for loops and lapply with purrr map_df.

library(dplyr)
library(purrr)
all_data <- list.files( "some/Where", full.names=TRUE pattern="\\.csv$" ) %>% 
  purrr::map_df(read.csv)
-1
votes

If you insist on rbind'ing stepwise to a variable that you initialize to NULL, then see below:


library(dplyr)

all.data <- NULL

my.files <- list.files( "some/Where", full.names=TRUE pattern="\\.csv$" )

for( csv.file in my.files ) {
    this.data <- read.csv( csv.file )
    all.data <- rbind( all.data, this.data )
}

There goes. I shudder.