0

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

  • exactly what is it you want set to NULL? – Sirius Mar 27 '21 at 11:44
  • Does this answer your question? [read data into R, using read\_cvs()](https://stackoverflow.com/questions/66820666/read-data-into-r-using-read-cvs) – Dave2e Mar 27 '21 at 13:41
  • @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 –  Mar 27 '21 at 14:49
  • 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 Mar 27 '21 at 16:03
  • @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*](https://www.burns-stat.com/pages/Tutor/R_inferno.pdf), e.g., in particular [Circle 2 *Growing Objects*](https://www.burns-stat.com/pages/Tutor/R_inferno.pdf#page=13) and [Circle 3 *Failing to Vectorize*](https://www.burns-stat.com/pages/Tutor/R_inferno.pdf#page=18). – Uwe Mar 27 '21 at 20:08
  • @Uwe I'm just wondering how if I was to do it this way how it would work –  Mar 28 '21 at 08:56
  • Well, see below for that now. It does what you say, but has a well deserved down vote. – Sirius Mar 29 '21 at 08:34

2 Answers2

0

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)
Guillaume
  • 606
  • 3
  • 13
-1

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.

Sirius
  • 5,224
  • 2
  • 14
  • 21