0
votes

I have a great number of csv files I have made into a list using the "list.files" function:

list.files("C:/Users/gyero/Documents/daphnia/SUMMARY_CHIMP")

I want to use the gather() function on each file and then sink() to produce a separate output for each csv file. I need some way to identify which output file corresponds to which input file.

To loop through files I had a crack at using lapply()

lapply(list.files[1:33], function{gather(WELL,MEASURE,A1:D6)})

I'm not sure how to get a corresponding output file name- so I tried preceding the lapply function with the following code so at least the output files would have a unique name and not get overwritten:

sink('DATA.CSV')
paste0(Sys.time(),'DATA.CSV')

However, I am very new to this and having trawled the internet I still can't realize my goal (the above didn't work). I would be MASSIVELY grateful to anyone who could spare the time to help a desperate noob out.

1

1 Answers

1
votes

You can try :

file_names <- list.files("C:/Users/gyero/Documents/daphnia/SUMMARY_CHIMP", 
                          full.names = TRUE)

lapply(file_names, function(x) {
    df <- read.csv(x)
    write.csv(tidyr::gather(df,WELL,MEASURE,A1:D6), 
              paste0(sub("\\.csv", "", basename(x)), "_output.csv"))
})

If you have files such as "A.csv", "B.csv" in the folder, this will create new files with "A_output.csv", "B_output.csv". Also, note that gather has been retired and replaced with pivot_longer now.