Search code examples
rdataframevariablesdplyrmerge

R: is it possible to merge to 2 variables in one?


Is it possible to merge 2 variables in a unique one?

This is my dataset

`City Name`  `Average amount` `Nominal Difference`   `%`   `Real Wage`    `Real Difference`

Barletta           2457            1007            41.0        27.5              11.3
Caserta            2445            910             37.2        27.4              10.2
Avellino           2363            1016            43.0        26.5              11.4
Lecce              2342            981             41.9        26.2              11.0
Benevento          2335            1157            49.6        26.1              13.0
Isernia            2334            1078            46.2        26.1              12.1
L'Aquila           2324            1010            43.5        26.0              11.3
Catanzaro          2310            1533            66.3        25.9              17.2
Campobasso         2259            1106            49.0        25.3              12.4
Enna               2242            922             41.1        25.1              10.3

I'd like to have the percentage (with a plus sign and inside parentesis) in the column of Nominal Difference.

To better explain TABLE

What I'm looking for is

`City Name`  `Average amount` `Nominal Difference`   `Real Wage`    `Real Difference`

Barletta           2457            1007 (+41.0%)        27.5              11.3
Caserta            2445            910  (+37.2%)        27.4              10.2
Avellino           2363            1016 (+43.0%)        26.5              11.4
Lecce              2342            981  (+41.9%)        26.2              11.0
Benevento          2335            1157 (+49.6%)        26.1              13.0
Isernia            2334            1078 (+46.2%)        26.1              12.1
L'Aquila           2324            1010 (+43.5%)        26.0              11.3
Catanzaro          2310            1533 (+66.3%)        25.9              17.2
Campobasso         2259            1106 (+49.0%)        25.3              12.4
Enna               2242            922  (+41.1%)        25.1              10.3

How can I do that?

UPDATE

> dput(f)
structure(list(`City Name` = c("Barletta -Andria-Trani", "Caserta", 
"Avellino", "Lecce", "Benevento", "Isernia", "L'Aquila", "Catanzaro", 
"Campobasso", "Enna"), `Average amount` = c(2456.92, 2444.58, 
2363.48, 2341.57, 2334.63, 2334.01, 2323.97, 2310.46, 2259.03, 
2242.38), `Nominal Difference` = c(1006.8, 909.62, 1016.28, 980.7, 
1157.25, 1077.51, 1010.32, 1532.79, 1106.31, 922.35), `%` = c(40.97, 
37.2, 42.99, 41.88, 49.56, 46.16, 43.47, 66.34, 48.97, 41.13), 
    `Real Wage` = c(27.51, 27.37, 26.46, 26.22, 26.14, 26.13, 
    26.02, 25.87, 25.29, 25.11), `Real Difference` = c(11.27, 
    10.18, 11.38, 10.98, 12.95, 12.06, 11.31, 17.16, 12.38, 10.32
    )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))

Solution

  • We can use paste0() inside mutate() and adjust the decimals with sprintf() and the padding with stringr::str_pad():

    library(dplyr)
    library(stringr)
    
    mydat <- tribble(~ `City Name`, ~ `Nominal Difference`,   ~`%`,
                     "Barletta", 1007, 41.0,
                     "Caserta",  910, 37.2
    )
    
    mydat %>% 
      mutate(`Nominal Difference` = paste0(
        str_pad(
          `Nominal Difference`,
          width = max(nchar(`Nominal Difference`))),
        sprintf(" (+%.1f%%)",`%`))
      )
    #> # A tibble: 2 x 3
    #>   `City Name` `Nominal Difference`   `%`
    #>   <chr>       <chr>                <dbl>
    #> 1 Barletta    "1007 (+41.0%)"       41  
    #> 2 Caserta     " 910 (+37.2%)"       37.2
    

    Created on 2023-02-27 by the reprex package (v2.0.1)