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

在R中的嵌套函数中合并函数的输出,r,merge,output,nested-function,R,Merge,Output,Nested Function,我有两个函数,每个函数都有不同的输出,我试图创建一个新函数来合并这些函数的两个输出,但是不断得到一个错误,说找不到对象,我知道一旦我离开了我的任何函数(在通用函数内),主函数就无法识别这些对象。我不知道如何使这些输出在主要功能的全球环境中可识别。这是密码 ############################################################################# ############################################

我有两个函数,每个函数都有不同的输出,我试图创建一个新函数来合并这些函数的两个输出,但是不断得到一个错误,说找不到对象,我知道一旦我离开了我的任何函数(在通用函数内),主函数就无法识别这些对象。我不知道如何使这些输出在主要功能的全球环境中可识别。这是密码

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

#############################################################################
#############################################################################
# 1. 数据集
IDr=c(序号(1,5))
BTR=c(“A”、“B”、“AB”、“O”、“O”)
数据=数据帧(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)
数据
IDr BTR A B C D E
111A001
2 2 B 1 0 1 1 1
3 AB 0 0 1 1 0
440011
0501010
IDd=c(序号(1,8))
BTD=c(“A”、“B”、“AB”、“O”、“AB”、“AB”、“AB”、“O”、“O”)
fg=c(代表(0.0025,各=2)、代表(0.00125,各=2)、代表(0.0011,各=2)、代表(0.0015,各=2))
数据=数据帧(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)
数据
国际直拨电话BTD A B C D E fg
11A00100.00250
2 B 0 1 0 0.00250
3 AB 0 0 1 1 0 0.00125
4 0 0 1 1 0 0.00125
5 AB 0 0 1 1 0 0.00110
6 AB 10 1 1 0.00110
7 0.00150
8 8 O 1 0 1 0.00150
############################################################################
############################################################################
#拳头功能
#使用fg计算重复设置(A:E)的频率
频率%
分组依据(跨所有变量))%>%
dplyr::summary(count=n(),frequency.epi=sum(fg),.groups='drop')
res=merge(result1,col.interest,all=TRUE)
res_final=cbind(df[1:2],res)
返回(res_final)
}
dfreq=freq(data\u D,colnames(data\u D)[3:7],data\u D[3:7])
dfreq
IDd BTD A B C D E计数频率.epi
111A0101050.0086
2B0011050.0086
3 AB 0 0 1 0 5 0.0086
440011050.0086
5 AB 0 0 1 0 5 0.0086
6 AB 10 1 1 0.0011
7 7 O 1 0 1 0.0015
8 8 O 1 0.0015
###############################################################
#由@MrFlic更正的第二个函数
mis.test=函数(D,R,阈值){
D=as.data.frame(D)
R=as.data.frame(R)
不匹配。i=功能(i){
dif=purrr::map2_-df(D[-1],R[i,-1],`-`)

dif[dif为什么要将所有函数混合到一个函数中?我建议将它们分开并编写
test.merge
以仅合并来自两个输出的数据

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
freq%
分组依据(跨所有变量))%>%
dplyr::summary(count=n(),frequency.epi=sum(fg),.groups='drop')
res=merge(result1,col.interest,all=TRUE)
res_final=cbind(df[1:2],res)
返回(res_final)
}
mis.test=函数(D,R,阈值){
D=as.data.frame(D)
R=as.data.frame(R)
不匹配。i=功能(i){
dif=purrr::map2_-df(D[-1],R[i,-1],`-`)

dif[dif
mis.test
freq.epi
没有被调用。你必须在
test.merge
@Rui中调用它们,我在
merge
中调用它们吗?就像
merge(mis.test,freq.epi)那样
?@Rui你的意思是在
合并
步骤之前,我可以这样称呼他们:
a=mis.test
b=freq.epi
然后
合并…
?我很抱歉我的愚蠢的问题,但我不知道在哪里称呼他们实际上@Ronak我只是没有想到它(有点愚蠢!).我会按照你的建议做,保持简单。但我仍然想知道是否有一种方法可以在通用函数中使用函数的输出(可能是另一个简单的例子)。因为我有很多类似的功能,我必须合并,我不知道这是否是对所有这些功能进行合并的最佳方式。有什么想法吗?谢谢you@Janet对不起,我错过了消息。我不确定我是否理解,但您可以调用主函数中的
mis.test
freq
函数?我的问题是,而不是cr正在使用一个新函数
test.merge
在哪里可以调用输出(在我的情况下
diff.mat
resu final
)。你的意思是在主函数中的步骤
合并
之前调用
mis.test
freq
?请@Ronak举例说明一下好吗?谢谢@Ronak的更新,我现在明白了如何使用这些输出。我很感激。
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])