I have a data.frame (call in the example "segments") with info related to a certain name (call "segments$seg_ID"). Each name has several information attached in columns. In other data.frame (call "trayectorias"), I have those names ("segments$seg_ID") now called ("trayectorias$seg_ID_trayectorias") . What I would like to do is associate info from the 1st data.frame ("segments") to the 2nd one, depending on the name. Important to note: in the 2nd data.frame ("trayectorias"), "segments$seg_ID" are most of the times repeated, that is to say, I have around 24000 rows in the "segments$seg_ID" data.frame and about 95000 in the "trayectorias$seg_ID_trayectorias" one.
To present my issue I have created a short reprex:
segments<-data.frame(stringsAsFactors=FALSE,
seg_ID = c("%%EDH_WSN", "%DIPA_PITES", "%DIPI_LADAT",
"%DRSI_SITET", "*%200_BAKER", "**EG1_MID",
"**TNT_IBUGO"),
Rumbo_circular = c(53, 297, 299, 335, 137, 321, 336),
)
trayectorias<-data.frame(stringsAsFactors=FALSE,
seg_ID_trayectorias = c("%%EDH_WSN", "%DIPA_PITES", "%%EDH_WSN", "%DRSI_SITET",
"%200_BAKER", "**EG1_MID", "%200_BAKER"),
)
Solution would be:
solution<-data.frame(stringsAsFactors=FALSE,
seg_ID_trayectorias = c("%%EDH_WSN", "%DIPA_PITES", "%%EDH_WSN", "%DRSI_SITET",
"%200_BAKER", "**EG1_MID", "%200_BAKER"),
Rumbo_circular = c(53, 297, 53, 335, 137, 321, 137)
)
As it can be seen, "segments$seg_ID" names were searched in the "trayectorias$seg_ID_trayectorias" data.frame and info from the "segments" data.frame ("segments$Rumbo_circular") was associated to each name.