I am trying to merge data in R as suggested in an answer to my other post here. Yet, I have an error.
First let me explain what I try to do. I have
Google returns this as the first listing for the R error, Error in fix.by(by.x, x) and I found it interesting no answer attempted a loop solution of the OP's very long command listing.
For future readers needing to merge many dataframes, consider binding individual dataframes into a list with lapply()
, run any needed calculations, and then run a Reduce(..., merge)
to merge all files of list into one wide dataframe. Below processes and merges all 100 files of original posting:
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")