I need to create a function that will return several things, I started with a real data example and was able to get the result, but not really sure how to combine all these commands into one r function. My code here:
# find all the cases with the given breakover and store in a data frame named find_id
find_id <- model%>%
filter(breakover == 5)%>%
select(id)
#With the returned id, inner join with another table
all_case <- sim_breakover %>% inner_join(find_id, by = "id")
#Filter down the inner joined table with the two conditions and return the section_eur column
all_case <- all_case %>%
filter(wells_per_section == 5 & section_eur >55250, section_eur < 74750 )%>%
select(id, well_eur, section_eur, incr_eur)
Below is the function I'm trying to come up with but not really sure how to finish, in my example 5 is breakover, section_eur is 65000, tolerance is 15%, both breakover and wells_per_section will equal 5:
match <- function(breakover, section_eur, tolerance) {
find_id <- df %>% filter(breakover == breakover)%>%select()
}
CodePudding user response:
It is not entirely clear to me what you are looking for. The following function returns all records where the breakover = req_breakover
and section_eur
is within tolerance
of req_eur
. Hopefully this is sufficient for you to figure out the remaining steps.
match <- function(df, req_breakover, req_eur, tolerance) {
lower_bound = req_eur * (1 - tolerance)
upper_bound = req_eur * (1 tolerance)
find_id <- df %>%
filter(breakover == req_breakover) %>%
select(id)
all_case <- df %>%
inner_join(find_id, by = "id")
all_case <- all_case %>%
filter(section_eur > lower_bound ,
section_eur < upper_bound )%>%
select(id, well_eur, section_eur, incr_eur)
return(all_case)
}
You would call the function as follows:
output = match(model, 5, 65000, 0.15)
A more compact function would look like:
match <- function(df, req_breakover, req_eur, tolerance) {
output <- df %>%
filter(breakover == req_breakover
section_eur > req_eur * (1 - tolerance),
section_eur < req_eur * (1 tolerance))%>%
select(id, well_eur, section_eur, incr_eur)
Note that the return
command is unnecessary. I have included it for clarity.