Hi All,
As a coding newbie I am struggling to combine 70 csv files into one. I also need the new file to include an additional (first) column to indicate which original csv file the respective rows came from (i.e. participant number).
I've tried the following, but I think it doesn't work because the original file names don't have subject numbers (i.e. only 4 columns), and I'm not sure how to add these to multiple files...
# needed for reading data
filenames <- list.files("CSV Raw Data", pattern = "stim", full.names = TRUE)
# needed for identifying subject numbers
subjects <- list.files("CSV Raw Data", pattern = "stim")
# this next line isn't used, but will be useful for combining the other data types, which don't contain subNum as a column.
testdata <- NULL
for (subj in 1:length(subjects)) {
# extract participant number from file name
p <- substr(subjects[subj],7,9)
# read the data from csv
psData <- read_csv(filenames[subj], col_types = cols(), col_names = FALSE)
# combine data array with existing data
testdata <- rbind(testdata, psData)
}
colnames(testdata)[1:5] <- c("subj","time","x","y","trial")
write_csv(testdata, "StimAllPs_csv.csv")
This gives me the following error message: "Error in attr(x, "names") <- as.character(value) :
'names' attribute [5] must be the same length as the vector [4]"
Any help would be greatly appreciated!
Thanks,
Nushuhr