简体   繁体   中英

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

}

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM