I have an R code which takes a set of .csv files and put all of them together.
files <- list.files()
for(i in 1:length(files)) assign(files[i], read.csv(files[i],header=F,stringsAsFactors=F,na.strings=c("", "NA")))
descript <- get(files[1])[1,][!is.na(get(files[1])[1,])]
variables <- get(files[1])[3,][!is.na(get(files[1])[3,])]
colNum <- length(variables)+length(descript)
## initialize dataframe
d <- get(files[1])
d <- d[-c(1:3),]
colnames(d) <- variables
jd <- data.frame(get(files[1])[2,])
colnames(jd) <- descript
d[,descript] <- jd[1,descript]
# ## combine all
for(i in 2:length(files)){
j <- get(files[i])
j <- j[-c(1:3),]
colnames(j) <- variables
jd <- data.frame(get(files[i])[2,])
colnames(jd) <- descript
j[,descript] <- jd[1,descript]
d <- rbind(d,j, fill = TRUE)
}
The structure of each .csv file is as follows: first row represents the name of some descriptive information about the session (e.g., recording date, etc). Second row: the values in each of these variables. The third row is empty. Fourth row, the name of the different variables of the experiment (e.g., condition, response, etc). From row 5 to the end, I have the values for each of these variables. My code works well, when the different .csv files have the same number of variables in the descriptive information part (first and second rows) or in the experiment variables (from row 4 to the end). The problem is that some files have different number of variables either in the descriptive or in the experimental part.
As you can see in my code, I have tried using `rbind(d,j, fill = TRUE), but I still get this error in the for loop part:
Error in names(x) <- value :
'names' attribute [45] must be the same length as the vector [28]