2

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

}
camille
  • 16,432
  • 18
  • 38
  • 60
  • 1
    Why index `fd` at all? You're writing out the file at each iteration, so you hardly need to keep it in memory. – jbowman Feb 25 '19 at 15:26

2 Answers2

2

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")))
dylanjm
  • 2,011
  • 9
  • 21
0

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

}
eylemyap
  • 189
  • 2
  • 2
  • 10