I have a code that will generate a tibble with
1 identifier col
1 col T with numeric values
X cols with numeric values, named A_1, A_2, A_3, ..., A_X
My output have to be
X cols named B_1, B_2, B_3, ..., B_X with numeric values such that
B_X[z] = A_X[z] * T[z]
If X = 1, the code would be straightforward:
Tibble %>%
mutate(B = A * T)
The issue is that I don't know the value of X.
Basically I would need to for (i in 1:X) {Tibble %>% add_column() -> Tibble}
but I am almost sure there are fancier solutions, especially because I'd like to avoid assignments and just proceed to summarize the B-cols.
Thank you.
CodePudding user response:
You could do:
df %>%
mutate(across(starts_with('A'), .names = "B_{str_extract(col, '[0-9] $')}") * T)
or even:
df %>%
mutate(across(starts_with('A'), ~.x*T, .names = "B_{str_extract(col, '[0-9] $')}"))
CodePudding user response:
You may get the columns to multiply using grep
and multiplying them with T
column should be straightforward. Using base R, you can do the following -
df <- data.frame(ID = 1:5, T = c(2, 4, 5, 6, 1), A_1 = c(3, 4, 5, 1, 2),
A_2 = 1:5, A_3 = c(7, 8, 3, 2, 9))
cols <- grep('A_\\d ', names(df), value = TRUE)
df[sub('A', 'B', cols)] <- df$T * df[cols]
df
# ID T A_1 A_2 A_3 B_1 B_2 B_3
#1 1 2 3 1 7 6 2 14
#2 2 4 4 2 8 16 8 32
#3 3 5 5 3 3 25 15 15
#4 4 6 1 4 2 6 24 12
#5 5 1 2 5 9 2 5 9