Dividing 2 data frames

I got one for you guys!

I have 2 data frames, the rownames for both are identical

df1 is 16x1
df2 is 16x785

I want to take each column of df2 and divide it by df1, then take the log of this value and store in a new df3

so, example, lets say df1 and df2 looked like this:

df1                                      df2
   [one]                               [one] [two] [three]
[A]  5                              [A]   10    5       15
[B]  7                              [B]   7     14      21
[C]  9                              [C]   9     9       18

The resulting data would look like this after division, with the same dimensions as df2:

     [one]  [two]  [three]
[A]  2         1         3
[B]  1         2         3
[C]  1         1         2

Then the log of this is easy of course.

I tried dividing df2/df1 and got the old: '/' only defined for equally-sized data frames error

Thanks in advance!

It looks like you can make this work if you pull out the vector df1$one

I did this with the pull() function below

library(tidyverse)

df1 <- data.frame(
  one = c(5,7,9)
)

df2 <- data.frame(
  one = c(10,7,9),
  two = c(5,14,9),
  three = c(15, 21, 18)
)

df2/pull(df1, one)
#>   one two three
#> 1   2   1     3
#> 2   1   2     3
#> 3   1   1     2

Created on 2019-11-05 by the reprex package (v0.3.0)

but

df2/df1$one 

works as well

1 Like

Thanks so much for the reply!

I accually solved it in the interim another method here:

df3 <- mapply('/', df2, df1)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.