Hi All

I've gotten some awesome help getting a formular that finds the intersection of two vectors. This works brilliantly, but I can't figure out how to make it run over another factor. A simple example looks likes this:

  df <- data.frame(
  id       = factor(rep(c("supply", "demand"), each = 10)),
  price    = c(5,7,9,11,13,15,17,19,21,23,20,18,16,14,12,10,8,6,4,2 ),
  quantity = c(3,5,7,13,19,31,37,53,61,67,6,18,20,24,40,46,66,70,76,78)
)

quantity_points <- with(
  df,
  seq(min(quantity), max(quantity), length.out = 500)
)

by_id <- split(df[, c("price", "quantity")], df$id)

interpolated_price <- lapply(
  by_id,
  function(x)
  {
    with(
      x,
      approx(
        quantity,
        price,
        xout = quantity_points
      )
    )$y
  }
)

index_of_equality <- with(interpolated_price, which.min(abs(supply - demand)))
quantity_points[index_of_equality]

Question: I need to run this over a larger data frame, where I have the same data, but also a new factor variable (called hour). So if you have the original data frame and add:

  df <- data.frame(
hour = factor(seq(1:20)),
  id       = factor(rep(c("supply", "demand"), each = 10)),
  price    = c(5,7,9,11,13,15,17,19,21,23,20,18,16,14,12,10,8,6,4,2 ),
  quantity = c(3,5,7,13,19,31,37,53,61,67,6,18,20,24,40,46,66,70,76,78)
)

How can I run it for each hour? I tried using:
by_hour <- split(df[, c("price", "quantity")], df$hour)
mapply(fx, by_hour)

And gathering the above into a fx <- function(){"the neat code"}, but I can't get it to work.

Kind Regards,
Lasse

	[[alternative HTML version deleted]]

