• Register
0 votes
1.1k 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 5 3
6,930 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
 
 
9 7 4
38,600 points

Related questions

1 vote
1 answer 2K views
2K 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 6 views
6 views
Problem: Hi, any idea that what should I do to fix it .. How to merge two columns in r?
asked Apr 3 Ifra 24.4k points
0 votes
1 answer 5 views
5 views
Problem: Can someone tell me how to avoid warning when introducing NAs by coercion
asked Mar 15 ummesalma 24.5k points
0 votes
1 answer 3 views
3 views
Problem: I want to know how to omit NA values in a data frame, but only in some columns I am interested in.
asked Mar 27 ummesalma 24.5k points
1 vote
1 answer 6 views
6 views
Problem: I&rsquo;m new in data frame. I want to know how to omit NA values in a data frame, but only in some columns I am interested in. For example, DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22)) but I only want to omit the data ... the result should be x y z 1 1 0 NA 2 2 10 33 na.omit seems delete all rows contain any NA. Can somebody help me out of this simple question?
asked Mar 10 Shovo210 13.3k points
0 votes
1 answer 4 views
4 views
Problem: Any solution for this error: Error in aggregate.data.frame(as.data.frame(x), ...) : arguments must have same length
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
2 answers 1.8K views
1.8K views
Problem : I am facing below error and I am unable to understand the meaning of the error. All my variable names are correct and there are no typing errors. Does anybody faced this issue before any solution on it? My erroneous code : datNewagg <- aggregate (dataNew, by = ... mean) It gives following error : Error in aggregate.data.frame(datNew, by = list("x", "y", : arguments must have same length
asked Dec 6, 2019 alecxe 7.5k points
0 votes
1 answer 3 views
3 views
Problem: i don&rsquo;t understand this problem. please help me? names' attribute [2] must be the same length as the vector [1]
asked 2 days ago Rohit kr 15.5k points
0 votes
1 answer 1.3K views
1.3K views
Problem : I have about 25 dataframes which are essentially the frequency counts of my elements stored in the column called Elements. They all have some of the different and some of the the same elements in my Elements columns so they just look like below : dataframe called ... on them but facing below error: Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match .
asked Dec 24, 2019 alecxe 7.5k points
0 votes
1 answer 14 views
14 views
Problem: Urgent problem, any help: Error: the following untracked working tree files would be overwritten by merge:
asked Mar 13 Wafa Abu Yousef 6.1k points