Rename data in more than one column

Hello!

I'm wanting to rename some column variables.

For example: in the "PAIS_ORIGEN" column, change "BR - BRAZIL" to BRAZIL

In the "PAIS_PROCEDENCIA" column, change "AR - ARGENTINA" to ARGENTINA

My data is in the "Base" dataframe

Base %>% rename(PAIS_ORIGEN, ARGENTINA="AR - ARGENTINA", PAIS_PROCEDENCIA, BRAZIL=" BR - BRAZIL")

rename() is for renaming columns not values from a column. There are several ways to accomplish this task, this is one of them.

library(dplyr)
library(stringr)

# Sample data in a copy/paste friendly format. Replace this with your own data frame
Base <- data.frame(
    PAIS_ORIGEN = "AR - ARGENTINA",
    PAIS_PROCEDENCIA = "BR - BRAZIL"
)

#Relevant code
Base %>%
    mutate(across(c(PAIS_ORIGEN, PAIS_PROCEDENCIA), ~ str_remove(.x, "^.{2}\\s-\\s")))
#>   PAIS_ORIGEN PAIS_PROCEDENCIA
#> 1   ARGENTINA           BRAZIL

Created on 2023-03-03 with reprex v2.0.2

Note: Next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

Thank you very much, with your help I was able to solve the problem

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.