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")
The merge argument only takes two values as input, so you have to do them separately:
newdata<- merge(data1[,1:2],data2[,1:2],by=1,all=TRUE)
newdata<- merge(newdata [,1:3 ],data3 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:4 ],data4 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:5 ],data5 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:6 ],data6 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:7 ],data7 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:8 ],data8 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:9 ],data9 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:10 ],data10 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:11 ],data11 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:12 ],data12 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:13 ],data13 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:14 ],data14 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:15 ],data15 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:16 ],data16 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:17 ],data17 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:18 ],data18 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:19 ],data19 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:20 ],data20 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:21 ],data21 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:22 ],data22 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:23 ],data23 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:24 ],data24 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:25 ],data25 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:26 ],data26 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:27 ],data27 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:28 ],data28 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:29 ],data29 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:30 ],data30 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:31 ],data31 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:32 ],data32 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:33 ],data33 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:34 ],data34 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:35 ],data35 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:36 ],data36 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:37 ],data37 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:38 ],data38 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:39 ],data39 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:40 ],data40 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:41 ],data41 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:42 ],data42 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:43 ],data43 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:44 ],data44 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:45 ],data45 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:46 ],data46 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:47 ],data47 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:48 ],data48 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:49 ],data49 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:50 ],data50 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:51 ],data51 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:52 ],data52 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:53 ],data53 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:54 ],data54 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:55 ],data55 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:56 ],data56 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:57 ],data57 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:58 ],data58 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:59 ],data59 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:60 ],data60 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:61 ],data61 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:62 ],data62 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:63 ],data63 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:64 ],data64 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:65 ],data65 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:66 ],data66 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:67 ],data67 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:68 ],data68 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:69 ],data69 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:70 ],data70 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:71 ],data71 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:72 ],data72 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:73 ],data73 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:74 ],data74 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:75 ],data75 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:76 ],data76 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:77 ],data77 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:78 ],data78 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:79 ],data79 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:80 ],data80 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:81 ],data81 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:82 ],data82 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:83 ],data83 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:84 ],data84 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:85 ],data85 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:86 ],data86 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:87 ],data87 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:88 ],data88 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:89 ],data89 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:90 ],data90 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:91 ],data91 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:92 ],data92 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:93 ],data93 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:94 ],data94 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:95 ],data95 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:96 ],data96 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:97 ],data97 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:98 ],data98 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:99 ],data99 [,1:2],by=1,all=TRUE )
newdata<- merge(newdata [,1:100 ],data100 [,1:2],by=1,all=TRUE )