Using strings in r to avoid manual work

Hi,

I have the following data frame called data:

  country    dog_2021  cat_2022  dog_2021 cat_2022
1  Norway        1        4         7         2
2      UK        2        5         8         2
3     USA        3        6         9         2

I want the columnames of this data frame to update for every new year that comes by, a so called rolling time window.

So, when the next year comes, it will be

  country    dog_2022  cat_2023  dog_2022 cat_2023
1  Norway        1        4         7         2
2      UK        2        5         8         2
3     USA        3        6         9         2

I would like this change to happen without the need to update the name of each column manually.. Any suggestions?

Also, I need to multiply all the columns by 1.25, except from the first column, which only contains characters. Ideally it would be something like this:

thisyear <- 2022
lastyear <- 2021
new_data <- data %>%
mutate_at(vars((paste(dog, lastyear, sep =""):paste(cat, thisyear, sep="")),
.funs = funs(. * 1.25)))

But when I run this code, it doesn't work out. Does anyone have an idea to solve this problem?

Thank you so much in advance!

Column names should not be duplicated. Here's a solution that assumes the second dog/cat columns should be _2023/_2024

dat <- data.frame(
  country =
    c("Norway", "UK", "USA"),
  dog_2022 =
    c(1, 2, 3),
  cat_2023 =
    c(4, 5, 6),
  dog_2023 =
    c(7, 8, 9),
  cat_2024 =
    c(2, 2, 2))
  
dat
#>   country dog_2022 cat_2023 dog_2023 cat_2024
#> 1  Norway        1        4        7        2
#> 2      UK        2        5        8        2
#> 3     USA        3        6        9        2

mk_cols <- function(x,y) colnames(x) = c("country", paste0("dog_",y),paste0("cat_",y+1),paste0("dog_",y+1),paste0("cat_",y+2))

mk_cols(dat,2022)
dat
#>   country dog_2022 cat_2023 dog_2023 cat_2024
#> 1  Norway        1        4        7        2
#> 2      UK        2        5        8        2
#> 3     USA        3        6        9        2

dat[,2:5] <- dat[,2:5]*1.5
dat
#>   country dog_2022 cat_2023 dog_2023 cat_2024
#> 1  Norway      1.5      6.0     10.5        3
#> 2      UK      3.0      7.5     12.0        3
#> 3     USA      4.5      9.0     13.5        3

Hi,

thanks for your reply, but this is not what I am looking for.

In later versions of dplyr, mutate_at is not encouraged to use any more. Use across, and it'll work. See below:

library(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

sample_data <- data.frame(country = c("Norway", "UK", "USA"),
                          dog_2021 = c(1, 2, 3),
                          cat_2021 = c(4, 5, 6),
                          dog_2022 = c(7, 8, 9),
                          cat_2022 = c(2, 2, 2))

current_year <- 2021
next_year <- 2022

sample_data %>%
    mutate(across(.cols = paste0("dog_", current_year):paste0("cat_", next_year),
                  .fns = ~ .x * 1.25))
#>   country dog_2021 cat_2021 dog_2022 cat_2022
#> 1  Norway     1.25     5.00     8.75      2.5
#> 2      UK     2.50     6.25    10.00      2.5
#> 3     USA     3.75     7.50    11.25      2.5

Created on 2021-12-21 by the reprex package (v2.0.1)

Hope this helps.

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.