Janet
Janet

Reputation: 225

merging the outputs of functions within nested function in R

I have 2 functions that each gives a different output, i was trying to create a new function that merge the 2 outputs of theses functions, but keep getting an error saying the object is not found, i understand that once i am out of any of my functions (inside of the general function), the main function does not recognize these objects. I do not know how to make these outputs recognizable in the global environment for the main function. Here is the code

#############################################################################
#############################################################################
# 1. datasets 
IDr= c(seq(1,5))
BTR=c("A","B","AB","O","O")
data_R=data.frame(IDr,BTR,A=c(0,1,rep(0,3)),B=c(0,rep(0,3),1),C=c(0,rep(1,3),0),D=c(0,rep(1,4)),E=c(1,1,0,rep(1,1),0),stringsAsFactors=FALSE)

 data_R
  IDr BTR A B C D E
1   1   A 0 0 0 0 1
2   2   B 1 0 1 1 1
3   3  AB 0 0 1 1 0
4   4   O 0 0 1 1 1
5   5   O 0 1 0 1 0


IDd= c(seq(1,8))
BTD= c("A","B","AB","O","AB","AB","O","O")
fg= c(rep(0.0025, each=2),rep(0.00125, each=2),rep(0.0011, each=2),rep(0.0015, each=2))
data_D=data.frame(IDd,BTD,A=c(rep(0,5),1,1,1),B=c(rep(0,6),1,1),C=c(rep(1,7),0),D=rep(1,8),E=c(rep(0,5),rep(1,2),0),fg,stringsAsFactors=FALSE)

  data_D
  IDd BTD A B C D E      fg
1   1   A 0 0 1 1 0 0.00250
2   2   B 0 0 1 1 0 0.00250
3   3  AB 0 0 1 1 0 0.00125
4   4   O 0 0 1 1 0 0.00125
5   5  AB 0 0 1 1 0 0.00110
6   6  AB 1 0 1 1 1 0.00110
7   7   O 1 1 1 1 1 0.00150
8   8   O 1 1 0 1 0 0.00150
############################################################################
############################################################################
# fist function
# calulate the frequency of repeated set (A:E) using fg 
freq<- function(df, Vars,col.interest){
  col.interest=as.data.frame(col.interest)
  resultat1= df  %>% 
    group_by(across(all_of(Vars))) %>%
    dplyr::summarise(count = n(), frequency.epi = sum(fg), .groups = 'drop')
  res=merge(resultat1,col.interest,all=TRUE)
  res_final=cbind(df[1:2],res)
  return(res_final)
  
}

dfreq= freq(data_D,colnames(data_D)[3:7],data_D[3:7])
dfreq
  IDd BTD A B C D E count frequency.epi
1   1   A 0 0 1 1 0     5        0.0086
2   2   B 0 0 1 1 0     5        0.0086
3   3  AB 0 0 1 1 0     5        0.0086
4   4   O 0 0 1 1 0     5        0.0086
5   5  AB 0 0 1 1 0     5        0.0086
6   6  AB 1 0 1 1 1     1        0.0011
7   7   O 1 1 0 1 0     1        0.0015
8   8   O 1 1 1 1 1     1        0.0015
###############################################################
# the second function that was corrected by @MrFlic 
 mis.test = function(D, R, threshold) { 
  D = as.data.frame(D)
  R = as.data.frame(R)
  mismatch.i = function(i) {
    dif = purrr::map2_df(D[-1], R[i,-1], `-`)
    dif[dif<0] = 0
    dif$mismatch=rowSums(dif)
    dif = cbind(ID = D[1],IDr=R[i,1], dif)
    dif = dif[which(dif$mismatch <= threshold),]
    return(list=dif[c(1,2,ncol(dif))])
  }
  
  diff.mat = do.call(rbind, lapply(1:nrow(R), function(x) mismatch.i(x)))
  diff.mat = as.data.frame(diff.mat)
  return(diff.mat)
}
# if i want mis.test for 1 person 
mis_one=mis.test(data_D[,c(1,3:7)],data_R[1,c(1,3:7)],2)
mis_one
  IDd IDr mismatch
1   1   1        2
2   2   1        2
3   3   1        2
4   4   1        2
5   5   1        2

# what i want to do in the main function is this step (for example using these exact outputs)
merge(mis_one,dfreq,by="IDd") # this was executed outside to show the expected output
# this is the output expected that i want if i run the main function 

  IDd IDr mismatch BTD A B C D E count frequency.epi
1   1   1        2   A 0 0 1 1 0     5        0.0086
2   2   1        2   B 0 0 1 1 0     5        0.0086
3   3   1        2  AB 0 0 1 1 0     5        0.0086
4   4   1        2   O 0 0 1 1 0     5        0.0086
5   5   1        2  AB 0 0 1 1 0     5        0.0086

Here is the main function, with many errors

