Data Analyst
Data Analyst

Reputation:

How to loop through CSV files in directory and output them as RDS files

This is the code I have for now, I keep getting an error that fd is undefined, I tried defining it as fd=data.frame() but it doesn't work.

Code:

file<-list.files(pattern=".csv$")  
#file creates a list of csv file names 

for (i in seq_along(filenames))

{
 fd[i]<- read.csv(file[i]) 

#read each csv file
output=c("o1.RDS","o2.RDS","o3.RDS")

#save each csv file as RDS every iteration, 
#with the name as specified in the vector output.

saveRDS(fd[i],file =output[i])     

}

Upvotes: 2

Views: 321

Answers (2)

eylemyap
eylemyap

Reputation: 309

Have you tried defining fd as a list?

fd <- list()

Also in the example above you have a mistake. It should be "filenames" instead of "file".

Here is the result, which worked for me:

fd <- list()

file <- list.files(pattern=".csv$")  
#file creates a list of csv file names 

for (i in seq_along(file))

{
        fd[i]<- read.csv(file[i]) 

        #read each csv file
        output = c("o1.RDS","o2.RDS","o3.RDS")

        #save each csv file as RDS every iteration, 
        #with the name as specified in the vector output.

        saveRDS(fd[i], file = output[i])     

}

Upvotes: 0

dylanjm
dylanjm

Reputation: 2101

You can do something like this, although its untested because I don't have a folder of .csv files at the moment:

library(tidyverse)

files <- list.files("./", pattern = ".csv")

map(files, ~read_csv(.x) %>% 
      write_rds(path = paste0("YOUR/PATH/HERE", basename(.x), ".rds")))

Upvotes: 2

Related Questions