-3

I can understand, this may have been answered earlier in some form, but I didn't have any luck with the solutions. Would be great if you could help me with this.

I have two txt files and I want to merge them. The format of the file is as below:

ID|C1|C2|C3

ID1|a|b|c

ID2|d|e|f

Basically, the respective columns in both txt files are separated by "|"

When I am merging the two files using merge, the error message displayed is 'by' must specify a uniquely valid column.

Also please see below the code.

mydata_1 <- read.delim("file_1.txt", skipNul = TRUE, sep = "|", header = FALSE, row.names = NULL)
mydata_2 <- read.delim("file_2.txt", skipNul = TRUE, sep = "|", header = FALSE, row.names = NULL)
merge(x=mydata_1, y=mydata_2, by = "ID", all = TRUE)

Edit - sharing a few lines of actual data for easy reference.

File 1

MS|T1|T2|T3|T4|T5|T6|T7|T8|T9|T10|T11|T12|T13|T14|T15|T16|T17|T18|T19|T20|T21
1|646.25|129.966666666666666666666666666666666667|67.1|19.3|0|281.5|0|227.25|126|0|0|7.5|0|0|10.5|3|0|6|600|NO|2G                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
2|573.24|91.2666666666666666666666666666666666667|23.0833333333333333333333333333333333333|3.3|3.703466796875|205|0|84.75|36|37.49|210|0|0|0|0|0|0|14|640|YES|3G                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
3|955.63|1163.93333333333333333333333333333333333|2.11666666666666666666666666666666666667|5.58333333333333333333333333333333333333|0|248|600|8.25|91.88|0|0|7.5|0|0|0|3|0|21|1050|NO|2G                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
4|906|306.666666666666666666666666666666666667|24.75|7.35|0|663.5|0|123|63|0|0|2.5|0|0|0|1|0|21|1050|NO|2G                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
5|147.6|29.4833333333333333333333333333333333333|21.45|0|0|75.6|0|63|0|0|0|9|0|0|0|3.6|0|3|150|NO|2G

File 2

MS|T1|T2|T3|T4|T5|T6|T7|T8|T9|T10|T11|T12|T13|T14|T15|T16|T17|T18|T19|T20|T21
1|100346.25|129.966666666666666666666666666666666667|167.1|19.3|0|281.5|0|227.25|126|0|0|7.5|0|0|10.5|3|0|6|600|NO|2G                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
2|73.24|91.2666666666666666666666666666666666667|203.0833333333333333333333333333333333333|3.3|3.703466796875|205|0|84.75|36|37.49|210|0|0|0|0|0|0|14|640|YES|3G                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
3|955.63|1163.93333333333333333333333333333333333|2.11666666666666666666666666666666666667|5.58333333333333333333333333333333333333|0|248|600|8.25|91.88|0|0|7.5|0|0|0|3|0|21|1050|NO|2G                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
4|906|306.666666666666666666666666666666666667|24.75|7.35|0|663.5|0|123|63|0|0|2.5|0|0|0|1|0|21|1050|NO|2G                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
clemens
  • 6,653
  • 2
  • 19
  • 31
Punchy
  • 3
  • 4

1 Answers1

1

You have specified that the file does not contain the names of the variables as its first line by providing FALSE to the argument header in read.delim(). As a result, there is no column ID:

text <- "ID|C1|C2|C3
ID1|a|b|c
ID2|d|e|f"

with header = FALSE there is no column ID:

read.delim(text = text, sep = "|", header = FALSE)

   V1 V2 V3 V4
1  ID C1 C2 C3
2 ID1  a  b  c
3 ID2  d  e  f

with header = TRUE you have the correct headers that are in the first row of your files:

read.delim(text = text, sep = "|", header = TRUE)
   ID C1 C2 C3
1 ID1  a  b  c
2 ID2  d  e  f
clemens
  • 6,653
  • 2
  • 19
  • 31
  • I did try using header = TRUE, but it doesn't work with that. – Punchy May 07 '18 at 08:54
  • 1
    It works with the example data you have provided. you might want to check out this: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – clemens May 07 '18 at 08:56
  • I would like to share 5 rows of data of each file so that it's more representative, but the character limit doesn't allow me to - is there a work around? – Punchy May 07 '18 at 09:11
  • I have pasted a few lines of data into the original question - please check – Punchy May 07 '18 at 09:19
  • that's rather easy: both files don't have an `ID` column you might want to change that to `MS` in merge. reading works just fine with `header = TRUE`. – clemens May 07 '18 at 10:02
  • MS vs ID wasn't the issue here - I pasted just these 5 lines of data into a fresh txt file, and it works - seems to be some issue with the base txt file. – Punchy May 07 '18 at 10:32