[R-sig-eco] Combine elements in pairs from two data.frames

Bede-Fazekas Ákos bfalevlist at gmail.com
Fri Nov 11 21:47:47 CET 2016


Dear Manoeli,
I think that this should work:

A <- data.frame(Number = c(601, 614, 2, 45), Gene = c("BIO_14", 
"BIO_16", "TB_54", "TU_85"), Crossing = c("CD/SINT", "BIORA", "MANL", 
"SOSSEGO"))
B <- data.frame(ID = 1:3, ParentA = c(601, 601, 45), ParentB = c(614, 2, 
601))
ParentA <- merge(x = B[, c("ID", "ParentA")], y = A, by.x = "ParentA", 
by.y = "Number")
ParentA <- ParentA[order(ParentA$ID), -2]
ParentB <- merge(x = B[, c("ID", "ParentB")], y = A, by.x = "ParentB", 
by.y = "Number")
ParentB <- ParentB[order(ParentB$ID), -2]
cbind(ParentA, ParentB) # detailed result
data.frame(Parents = paste0(.Last.value[, 1], "/", .Last.value[, 4]), 
Genotype = paste0(.Last.value[, 2], "/", .Last.value[, 5])) # the result 
you expected

HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences

2016.11.11. 16:04 keltezéssel, f.krah at mailbox.org írta:
> something like this?
>
> x <- cbind(c("a", "b", "c", "d"), 1:4)
> y <- cbind(c("a", "a", "d"),c("b", "c", "a"))
> t(apply(y, 1, function(z) x[match(z, x[,1]),2]))
>
>
>> On 11 Nov 2016, at 15:49, Manoeli Lupatini <mlupatini at gmail.com> wrote:
>>
>> In the example below I have two datasets (A and B). I want to combine the
>> elements in pair in DatasetA based on names in cells belong to ParentA and
>> ParentB from DatasetB.
>>
>> Dataset A:
>>
>> Number    Gene      Crossing
>> 601       BIO_14    CD/SINT
>> 614       BIO_16    BIORA
>> 2         TB_54     MANL
>> 45        TU_85     SOSSEGO
>>
>> Dataset B:
>>
>> ParentA     ParentB
>> 601         614
>> 601         2
>> 45          601
>>
>> The result will be like this:
>>
>> Parents     Genotype
>> 601/614     BIO_14/BIO_16
>> 601/2       BIO_14/TB_54
>> 45/601      TU_85/BIO_14
>>
>> Thanks,
>>
>> Manoeli
>>
>> --
>> Manoeli Lupatini
>> Research Supervisor/Biotrigo Genética Ltda.
>> Passo Fundo/RS/Brasil
>> +55 55 91411174
>> Agronomist
>> MSc in Soil Science
>> Dr in Soil Science
>> Dr in Biology
>> Perfil público
>> https://br.linkedin.com/in/manoeli-lupatini-7a068943
>>
>> 	[[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-ecology mailing list
>> R-sig-ecology at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology



More information about the R-sig-ecology mailing list