0

I read multiple csv files and get a list of 11 data frames. Now I want to use each data frame in the list to get a new table and save it to a csv file with the origin name separately.

I read csv file by

path <- "D:/DATA/01/processdata/singlecity"
fileNames = list.files(path, pattern="*.csv$")
filePath <- sapply(fileNames, function(x){ 
paste(path,x,sep='/')}) 
dfs <- lapply(filePath, function(x){
read.csv(x, header=T)}) 

then get a list of dataframe like :

EU CHINA USA 
for (i in data){
   y <- data.table(i)
   y <- dplyr::filter(y, grepl('2002|2003|2004|2005|2006', V2))
   write.csv(y, "y.csv')
}

what I want to output is that:

EU_t1.csv
CHINA_t2.csv
USA_t3.csv

I don't know how to use write.csv to return different files based on origin data frame name.

5
  • I edit data frame names for different names Commented Nov 21, 2020 at 9:06
  • Do you have only these dataframes in the environment? Or there are other dataframes as well that you don't want to write? Commented Nov 21, 2020 at 9:10
  • I have other dataframes in the environment. But these dataframes already in a list since I read multiple cvs files at same time as a list. Also, I can only have these dataframes if needed. Commented Nov 21, 2020 at 9:15
  • data is that list with all the dataframes? What does names(data) return? Commented Nov 21, 2020 at 9:19
  • I edit the way to read csv files in main question. For names(data) , return EU.csv CHINA.csv US.csv Japan.csv UK.csv Commented Nov 21, 2020 at 9:25

2 Answers 2

2

You can try the following :

path <- "D:/DATA/01/processdata/singlecity"
fileNames = list.files(path, pattern="*.csv$", full.names = TRUE)

lapply(seq_along(fileNames), function(x){
  file <- fileNames[x]
  data <- subset(read.csv(file), grepl('2002|2003|2004|2005|2006', V2))
  write.csv(data, sprintf('D:/DATA/01/processdata/singlecity/T1/%s_t%d.csv', 
            tools::file_path_sans_ext(basename(file)), x), row.names = FALSE)
}) 
Sign up to request clarification or add additional context in comments.

3 Comments

How to deal with different data frame names, for example A, B ,C, how to export A_t.csv, B_t.csv, C_t.csv, also how to write into a new path
Thank you, it works, great! Also, how to save these new files to a different path rather same path? For example:new_path <- "D:/DATA/01/processdata/singlecity/T1"
You can add the path in sprintf directly. See updated answer
0

Here's a solution modifying the for loop you've shared.

dfs <- list(dataframe1, dataframe2, dataframe3)

for (i in seq_along(dfs)) {
   y <- data.table(dfs[[i]])
   y <- dplyr::filter(y, grepl('2002|2003|2004|2005|2006', V2))
   write.csv(y, paste0("datafram_t", i, ".csv"))
}

This will create files with the names you've requested:

datafram_t1.csv
datafram_t2.csv
datafram_t3.csv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.