Change values in columns

So, I have a dataframe like this: df<-data.frame("Year" = c(2021,2021,2021), "Value" = c(358,723,287), "Country" = c("FR","USA","IE"))

I want to change the values in "Country" column for the full name: France, United States, Ireland. How can I achieve this? Thanks.

One way would be to use the mutate function from the dplyr package to change the column. You could then recode the values with the recode function (also from dplyr) in the following way.

df <- data.frame("Year" = c(2021,2021,2021),
                 "Value" = c(358,723,287),
                 "Country" = c("FR","USA","IE"))

#install.packages("dplyr") #if you don't have dplyr
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>     filter, lag
#> The following objects are masked from 'package:base':
#>     intersect, setdiff, setequal, union
       Country = recode(Country,
                        "FR" = "France",
                        "USA" = "United States",
                        "IE" = "Ireland"))
#>   Year Value       Country
#> 1 2021   358        France
#> 2 2021   723 United States
#> 3 2021   287       Ireland

Created on 2021-04-15 by the reprex package (v0.3.0)

1 Like

Great Hlynur, works perfectly. Thanks

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.