Home > database >  Multiply few columns by one column in R
Multiply few columns by one column in R

Time:08-02

I have columns named X2000 till X2019, how to multiply them by column Multiplier ?

for(i in 2000:2019){
 df.new$X[i]=df.new$Multiplier*df.new$X[i]
  }

CodePudding user response:

Sample data

# A tibble: 100 x 13
   Multiplier X2000 X2001 X2002 X2003 X2004 X2005 X2006 X2007 X2008 X2009 X2010
        <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
 1          3    21    62    40    51    36    36    71    40    66    44    29
 2          5    98    42    72    10    95     7    57     8    99    53    31
 3          5     7    67    93    60    83     6    15    45    98    48    59
 4          4    98     3    41    18    81    85    32    28    58    41     3
 5          5    10    25    27    33     5    57     2    95    52    49    73
 6          3    14    90    90    79    76    60    95    53    72    13    86
 7          4    72    94    65    88    88    12    52    11     2    98    24
 8          5    88    39    70    48    79    92    34    50     5    28     3
 9          3     1    28    43   100     9    60    64     5    65    77    77
10          5    60    78    43    50    15    29    55    84     9    40    41
# ... with 90 more rows, and 1 more variable: X2011 <int>

Multiply

library(tidyverse)

df %>%  
  mutate(across(X2000:X2019, ~ .x * Multiplier))

# A tibble: 100 x 13
   Multiplier X2000 X2001 X2002 X2003 X2004 X2005 X2006 X2007 X2008 X2009 X2010
        <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
 1          3    63   186   120   153   108   108   213   120   198   132    87
 2          5   490   210   360    50   475    35   285    40   495   265   155
 3          5    35   335   465   300   415    30    75   225   490   240   295
 4          4   392    12   164    72   324   340   128   112   232   164    12
 5          5    50   125   135   165    25   285    10   475   260   245   365
 6          3    42   270   270   237   228   180   285   159   216    39   258
 7          4   288   376   260   352   352    48   208    44     8   392    96
 8          5   440   195   350   240   395   460   170   250    25   140    15
 9          3     3    84   129   300    27   180   192    15   195   231   231
10          5   300   390   215   250    75   145   275   420    45   200   205
# ... with 90 more rows, and 1 more variable: X2011 <int>

CodePudding user response:

Basic arithmetic is vectorized on data frames and recycling rule applies.

j <- paste0("X", 2000:2019)
df.new[i] <- df.new$multiplier * df.new[j]

Reproducible Example

set.seed(4891738)
d <- data.frame(round(matrix(rnorm(6), 2), 2))
#    X1   X2    X3
#1 0.27 1.11 -0.85
#2 0.56 2.37 -0.61

## multiple 'X1' to other columns
d[2:3] <- d$X1 * d[2:3]
d
#    X1     X2      X3
#1 0.27 0.2997 -0.2295
#2 0.56 1.3272 -0.3416

how to fix your loop

X[i] does not dynamically translate to "X2000", "X2001", etc. So you need:

for(i in 2000:2019){
 df.new[[paste0("X", i)]] = df.new$Multiplier * df.new[[paste0("X", i)]]
}
  •  Tags:  
  • r
  • Related