I have two dataframes with those columns:
1ºData frame:
N | Accesion |
1 | ATT |
2 | BTT |
3 | CTT |
4 | GTT |
5 | ITT |
2º Dataframe:
| Accesion|
ATT
ATT
ATT
CTT
CTT
CTT
CTT
GTT
GTT
.
.
.
And this is what I would like to have in the 2º Database:
N | Accesion
1 | ATT
1 | ATT
1 | ATT
2 | BTT
2 | BTT
2 | BTT
2 | BTT
2 | BTT
3 | CTT
.
.
I was trying to do this with a double loop: Firstly I sorted the accessions:
df1 <- with(df1 , df1 [order(df1 $Accession) , ])
df2 <- with(df2, df2[order(df2$Accesion) , ])
And then I used a double loop in order to access in each accession and compare, if the accession matches, then, create a row with the number of the row beside that accession.
for (i in 1:length(df1$Accession)){
for (z in 1:length(df2$Accesion)){
if (df2$Accesion[z] == df1$Accession[i]){
df2$N[z] <- df1$N[i]
}
}}
This work when the dataframe is small, but I'm currently working with data frame with length around 60000 rows.
Do you know another efficient way to do this?
Here you are and a reproducible example:
df1 <- data.frame(N=c(1,2,3,4,5), Accession=c("ATT","BTT","CTT","GTT","ITT"))
df2 <- data.frame(Accession=c("ATT","ATT","ATT","BTT","BTT","BTT","CTT","CTT","GTT")))
Thank you in advance