Renaming rows in column

This example:

df <- data.frame(
          stringsAsFactors = FALSE,
                 SECUENCIAL = c(47638,47639,
                                47640,47641,47642,47643,47644,47645),
                      YEAR = c(2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019),
                     MONTH = c(1, 1, 1, 2, 2, 2, 1, 1),
                   WEEKDAY = c(2, 4, 6, 1, 4, 4, 7, 4),
                      HOUR = c(10, 11, 22, 10, 17, 11, 8, 3),
             COD_PROV = c(24, 24, 24, 24, 24, 24, 24, 24),
              COD_MUN = c("24115", "24108","24115","24108","24115","24110","24110",
                                "24115"),
                      ZONE = c(1, 3, 3, 1, 1, 3, 3, 3))

I want to rename the number in the COD_MUN column to a name based one: 24115 -> PlaceOne , 24108 -> PlaceTwo or 24110 -> PlaceThree.
It is possible to do this with Rstudio? I've been trying with rownames, but I still don't have enough mastery over R to do it.
Thanks all.

Have you tried with named vectors?

sample_df <- data.frame(
  stringsAsFactors = FALSE,
  SECUENCIAL = c(
    47638, 47639,
    47640, 47641, 47642, 47643, 47644, 47645
  ),
  YEAR = c(2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019),
  MONTH = c(1, 1, 1, 2, 2, 2, 1, 1),
  WEEKDAY = c(2, 4, 6, 1, 4, 4, 7, 4),
  HOUR = c(10, 11, 22, 10, 17, 11, 8, 3),
  COD_PROV = c(24, 24, 24, 24, 24, 24, 24, 24),
  COD_MUN = c(
    "24115", "24108", "24115", "24108", "24115", "24110", "24110",
    "24115"
  ),
  ZONE = c(1, 3, 3, 1, 1, 3, 3, 3)
)

name_changes <- c("24115" = "PlaceOne", "24108" = "PlaceTwo", "24110" = "PlaceThree")
sample_df["new_names"] <- name_changes[sample_df[["COD_MUN"]]]

sample_df
#>   SECUENCIAL YEAR MONTH WEEKDAY HOUR COD_PROV COD_MUN ZONE  new_names
#> 1      47638 2019     1       2   10       24   24115    1   PlaceOne
#> 2      47639 2019     1       4   11       24   24108    3   PlaceTwo
#> 3      47640 2019     1       6   22       24   24115    3   PlaceOne
#> 4      47641 2019     2       1   10       24   24108    1   PlaceTwo
#> 5      47642 2019     2       4   17       24   24115    1   PlaceOne
#> 6      47643 2019     2       4   11       24   24110    3 PlaceThree
#> 7      47644 2019     1       7    8       24   24110    3 PlaceThree
#> 8      47645 2019     1       4    3       24   24115    3   PlaceOne

Hope this helps.

3 Likes

this is a standard application of a left join. Create a new dataframe with your lookup table:

lookup_table <- tribble(
  ~COD_MUN, ~place_name,
  "24115",  "PlaceOne" , 
  "24108", "PlaceTwo",
  "24110", "PlaceThree"
)

and then look up the place codes thus:

df %>% left_join(lookup_table)

You now have a new column place_name with the names of the places that match your place codes.

This will work as long as the column of place codes has the same name in df as it does in the lookup table. Otherwise, you will need to look into by in the help for left_join.

1 Like

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.