Condition in argument for a function

Hello Everyone,
I have a function final with arguments x and y. I would like to select y in such a way that months both x and y are equal.
For example:
In the first run, it would be x1+y1 and x2+y2.
Likewise, the 13th run will be x13+y1..... x14+y2.
I would be grateful if you can suggest ton the final code which tried as below.

Thanks

library(tidyverse)
dat <- data.frame("month"=01:12,
                  "mean_dat"=runif(12,1,15))
date1=seq(as.Date("2019/1/1"), as.Date("2019/12/31"), "days")
T_out=runif(length(date1),1,15)
output <- data.frame(date1,T_out)
output$mon=format(as.Date(output$date1, format="%Y-%m-%d"),"%m")
output$mon=as.numeric(output$mon)
head(output)
final<- function(x,y)x+y
(mapply (function(x, y) final(x, y),
        x=output$T_out, y=ifelse(dat$month==output$mon, dat$mean_dat,0) ))

I find the way you ask the question be quite abstract...
Would it be correct to think that you are trying to filter one dataframe of temperatures to keep only those records with close temperature values to the average temperature values (within month groups) as calculated from a second dataset ?

(putting aside for a moment the question of what you consider to be 'close')

Thanks @nirgrahamuk for prompt reply. I have updated the question and still find the warning

Warning message:
In dat$month == output$mon :
 longer object length is not a multiple of shorter object length

You want to calculate two sets of mean temperatures by month, and within each month, add the values together?

It's a simple addition for

output$T_out+dat$mean_dat which is x+y

Here, y needs to select for a given month as in X.

As in the attached picture, mean_dat changes with the months of the output dataset.

Final column will be my solution.

I dont understand what the red lines indicate but I'm assuming you want this

(joined <- left_join(output, dat,
                    by = c("mon" = "month")) %>% 
           mutate(y = T_out + mean_dat))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.