[R] E-mail # 2 / attachments matrix test cases

arun smartpink111 at yahoo.com
Mon Sep 10 14:22:10 CEST 2012


HI Sridhar,

Try this:
#I saved the file as .csv.

 testMaster<-read.csv("test1Master.csv")
 test2Sorted<-read.csv("test2Sorted.csv")
testMaster1<-testMaster[order(testMaster[,1]),]
test2Sorted1<-test2Sorted[order(test2Sorted[,1]),]
Combinedat<-as.matrix(merge(testMaster1,test2Sorted1,by="X"))
 rownames(Combinedat)<-Combinedat[,1]
Combinedat<-Combinedat[,-1]
 head(Combinedat,3)
#              edgeMax Kmeans firstEdge25 firstEdge50 FirstEdge.75 V1 
#IMAGE:1010026 "3"     "3"    "1"         "1"         "3"          "2"
#IMAGE:1010082 "1"     "1"    "2"         "2"         "1"          "4"
#IMAGE:1010367 "4"     "4"    "1"         "1"         "1"          "2"
A.K.




----- Original Message -----
From: Sridhar Iyer <sridiyer at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: Milan Bouchet-Valat <nalimilan at club.fr>
Sent: Monday, September 10, 2012 8:05 AM
Subject: E-mail # 2 / attachments matrix test cases

forgot to attach the sample files. sorry.

On Mon, Sep 10, 2012 at 7:02 AM, Sridhar Iyer <sridiyer at gmail.com> wrote:
> Hi Milan & Arun,
>
> Thank you so much for the response. Sorry I did not include a test file.
> (New to R,  did not realize the importance of enclosing a test case - it does
> make sense).
>
> test1Master.xls   - has some data (from a set of experiments)  Row 1
> (row names are genes)
>                                     row names in some random order
> test2sorted.xls  - has data from a new experiment.
>                                       row names in a different order
> (but genes are same as in test1Master file
>                                                    only the oder is different)
>
> I need to append data from the new experiment (test2Sorted) into
> test1Master file.
>
> Thanks
>
> Srid
> On Mon, Sep 10, 2012 at 6:51 AM, arun <smartpink111 at yahoo.com> wrote:
>> HI,
>> You can also use ?merge()
>> Same example data from Milan:
>> A <- matrix(runif(4*10), 10)
>> rownames(A) <- LETTERS[1:10]
>> B <- matrix(runif(10), 10)
>> rownames(B) <- sample(LETTERS[1:10])
>>
>> #Either, use
>>  C<-as.matrix(merge(A,B[order(rownames(B)),],by="row.names"))
>> #or C<-as.matrix(merge(A,B[order(rownames(B)),],by=0))
>> row.names(C)<-C[,1]
>> C<-C[,-1]
>>
>>  colnames(C)<-NULL
>>  C
>> A.K.
>>
>>
>>
>>
>> ----- Original Message -----
>> From: Milan Bouchet-Valat <nalimilan at club.fr>
>> To: Sridhar Iyer <sridiyer at gmail.com>
>> Cc: r-help at r-project.org
>> Sent: Monday, September 10, 2012 3:22 AM
>> Subject: Re: [R] R- merging two matrix
>>
>> Le dimanche 09 septembre 2012 à 19:29 -0500, Sridhar Iyer a écrit :
>>> Hi,
>>>
>>> I have two matrices.  (same size, same number of rows,
>>> same names for the rows  - one has data in sorted
>>> order, another one master data in unsorted order. I need to
>>> combine both).
>>>
>>> Rows names of Matrix A
>>> P, Q, R, S, T, U, V .......        (some order) - each row has 4 values
>>>
>>> Row names of Matrix B (same as B, but in a different (sorted) order)
>>>
>>> Q, P, T, U, V, .......     (same names, but different order) - has one value
>>>
>>> I need to create a master matrix with data from these two.
>>>
>>> How do I take the numerical value for Q (from Matrix B) and append it to the
>>> values in Matrix A. (so it becomes the 5th value in the master file)
>>>
>>> Thank you very much
>>>
>>> Srid Iyer
>> A small reproducible example would have been nice. Anyways, let's build
>> two matrices:
>> A <- matrix(runif(4*10), 10)
>> rownames(A) <- LETTERS[1:10]
>> B <- matrix(runif(10), 10)
>> rownames(B) <- sample(LETTERS[1:10])
>>
>> Then you can just do:
>> C <- cbind(A, B[match(rownames(A), rownames(B)),])
>>
>> or, if A is sorted by row names (depends on the locale...) :
>> D <- cbind(A, B[order(rownames(B)),])
>>
>> Regards
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>




More information about the R-help mailing list