0

I recently moved from Stata to R and I'm getting more and more familiar with tidyverse. In Stata I usually relied on for-loops to operate similar calculations, only differing by the referring variables, that can be identified by a variable's suffix. Consider the following example:

newvar_a = oldvar_a + constant
...
newvar_h = oldvar_h + constant

Usually the calculations are a little bit more complex, but basically they always contain one or more variables that share the same suffix and another group plus something. I use these calculations a lot of times. Is there an easy and efficient equivalent in R? I tried solving my problem with functions and loops but didn't get anywhere close. Searching unfortunately didn't show up anything. I'd prefer a solution relying on tidyverse as I'm getting familiar with tidyverse and especially the "apply"-functions seem quite complex to me, but if there is no simple solution with tidyverse, I gladly accept any solution.

Thanks a lot!

  • 1
    Please provide a reproducible example with sample data and desired output. – GuedesBF Sep 02 '21 at 12:50
  • 1
    Welcome to SO, user16814613! Your example is not "similar operations", they are identically `. + constant`. If your constants are intended to be different, then you need to be explicit about that. Please make this question *reproducible* to include usable sample data, expected output given that sample data, and code attempted. If you are getting errors/warnings, include the literal text, too. Please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info for good suggestions on how to do this. Thank you! – r2evans Sep 02 '21 at 12:53
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 02 '21 at 13:40

3 Answers3

1

Here is a tidyverse solution

Example data

df <-
   tibble(
      x1 = rnorm(10),
      x2 = rnorm(10),
      x3 = rnorm(10)
   )

Code

df %>%
   #mutate: allow me to create new variables or change existing variables 
   mutate(
      #across: allow to apply a function to multiple columns
      across(
         #apply a function to columns from x1 to x3
         .cols = x1:x3,
         #create a function that add 2 to a variable
         .fns = function(x) x + 2,
         # set a pattern to create new variable and their respective names
         .names = "{.col}_sum")
         )
      )

Output

# A tibble: 10 x 6
       x1      x2     x3 x1_sum x2_sum x3_sum
    <dbl>   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
 1  0.446  1.48   -1.44   2.45   3.48   0.559
 2  1.23   0.294   1.06   3.23   2.29   3.06 
 3  1.95   0.418  -0.145  3.95   2.42   1.85 
 4 -0.265 -1.33    0.561  1.73   0.667  2.56 
 5 -1.09   1.28    0.130  0.912  3.28   2.13 
 6  2.01  -0.182   0.583  4.01   1.82   2.58 
 7 -0.645  1.15    1.22   1.35   3.15   3.22 
 8 -0.437 -0.955   1.03   1.56   1.04   3.03 
 9  0.168  0.0271  0.293  2.17   2.03   2.29 
10  0.432  0.527   1.65   2.43   2.53   3.65 
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
0

It would be much easier to help with a proper reproducible example. That said, you can use dplyr with mutate, and perform an operation across all columns that match es the desired name pattern.

library(dplyr)

df %>% mutate(across(matches('oldvar_'), ~.x + constant))
GuedesBF
  • 8,409
  • 5
  • 19
  • 37
0

in base you can use assign and get like this:

for (i in letters[1:8]) {
  assign(paste0("newvar_", i), get(paste0("oldvar_", i)) + constant)
}
koolmees
  • 2,725
  • 9
  • 23