-2

I have two file:

  1. first file is single column file with header (file1$sample), contain 200 samples/line.

  2. second is multi column and multiple rows:

    dim(file2) 
    #[1] 652 20206  
    

I want to match file1$sample and file2$sample and extract matrix (200*20206).

I tried following command:

new <- merge(file1, file2, by.x='sample', by.y='sample') 

but is gives only single line file

dim(new) 
#[1] 0 20206

So please help me how can I extract (200*20206) matrix.

Thanks

nicola
  • 24,005
  • 3
  • 35
  • 56
mona
  • 101
  • 1
  • 2
  • 12
  • 1
    Try `file2[match(file1$sample,file2$sample),]`. – nicola May 12 '16 at 10:47
  • Thanks nicola, I tried this , it return matrix ( 200 *20206), but all values are "NA" – mona May 12 '16 at 10:56
  • Welcome to Stack Overflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 May 12 '16 at 10:56
  • 1
    [How to join (merge) data frames (inner, outer, left, right)?](http://stackoverflow.com/questions/1299871) – zx8754 May 12 '16 at 10:56

2 Answers2

1

Try this:

result=merge(x=file1,y=file2,by="sample",all=T)
Sotos
  • 51,121
  • 6
  • 32
  • 66
0

Try this :

file1 = read.table("mydata1.txt", header=TRUE)
file2 = read.table("mydata2.txt", header=TRUE)
m=merge(file1,file2,by="sample")
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • I tried: file1 = read.table("mydata1.txt", header=T) [1] 200 1, file2 = read.table("mydata2.txt", header=T) [1] 652 20206 but output is [1] 0 20206 – mona May 12 '16 at 11:08
  • That code should work. Post an extract of both the files & I shall post the output. – Ani Menon May 12 '16 at 14:18