Home > Software design >  Make a loop function by including gender into the algorithm
Make a loop function by including gender into the algorithm

Time:11-04

I have the following data set:

Age<-c(2,2.1,2.2,3.4,3.5,4.2,4.7,4.8,5,5.6,NA, 5.9, NA)
R<-c(2,2.1,2.2,3.4,3.5,4.2,4.7,4.8,5,5.6,NA, 5.9, NA)
sex<-c(1,0,1,1,1,1,1,0,0,0,NA, 0,1)
df1<-data.frame(Age,R,sex)

# Second dataset:
Age2<-seq(2,20,0.25)    
Mspline<-rnorm(73)
df2.F<-data.frame(Age2, Mspline)

# Third data
Age2<-seq(2,20,0.25)    
Mspline<-rnorm(73)
df2.M<-data.frame(Age2, Mspline)

I was wondering how I can include gender into the calculation and combine these two algorithm to make a loop function. What I need is:

If sex=1 then use the following function to calculate Time

last = dim(df2.F)[1]
fM.F<-approxfun(df2.F$Age2, df2.F$Mspline, yleft = df2.F$Mspline[1] , yright = df2.F$Mspline[last])
df1$Time<-fM.F(df1$Age)

and If sex=0 then use this function to calculate Time

last = dim(df2.M)[1]
fM.M<-approxfun(df2.M$Age2, df2.M$Mspline, yleft = df2.M$Mspline[1] , yright = df2.M$Mspline[last])
df1$Time<-fM.M(df1$Age)

I mean: Read the first record in df1 if it is Female (with age=4.1) the time=fM.F(its age=4.1) but if the gender is Male then to calculate Time apply fM.M on its age so time=fM.M(4.1)

CodePudding user response:

You can create a function that takes the Age vector, the sex value, and the male and female specific dataframes, and selects the frame to use based on the sex value.

f <- function(age, s, m,f) {
  if(is.na(s)) return(NA)

  if(s==0) df = m
  else df = f
  
  last = dim(df)[1]
  fM<-approxfun(df$Age2, df$Mspline, yleft = df$Mspline[1] , yright = df$Mspline[last])
  fM(age)
}

Now, just apply the function by group, using pull(cur_group(),sex) to get the sex value for the current group.

library(dplyr)

df1 %>% 
  group_by(sex) %>% 
  mutate(time = f(Age, pull(cur_group(),sex), df2.M, df2.F))

Output:

     Age     R   sex   time
   <dbl> <dbl> <dbl>  <dbl>
 1   2     2       1 -0.186
 2   2.1   2.1     0  1.02 
 3   2.2   2.2     1 -1.55 
 4   3.4   3.4     1 -0.461
 5   3.5   3.5     1  0.342
 6   4.2   4.2     1 -0.560
 7   4.7   4.7     1 -0.114
 8   4.8   4.8     0  0.247
 9   5     5       0 -0.510
10   5.6   5.6     0 -0.982
11  NA    NA      NA NA    
12   5.9   5.9     0 -0.231
13  NA    NA       1 NA    
  • Related