I have a problem very similar to this one, but slightly more involved.
I have a heap of csv files that each record 50 observations for a particular replicate of a particular variety.
The files are named Genotype_Rep.csv, and I have been able to figure out how to extract “Genotype” and “Rep” from the filename, thanks to the solution I mentioned above.
However because each csv file has 50 records, I need to add the Genotype to every line, which doesn’t work in the above solution.
Example:
#Assume that the names of the files in the wd has been assigned to 'filenames'.
#Here's a dummy version:
filenames <- c("A_1.csv", "A_2.csv", "B_1.csv", "B_2.csv")
# extract ID from filename
ids <- gsub("([A-Z])_[0-9].csv", "\\1", filenames)
import <- mdply(filenames, read.csv)
import$ID <- IDs[import$Var1]
import$Var1 <- NULL
This works really nicely when each file has one observation, but not when I need to add it to several lines. I’ve no doubt it’s very simple, but if someone could help me out, that would be great.
when I test mdply() for reading multiple data.frames from files, the column that contains the file indices is “X1”, not “Var1”. So try replacing
with
(I also figured you meant to use “ids”, not “IDs”.)