合并 R 中嵌套函数中函数的输出

merging the outputs of functions within nested function in R

我有 2 个函数,每个函数都提供不同的输出,我试图创建一个新函数来合并这些函数的 2 个输出,但一直收到错误消息说找不到对象,我明白一旦我在我的任何功能(在一般功能内部)之外,主功能不识别这些对象。我不知道如何使这些输出在主要功能的全局环境中可识别。这是代码

#############################################################################
#############################################################################
# 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

这里是main函数,有很多错误

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

我不知道是否有其他方法可以在 main 函数中使用函数的输出。预先感谢您的帮助

为什么要将所有功能合二为一?我建议将它们分开并写入 test.merge 以仅合并来自 2 个输出的数据。

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

这是对您的原始代码的修复。

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])

我相信这可以优化并以更好的方式编写(如第 1 部分中所建议的),但由于我不知道这里的大局,我将把它留给 OP。