I have a data frame which looks like this
> data
A B
1 1 2
2 2 1
I have a reference data frame which looks like this
> ref
Names Values
1 A 5
2 B 10
I want to multiply each column by corresponding row in Ref having same Name
the result should be this
> result
A B
1 5 20
2 10 10
What is the fastest way to achieve this in R? Any help would be greatly appreciated
We can match
the column names of 'data' with 'Names' column of 'ref', get the corresponding 'Values' based on the numeric index and then multiply by replicating the ref$Values
data*ref$Values[match(names(data), ref$Names)][col(data)]
# A B
#1 5 20
#2 10 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With