Height_m
should have transformation
value as 100, I guess.
You can use cur_column()
to get the column name to match with Transformation_d
dataframe and get corresponding transformation
value to multiply.
library(dplyr)
d %>%
mutate(across(-1, ~. * Transformation_d$transformation[match(cur_column(),
Transformation_d$marker)])) %>%
transmute(ID,
Height = coalesce(Height_cm, Height_m),
Weight = coalesce(Weight_kg, Weight_lb))
# ID Height Weight
#1 1 180 70
#2 2 165 54
#3 3 180 80
#4 4 100 27
#5 5 190 90
#6 6 170 100
A similar process in base R
cbind(d[1],
transform(sweep(d[-1], 2,
Transformation_d$transformation[match(names(d)[-1],
Transformation_d$marker)], `*`),
Height = ifelse(is.na(Height_cm), Height_m, Height_cm),
Weight = ifelse(is.na(Weight_kg), Weight_lb, Weight_kg)))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…