Programmers learn & share
0 votes
156 views

Problem :

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

error in fix.by(by.x, 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 :

library(zoo)

dfList <- lapply(c(1:100), function(i) {
   df <- read.table(paste0("rundata  ", i), sep= " ", col.names=c("tm","score","current"))  
   df <- df[!is.na(df$tm),]
   df$score <- zoo::na.locf(df$score)
   colnames(df) <- paste0(colnames(df), i)
   return(df)
})
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
59 comments
241 users