0

This is the first time I post here and I appreciate your help. I have two different large text files: First# a.txt has one column like:

rs151511
rs4156465
rs15651
chr1:1545455

Second one b.txt has three columns with large number of rows:

rs151511    45615614  1
rs4156465   51156155  2
rs15651     15615615  3
rs151655    45615614  1
rs156161    51156155  2
rs122242    15615615  3

I need to write a code which produces a new file "c.txt" which has file#1 list with identical information from file#2 with the below format:

rs151511       45615614  1
rs4156465      51156155  2
rs15651        15615615  3
chr1:1545455   1545455   1

I appreciate your help.

alistaire
  • 42,459
  • 4
  • 77
  • 117
forever
  • 139
  • 1
  • 2
  • 8
  • 2
    Possible duplicate of [How to join (merge) data frames (inner, outer, left, right)?](http://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – alistaire Nov 11 '16 at 00:08

1 Answers1

0

If they are both dataframes or matrices in R, the following should work.

final <- b[which(b[, 1] %in% a[, 1]), ]
write.table(final, "c:/c.txt", sep="\t")

If you have a vector, you should be able to switch the a[, 1] with the vector a. There is a readlines function for reading in difficult txt files that you may want to look at.

Sam Ulloa
  • 186
  • 2
  • 12
  • Thank you very much for your answer. I just have the last line in a.txt which is chr1:1545455. I want to add it to c.txt as chr1:1545455 ch1 1545455. I do not know how to split ch1 from number and add it to the file. I appreciate your help – forever Nov 11 '16 at 02:48
  • If it is only that line and only once: – Sam Ulloa Nov 11 '16 at 07:41
  • rbind(final, c('chr1:1545455', 1545455, 1) – Sam Ulloa Nov 11 '16 at 07:42
  • Excuse me: Whey there is " within the file elements? such as "V1" "V2" "V3" "7160" "chr1" 785910 "rs12565286" "10570" "chr1" 849670 "rs2905062" "42158" "chr1" 1205055 "rs1815606" "92869" "chr1" 1774697 "rs6603811" – forever Nov 11 '16 at 08:41
  • Dear Sam, I have millions of them, How to do it automatically? Thank you very much – forever Nov 11 '16 at 08:59
  • It is the same question here: I have the below file: chr1:109457160 chr1:109457233 chr1:109457614 I need to get new one like: chr1:109457160 chr1 109457160 chr1:109457233 chr1 109457233 chr1:109457614 chr1 109457614 How to do it in R? I appreciate your reply – forever Nov 11 '16 at 09:09
  • Look at using grepl('chr1', a[, 1]). I don't quite understand what you want to do with those. – Sam Ulloa Nov 11 '16 at 15:21