test.merge=function(D,DF,R,threshold,Vars,col.interest){
  R=as.data.frame(R)
  D=as.data.frame(D)
  DF=as.data.frame(DF)
  col.interest=as.data.frame(col.interest)
  # remark1: Here i know i repeated the same arguments because i did not know what to set in order to do the calculation
  freq.epi<- function( Vars,col.interest){
    resultat1= DF  %>% 
      group_by(across(all_of(Vars))) %>%
      dplyr::summarise(count = n(), frequency.epi = sum(fg), .groups = 'drop')
    res=merge(resultat1,col.interest,all=TRUE)
    res_final=cbind(DF[1:2],res)
    return(res_final)
    
  }
  # same as remark1 for the arguments
  mis.test = function(D, R, threshold) { 
    D = as.data.frame(D)
    R = as.data.frame(R)
    mismatch.i = function(i) {
      dif = purrr::map2_df(D[-1], R[i,-1], `-`)
      dif[dif<0] = 0
      dif$mismatch=rowSums(dif)
      dif = cbind(ID = D[1],IDr=R[i,1], dif)
      dif = dif[which(dif$mismatch <= threshold),]
      return(list=dif[c(1,2,ncol(dif))])
    }
    diff.mat = do.call(rbind, lapply(1:nrow(R), function(x) mismatch.i(x)))
    diff.mat = as.data.frame(diff.mat)
    return(diff.mat)
  }
  # i dont know how to make diff.mat and res_final visible for test.merge
  # i am trying to merge the two outputs res_final and diff.mat by the IDd
  tab=merge(diff.mat,res_final,by="IDd")
  return(tab)
  
}

test.merge(data_D[,c(1,3:7)],data_D,data_R[1,c(1,3:7)],2,colnames(data_D)[3:7],data_D[3:7])
# Error in merge(diff.mat, res_final, by = "IDd") : 
# object 'diff.mat' not found

I dont know if there is other ways to use the outputs of functions within the main function. Thank you in advance for your help

Upvotes: 1

Views: 344

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389055

Why do you want to mix all the functions into one? I would suggest to keep them separate and write test.merge to only merge data from 2 outputs.

freq<- function(df, Vars,col.interest){
  col.interest=as.data.frame(col.interest)
  resultat1= df  %>% 
    group_by(across(all_of(Vars))) %>%
    dplyr::summarise(count = n(), frequency.epi = sum(fg), .groups = 'drop')
  res=merge(resultat1,col.interest,all=TRUE)
  res_final=cbind(df[1:2],res)
  return(res_final)
  
}

mis.test = function(D, R, threshold) { 
  D = as.data.frame(D)
  R = as.data.frame(R)
  mismatch.i = function(i) {
    dif = purrr::map2_df(D[-1], R[i,-1], `-`)
    dif[dif<0] = 0
    dif$mismatch=rowSums(dif)
    dif = cbind(ID = D[1],IDr=R[i,1], dif)
    dif = dif[which(dif$mismatch <= threshold),]
    return(list=dif[c(1,2,ncol(dif))])
  }
  
  diff.mat = do.call(rbind, lapply(1:nrow(R), function(x) mismatch.i(x)))
  diff.mat = as.data.frame(diff.mat)
  return(diff.mat)
}

test.merge = function(x, y) {
  merge(x,y,by="IDd")
}
test.merge(mis.test(data_D[,c(1,3:7)],data_R[1,c(1,3:7)],2), 
           freq(data_D,colnames(data_D)[3:7],data_D[3:7]))

#  IDd IDr mismatch BTD A B C D E count frequency.epi
#1   1   1        2   A 0 0 1 1 0     5        0.0086
#2   2   1        2   B 0 0 1 1 0     5        0.0086
#3   3   1        2  AB 0 0 1 1 0     5        0.0086
#4   4   1        2   O 0 0 1 1 0     5        0.0086
#5   5   1        2  AB 0 0 1 1 0     5        0.0086

And here is the fix to your original code.

test.merge=function(D,R,threshold,DF, Vars,col.interest){
  R=as.data.frame(R)
  D=as.data.frame(D)
  DF=as.data.frame(DF)
  col.interest=as.data.frame(col.interest)
 
  freq.epi<- function(DF, Vars,col.interest){
    resultat1= DF  %>% 
      group_by(across(all_of(Vars))) %>%
      dplyr::summarise(count = n(), frequency.epi = sum(fg), .groups = 'drop')
    res=merge(resultat1,col.interest,all=TRUE)
    res_final=cbind(DF[1:2],res)
    return(res_final)
    
  }
  # same as remark1 for the arguments
  mis.test = function(D, R, threshold) { 
    D = as.data.frame(D)
    R = as.data.frame(R)
    mismatch.i = function(i) {
      dif = purrr::map2_df(D[-1], R[i,-1], `-`)
      dif[dif<0] = 0
      dif$mismatch=rowSums(dif)
      dif = cbind(ID = D[1],IDr=R[i,1], dif)
      dif = dif[which(dif$mismatch <= threshold),]
      return(list=dif[c(1,2,ncol(dif))])
    }
    diff.mat = do.call(rbind, lapply(1:nrow(R), function(x) mismatch.i(x)))
    diff.mat = as.data.frame(diff.mat)
    return(diff.mat)
  }
  
  tab=merge(mis.test(D, R, threshold),freq.epi(DF, Vars, col.interest),by="IDd")
  return(tab)
  
}

test.merge(data_D[,c(1,3:7)],data_R[1,c(1,3:7)],2,data_D, colnames(data_D)[3:7],data_D[3:7])

I am sure this could be optimised and written in a better way (as suggested in 1st part) but since I don't know the bigger picture here I'll leave this to OP.

Upvotes: 2

Related Questions