• Register
0 votes
1.5k 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

6.9k points

Please log in or register to answer this question.

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
 
 
38.6k points
edited by

Related questions

1 vote
1 answer 2.6k views
2.6k views
Problem : I am very new to R programming. Currently I am learning R programming. I have below two tables I have asembled them via cbind(). My first Table table_alpha is as below: x1 col1_x1 col2_x1 A "1999 12 01 00:00" " 0.4" " 0" B "1999 12 02 00:00" " - ... = table_num[,1], fill=-9999) Please find below the error: &ldquo;Error in fix.by(by.x, x) : 'by' must specify uniquely valid columns&rdquo;.
asked Mar 28, 2020 morrisBson 3.2k points
0 votes
1 answer 12 views
12 views
Problem: By must specify a uniquely valid column.
asked Jul 21 Rada Gracelynn 104k points
0 votes
1 answer 7 views
7 views
Problem: By' must specify a uniquely valid column.
asked Jul 18 Priscilla Gurpreet 74.7k points
0 votes
1 answer 11 views
11 views
Problem: Partial date of birth (format y-mm-dd)
asked Jul 23 asha 121k points
0 votes
1 answer 9 views
9 views
Problem: R merge two data frames by common columns.
asked Jul 17 Isac Christiaan 103k points
0 votes
1 answer 13 views
13 views
Problem: Gpar' element 'fill' must not be length 0?
asked Jul 15 Nahil 128k points
0 votes
1 answer 99 views
99 views
Problem: Can someone tell me how to avoid warning when introducing NAs by coercion
asked Mar 15 ummesalma 29.1k points
0 votes
1 answer 35 views
35 views
Problem: Hi, any idea that what should I do to fix it .. How to merge two columns in r?
asked Apr 3 Ifra 43.4k points
0 votes
1 answer 2 views
2 views
Problem: \x90[\x90&hellip;&fnof;`&fnof;g (run girls run) - &rsquo;&plusmn;&fnof;m&permil;&oslash;
asked Jul 19 asha 121k points
0 votes
1 answer 8 views
8 views
Problem: R replace na with value from another column.
asked Jul 11 Aurelia Maja 78.4k points