I have a data.frame of Quantities and other of Weights and want to multiply Quantities with their corresponding Weights. Can the task be accomplished by selecting only numeric columns? However, I want to keep the other columns in the final output too. Struggling how to get it more efficiently.
library(tidyverse)
df1 <-
tibble(
"A" = paste0("A", 1:10)
, "Q" = 1:10
)
df1
#> # A tibble: 10 x 2
#> A Q
#> <chr> <int>
#> 1 A1 1
#> 2 A2 2
#> 3 A3 3
#> 4 A4 4
#> 5 A5 5
#> 6 A6 6
#> 7 A7 7
#> 8 A8 8
#> 9 A9 9
#> 10 A10 10
df2 <-
tibble(
"A" = paste0("A", 1:10)
, "W" = 11:20
)
df2
#> # A tibble: 10 x 2
#> A W
#> <chr> <int>
#> 1 A1 11
#> 2 A2 12
#> 3 A3 13
#> 4 A4 14
#> 5 A5 15
#> 6 A6 16
#> 7 A7 17
#> 8 A8 18
#> 9 A9 19
#> 10 A10 20
select(df1, -A) * select(df2, -A)
#> Q
#> 1 11
#> 2 24
#> 3 39
#> 4 56
#> 5 75
#> 6 96
#> 7 119
#> 8 144
#> 9 171
#> 10 200
df1*df2
#> Error in FUN(left, right): non-numeric argument to binary operator
Edited
My real data sets has many columns, so looking for a more generic solution.