简体   繁体   English

R中的嵌套函数-问题-

[英]Nested functions in R - Issues-

i am editing my question with the functions i am working with.我正在使用我正在使用的功能编辑我的问题。 I have two functions: the first one does the calculation (difference between 2 datasets but only for one entry in the first dataset), the second does the calculation for the whole dataset 1. I wanted to create a new function that can give me the choice to whether do the calculation for one entry or the whole dataframe.我有两个函数:第一个函数进行计算(两个数据集之间的差异,但仅针对第一个数据集中的一个条目),第二个函数执行整个数据集 1 的计算。我想创建一个新函数,可以给我选择是对一个条目还是整个数据框进行计算。

# Sample Data
data_R <- data.frame(
  IDr= c(seq(1,5)),
  BTR= c("A","B","AB","O","O"),
  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

data_D <- data.frame(
  IDd = c(seq(1,8)),
  BTD = c("A","B","AB","O","AB","AB","O","O"),
  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=c(rep(0.0025, each=2),rep(0.00125, each=2),rep(0.0011, each=2),rep(0.0015, each=2)),
  stringsAsFactors=FALSE)
data_D

And here are the functions这是功能

# first function 
# difference for one patient
mismatch.i = function(D, R, i, threshold) {
  D = as.data.frame(D)
  R = as.data.frame(R)
  dif = purrr::map2_df(D[-1], R[i,-1], `-`)
  dif[dif<0] = 0
  dif$mismatch=rowSums(dif)
  dif = cbind(ID = D[1],R[i,1], dif)
  dif = dif[which(dif$mismatch <= threshold),]
  return(list=dif[c(1,2,ncol(dif))])
}

# the second function
# difference for the whole data frame data_R
mismtach.matrice <- function(D,R,threshold){ 
  D = as.matrix(D)
  R = as.matrix(R)
  diff.mat = do.call(rbind, lapply(1:nrow(R), function(x) mismatch.i(D,R,x,threshold)))
  diff.mat = as.data.frame(diff.mat)
  return(diff.mat)
}

And here's an example of running the functions这是运行函数的示例

mis.i = mismatch.i(data_D[,c(1,3:7)], data_R[,c(1,3:7)], 1, 4)
mis.i
  IDd R[i, 1] mismatch
1   1       1        2
2   2       1        2
3   3       1        2
4   4       1        2
5   5       1        2
6   6       1        3
7   7       1        4
8   8       1        3


mis.whole = mismtach.matrice(data_D[,c(1,3:7)], data_R[,c(1,3:7)], 4)
mis.whole
    IDd R[i, 1] mismatch
1    1       1        2
2    2       1        2
3    3       1        2
4    4       1        2
5    5       1        2
6    6       1        3
7    7       1        4
8    8       1        3
9    1       2        0
10   2       2        0
11   3       2        0
12   4       2        0
13   5       2        0
14   6       2        0
15   7       2        1
16   8       2        1
17   1       3        0
18   2       3        0
19   3       3        0
20   4       3        0
21   5       3        0
22   6       3        2
23   7       3        3
24   8       3        2
25   1       4        0
26   2       4        0
27   3       4        0
28   4       4        0
29   5       4        0
30   6       4        1
31   7       4        2
32   8       4        2
33   1       5        1
34   2       5        1
35   3       5        1
36   4       5        1
37   5       5        1
38   6       5        3
39   7       5        3
40   8       5        1

I tried to include the first function in the 2nd one, here is what i did and i get an error because obviously i don't understand how nested functions work.我试图在第二个函数中包含第一个函数,这是我所做的,但出现错误,因为显然我不明白嵌套函数是如何工作的。

# in this main function D, R and Threshold should remain as arguments
mis.test = function(D, R, threshold) { 
  D = as.data.frame(D)
  R = as.data.frame(R)
  mismatch.i = function(D, R, i, threshold) {
    dif = purrr::map2_df(D[-1], R[i,-1], `-`)
    dif[dif<0] = 0
    dif$mismatch=rowSums(dif)
    dif = cbind(ID = D[1],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), mismatch.i(x)))
  diff.mat = as.data.frame(diff.mat)
  return(diff.mat)
}
mis.test(data_D[,c(1,3:7)],data_R[,c(1,3:7)],4)
#  Error in mismatch.i(x) : object 'x' not found

I want to be able to run this function with either 1 entry in data_R or the whole data frame.我希望能够使用data_R 1 个条目或整个数据帧运行此函数。 If i run mis.test(data_D[,c(1,3:7)],data_R[1,c(1,3:7)],4) i would get the result of mis.i and if i run mis.test(data_D[,c(1,3:7)],data_R[,c(1,3:7)],4) i would get the result of mis.whole .如果我运行mis.test(data_D[,c(1,3:7)],data_R[1,c(1,3:7)],4)我会得到mis.i的结果,如果我运行mis.test(data_D[,c(1,3:7)],data_R[,c(1,3:7)],4)我会得到mis.whole的结果。 I hope it is clear, thank you in advance for your help.我希望它很清楚,在此先感谢您的帮助。

Your lapply is a bit off.你的lapply有点lapply了。 You need to pass in a function.您需要传入一个函数。 Right now you are attempting to call mismatch.i(x) and x isn't defined anywhere.现在您正在尝试调用mismatch.i(x)并且x未在任何地方定义。 Plus you defined mismatch.i to have additional parameters that you are not passing.另外,您定义了mismatch.i以具有您未传递的其他参数。 It should look like它应该看起来像

diff.mat = do.call(rbind, lapply(1:nrow(R), function(x) mismatch.i(D, R, x, threshold)))

Here we clearly make a function that lapply can call and pass the value of x to the i= parameter and pass along the result of the values.这里我们明确地做了一个函数, lapply可以调用并将x的值传递给i=参数并传递值的结果。

Since it is a nested function, you could also leave out the redundant parmaters from the inner function (since they will never change) So you could do由于它是一个嵌套函数,因此您也可以从内部函数中删除多余的参数(因为它们永远不会改变)所以您可以这样做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM