Search code examples
rdatabasedataframedplyrcalculated-columns

How to do calculations on a column of a data frame using values contained in another data frame in R?


I have 2 data frames: one with experimental data and one with values of some constants. Experimental data and constants are separated by categories (a and b). I would like to include a new column in the experimental data frame that is the result of the following calculation:

z = k*y

To do this, I'm using the dplyr package and the mutate() function, but I'm not getting the expected result. Does anyone have any tips or suggestions, even if it is necessary to use another package?

library(dplyr)

Category <- c("a", "b")
k <- c(1, 2)

# Data frame with the constants for each category
Constant <- data.frame(Category, k)

x <- seq(0,5,1)

df <- expand.grid(x = x,
                  Category = Category)

# Data frame with the experimental resultas
df$y <- seq(1,12,1)

# Failed attempt to calculate z separated by categories
df %>%
  group_by(Category) %>%
  mutate(z = Constant*y)

Solution

  • With dplyr you can do the following:

    library(dplyr)
    
    left_join(df, Constant, by = c("Category")) %>%
      mutate(z = k * y) %>%
      select(-k)