rename and associate info

I need to give name to a number based on info from a data.frame. To expose my issue I create a short reprex:
segmento_info<- data.frame(stringsAsFactors=FALSE,
ID = c(35540, 35658, 35731, 35825, 35541, 35542,
35543, 35544),
Segment_Name = c("XILTI_ARTAT", "ZAG_VBA", "ZMR_ORBIS",
"ZWN_PIREK", "VERCE_MLP.A", "VERED_BMN.A",
"VERED_WSN", "VEREV_GIMEX")
)

InfoTOChange<- data.frame(stringsAsFactors=FALSE,
ID_1 = c(35540, 35540, 35540, 35540, 35540, 35540,
35540, NA),
ID_2 = c(35658, 35731, 35825, 35541, 35542, 35543,
35544, NA),
Non_Possible_Conflict = c("TRUE", "TRUE", "TRUE", "FALSE", "FALSE",
"FALSE", "FALSE", NA),
Possible_Inicial_Conflict = c("FALSE", "FALSE", "FALSE", "TRUE", "TRUE",
"FALSE", "FALSE", NA),
Possible_End_Conflict = c("FALSE", "FALSE", "FALSE", "TRUE", "TRUE",
"FALSE", "FALSE", NA)
)

Data.frame "segmento_info" contains the info I would like to change in the other data.frame "InfoTOChange". As it can be seen in the "segmento_info" data.frame, each ID has associated a name. What I would like the code to do for me, is to asociate ID_1 and ID_2 to its original name from the "segmet_info" data.frame; that is to say:

  • ID_1 is 35540, however, it´s name (searched in the "segment_info"data.frame) is XILTI_ARTAT.
  • ID_2 is 35658, however, it´s name (searched in the "segment_info"data.frame) is ZAG_VBA.

All IDs (both ID_1 and ID_2) from the data.frame "InfoTOChange" are contained in the other data.frame. "InfoTOChange" has asocciated 3 rows that I would like to keep.
Therefore, solution would be:
InfoAlreadyChanged<- data.frame(stringsAsFactors=FALSE,
Segment_Name_1 = c("XILTI_ARTAT", "XILTI_ARTAT", "XILTI_ARTAT",
"XILTI_ARTAT", "XILTI_ARTAT", "XILTI_ARTAT",
"XILTI_ARTAT"),
Segment_Name_2 = c("ZAG_VBA", "ZMR_ORBIS", "ZWN_PIREK",
"VERCE_MLP.A", "VERED_BMN.A", "VERED_WSN",
"VEREV_GIMEX"),
Non_Possible_Conflict = c("TRUE", "TRUE", "TRUE", "FALSE", "FALSE",
"FALSE", "FALSE"),
Possible_Inicial_Conflict = c("FALSE", "FALSE", "FALSE", "TRUE", "TRUE",
"FALSE", "FALSE"),
Possible_End_Conflict = c("FALSE", "FALSE", "FALSE", "TRUE", "TRUE",
"FALSE", "FALSE")
)

Important to note that I have around 35832 rows in the data.frame "segmento_info" and 42778 rows in the "InfoTOChange" data.frame, that means code should be as simple as possible to reduce compiling time.

Thanks in advance for your help!

The solutions I gave here to a similar problem should work in your case as well:

I'm just including one option here, but take a look at the link in that other thread for more. I'm using dplyr at the end just to do some reordering of the variables (though, again, there are many ways to do this in R)

segmento_info<- data.frame(stringsAsFactors=FALSE,
                           ID = c(35540, 35658, 35731, 35825, 35541, 35542,
                                  35543, 35544),
                           Segment_Name = c("XILTI_ARTAT", "ZAG_VBA", "ZMR_ORBIS",
                                            "ZWN_PIREK", "VERCE_MLP.A", "VERED_BMN.A",
                                            "VERED_WSN", "VEREV_GIMEX")
)

InfoTOChange<- data.frame(stringsAsFactors=FALSE,
                          ID_1 = c(35540, 35540, 35540, 35540, 35540, 35540,
                                   35540),
                          ID_2 = c(35658, 35731, 35825, 35541, 35542, 35543,
                                   35544),
                          Non_Possible_Conflict = c("TRUE", "TRUE", "TRUE", "FALSE", "FALSE",
                                                    "FALSE", "FALSE"),
                          Possible_Inicial_Conflict = c("FALSE", "FALSE", "FALSE", "TRUE", "TRUE",
                                                        "FALSE", "FALSE"),
                          Possible_End_Conflict = c("FALSE", "FALSE", "FALSE", "TRUE", "TRUE",
                                                    "FALSE", "FALSE")
)

InfoTOChange$Segment_Name_1 <- segmento_info[match(InfoTOChange$ID_1, segmento_info$ID), ]$Segment_Name

InfoTOChange$ID_1 <- NULL

InfoTOChange$Segment_Name_2 <- segmento_info[match(InfoTOChange$ID_2, segmento_info$ID), ]$Segment_Name

InfoTOChange$ID_2 <- NULL

dplyr::select(InfoTOChange, dplyr::starts_with("Segment"), dplyr::everything())
#>   Segment_Name_1 Segment_Name_2 Non_Possible_Conflict Possible_Inicial_Conflict
#> 1    XILTI_ARTAT        ZAG_VBA                  TRUE                     FALSE
#> 2    XILTI_ARTAT      ZMR_ORBIS                  TRUE                     FALSE
#> 3    XILTI_ARTAT      ZWN_PIREK                  TRUE                     FALSE
#> 4    XILTI_ARTAT    VERCE_MLP.A                 FALSE                      TRUE
#> 5    XILTI_ARTAT    VERED_BMN.A                 FALSE                      TRUE
#> 6    XILTI_ARTAT      VERED_WSN                 FALSE                     FALSE
#> 7    XILTI_ARTAT    VEREV_GIMEX                 FALSE                     FALSE
#>   Possible_End_Conflict
#> 1                 FALSE
#> 2                 FALSE
#> 3                 FALSE
#> 4                  TRUE
#> 5                  TRUE
#> 6                 FALSE
#> 7                 FALSE

Created on 2019-12-06 by the reprex package (v0.3.0.9001)

R is an interpreted language, so you don't need to worry about compilation!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.