Programmers learn & share
0 votes

Problem :

I am trying to merge data in R but facing following error

error in, x) : 'by' must specify one or more columns as numbers, names or logical

by (6.9k points)   | 156 views

1 Answer

0 votes

Solution :

If you need to merge many dataframes then please consider binding individual dataframes into a list with the lapply() after that you can run any needed calculations after that run in a Reduce(..., merge) to merge all the files of list into one the wide dataframe. Below is the correct method which processes and merges 100 files :


dfList <- lapply(c(1:100), function(i) {
   df <- read.table(paste0("rundata  ", i), sep= " ", col.names=c("tm","score","current"))  
   df <- df[!$tm),]
   df$score <- zoo::na.locf(df$score)
   colnames(df) <- paste0(colnames(df), i)
newdata <- Reduce(function(...) merge(..., by=1, all=T), dfList)

write.table(newdata, "outputR
by (36.1k points)  
edited by
2,204 questions
2,604 answers
241 users