nhaus
nhaus

Reputation: 1023

Divide values of a specific group by value depending on the group

I am trying to divide different groups of my data frame by different values, depending on the group that I am currently operating on.

The value by which the numbers should be divided I get from a different dataset, that assigns to each "grouping variable" a value.

I think something like this might work, but I do not know how I can "access" the grouping variable that dplyr currently operates on...

diamonds  # built in dataset
div_df <- data.frame(E=4,I=10,J=31,H=5,F=2,G=1,D=400,row.names = "value") %>% t() %>% rownames_to_column("color")


diamonds  %>% group_by(color) %>% mutate(price=price/div_df[div_df["color"]==`GROUPING VARIABLE`][2])

If anyone knows how to achieve what I am doing, I would be very grateful!

Cheers!

Upvotes: 1

Views: 834

Answers (2)

zx8754
zx8754

Reputation: 56249

Merge the datasets, then calculate the price.

merge(diamonds, div_df, by = "color") %>% 
     mutate(priceNew = price/value)

Upvotes: 1

stefan
stefan

Reputation: 125617

You are probably looking for dplyr::group_vars which returns the grouping variable as a character string:

library(dplyr)
library(tibble)

div_df <- c(E = 4, I = 10, J = 31, H = 5, F = 2, G = 1, D = 400) %>%
  enframe(name = "color")

ggplot2::diamonds %>%
  group_by(color) %>%
  mutate(price = price / div_df[["value"]][match(.data[[group_vars(.)]], div_df[["color"]])])
#> # A tibble: 53,940 × 10
#> # Groups:   color [7]
#>    carat cut       color clarity depth table price     x     y     z
#>    <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  0.23 Ideal     E     SI2      61.5    55  81.5  3.95  3.98  2.43
#>  2  0.21 Premium   E     SI1      59.8    61  81.5  3.89  3.84  2.31
#>  3  0.23 Good      E     VS1      56.9    65  81.8  4.05  4.07  2.31
#>  4  0.29 Premium   I     VS2      62.4    58  33.4  4.2   4.23  2.63
#>  5  0.31 Good      J     SI2      63.3    58  10.8  4.34  4.35  2.75
#>  6  0.24 Very Good J     VVS2     62.8    57  10.8  3.94  3.96  2.48
#>  7  0.24 Very Good I     VVS1     62.3    57  33.6  3.95  3.98  2.47
#>  8  0.26 Very Good H     SI1      61.9    55  67.4  4.07  4.11  2.53
#>  9  0.22 Fair      E     VS2      65.1    61  84.2  3.87  3.78  2.49
#> 10  0.23 Very Good H     VS1      59.4    61  67.6  4     4.05  2.39
#> # … with 53,930 more rows

Upvotes: 1

Related Questions