0

I tried joining csv files present in two directories in R, having same name. For example 1.csv and 2.csv are present in /path/directory1 and /path/directory2. I joined the two files(1.csv with 1.csv). As given in below code:

directories <- c("/path/directory1", "/path/directory2")
files <- lapply(directories, list.files, pattern="*.csv", full.names = TRUE)
files <- lapply(files, sort)
joined <- Map(function(x,y) { join(read.csv(x), read.csv(y), by=c("date")) },
           files[[1]], files[[2]])

Now, "joined" contains the list of csv’s. Now I want to store all the csv’s in directory (say “path/directory3”) but the csv name should be based on column For example, Every csv contains information about each country. There is “country” column: values are Australia, New Zealand, India, United States.

So, csv name should be Australia.csv, New Zealand.csv and so on.

Please help! Thanks in Advance!!

Kaushik
  • 173
  • 2
  • 11
  • I guess you'd be better of with some unix based solution, like `awk '{print $0 >> $1.tsv}'`, [docs here](https://www.gnu.org/software/gawk/manual/html_node/Redirection.html). – liborm Aug 11 '17 at 14:10
  • Yes correct, but I want to do this in R. – Kaushik Aug 11 '17 at 14:24

1 Answers1

0

You can try something like:

lapply(joined, function(x) write.csv(x, file = paste0(x[["country"]][1], ".csv")))

Obviously, add the relevant arguments you're interested in for write.csv as required.

To get the file name, we select just the "country" column (x[["country"]]) and then just the first value in that column ([1]) since we only need one value to create a file name.

A5C1D2H2I1M1N2O1R2T1
  • 177,446
  • 27
  • 370
  • 450
  • Thanks!!! one more query, if we want to remove same columns from the list of data frames. And then write it to the csv's. Could you tell this also? – Kaushik Aug 11 '17 at 14:51
  • @Kaushik, do they all have the same name? If so, instead of `write.csv(x,...)`, you could use one of the approaches [at this answer](https://stackoverflow.com/questions/4605206/drop-data-frame-columns-by-name) to subset `x` before writing it to a csv file. – A5C1D2H2I1M1N2O1R2T1 Aug 11 '17 at 14:54