Home > database >  How to calculate ratio between 2 columns in R (with a function)?
How to calculate ratio between 2 columns in R (with a function)?

Time:04-19

we are running an experiment that checks the effect of brand on product value. In order to calculate the results, we first need to calculate the ratio between 2 answers to the same product. We want to do it by a function and not manual. for example

prod1_nobrand prod1_brand prod2_nobrand prod2_brand
1             2           4             8
2             6           5             20

enter image description here

we want to create a new column for each ratio for example

prod1_ratio prod2_ratio
2           2
3           4

any idea how to do that? thank you!

CodePudding user response:

The simple base R way to do that would be as follows...

yourdataframe$new_ratio_col <- yourdataframe$prod1 / yourdataframe$prod2

CodePudding user response:

I'm not 100% sure I'm understanding your ask correctly. But my feeling is that you should familiarize yourself with dplyr functions. You can use the mutate function to create new columns in this way:

library(dplyr)

df %>%
mutate(prod1_ratio = prod1_nobrand  / prod2 prod1_brand)

CodePudding user response:

One option is the put it into long form, calculate the ratio, then pivot back to wide form with tidyverse.

library(tidyverse)

df %>%
  pivot_longer(everything(), names_to = c("num", ".value"), names_sep = "_") %>%
  mutate(ratio = brand / nobrand) %>% 
  select(-ends_with("brand")) %>% 
  group_by(num) %>% 
  mutate(rowid = row_number()) %>% 
  pivot_wider(names_from = "num", values_from = "ratio", names_glue = "{num}_ratio") %>% 
  select(-rowid)

Output

  prod1_ratio prod2_ratio
        <dbl>       <dbl>
1           2           2
2           3           4

Data

df <- structure(list(prod1_nobrand = 1:2, prod1_brand = c(2L, 6L), 
    prod2_nobrand = 4:5, prod2_brand = c(8L, 20L)), class = "data.frame", row.names = c(NA, 
-2L))
  • Related