How would I divide one data frame by another? The two data frames have the same columns and same rows, but I need to divide every intersect with its corresponding intersect into a new data frame, e.g. below:
DF1
Name Jan Feb Mar
Aaron 2 4 3
Blake 5 6 4
DF2
Name Jan Feb Mar
Aaron 4 6 6
Blake 7 6 5
DF1/DF2 = DF3
DF3 (result)
Name Jan Feb Mar
Aaron 0.5 0.7 0.5
Blake 0.7 1.0 0.8
I'm using subset then dcast to build each data frame, but having a hard time figuring out how to divide them. Thanks for your help!
We divide the numeric columns in both 'DF1' and 'DF2' (by removing the first column) and cbind
with the first column.
DF3 <- cbind(DF1[1],round(DF1[-1]/DF2[-1],1))
DF3
# Name Jan Feb Mar
# 1 Aaron 0.5 0.7 0.5
# 2 Blake 0.7 1.0 0.8
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