I have a bunch of csv files that need to be merged into one file. They all have the same first column and column B is unique to each file. The other issue I have is that they don't have column headers. I am trying to give column B the name of the file so when they are merged, I can tell which time point each column is. Currently, I have files that look like this:
| 399 | 0.1887 |
| 400 | 0.1298 |
| 401 | -0.1249 |
Ultimately, I want my file to be like this:
| Wavelength | 137.5uU 0 | 137.5uU 1 | 137.5uU 2 |
| 399 | 0.1887 | 0.1489 | 0.2676 |
| 400 | 0.1298 | 0.2988 | -0.0298 |
| 401 | -0.1249 | -0.1397 | 0.1293 |
I was able to make a file with the second column of data for all my files using this code:
csv_files <- list.files(path="C:")
abs <- do.call(cbind, lapply(csv_files, function(x) {
setNames(data.frame(read.csv(x)[[2]]), tools::file_path_sans_ext(basename(x)))
}))
I was then writing the new file into a folder with my T0 file where I would merge the two files with this code:
df <- list.files(path="C:/", full.names = TRUE) %>%
lapply(read_csv) %>%
bind_cols()
The problem I am running into here is that the first code is overwriting the first row of data for the headers so the # of rows in my T0 and my abs files are not aligned. Also, the first code is making a column that numbers the rows and will be imported as data in the next file.
I did also try the solutions in this thread and could not get them to work.
How to combine CSV files in R and add the file name as a column header