[R] Making a function and applying it over a list(?)
Christoph Häni
ch.haeni at gmail.com
Thu Oct 24 20:59:36 CEST 2013
You could store your first approach in a function and lapply it to
your by_hour variable:
df <- data.frame(
hour = factor(rep(1:5,4)),
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)
)
myfu <- function(x){
df <- x # for simplicity
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]
}
by_hour <- split(df,df$hour)
lapply(by_hour,myfu)
Was that what you were looking for?
Cheers,
Christoph
2013/10/24 Lasse Thorst <lath at energidanmark.dk>:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list