xingxing
2011-Oct-12 20:28 UTC
[R] How to transfer the variable into function in this code?
as shown in the code, four variables input: diff_expr,diff_coex, klist, Pop_size, are included in the function. However, within the function, another variable "vector" is utilized in some subfunction. How they get its value? thx get_quantiles <-function(diff_expr,diff_coex,klist,pop_size){ ##### The function to get the "node_score_term" and "edge_score_term" of a sub-network denoted by "vector" my.fun<-function(vector){ return(diff_coex[vector[1],vector[2]]) } # "vector" is a binary vector with length equal to the size of the whole network. # An element of value "1" indicates the inclusion of that gene in the selected sub-network. node_edge<-function(vector){ selected_subset<-which(vector==1) n<-length(selected_subset) node_score<-sum(diff_expr[selected_subset])/sqrt(n) edges<-combn(selected_subset,2) edge_score<-sum(apply(edges,2,my.fun))/sqrt(choose(n,2)) return(c(node_score,edge_score)) } } -- View this message in context: http://r.789695.n4.nabble.com/How-to-transfer-the-variable-into-function-in-this-code-tp3899576p3899576.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2011-Oct-13 03:00 UTC
[R] How to transfer the variable into function in this code?
As provided, the function you give doesn't actually do anything, it only defines some subfunctions which are removed upon completion of the function call. If that were the actual function, you would be right to be confused, but the provided code doesn't give the proper context to the problem. You seem to be interested in the workings of get_quantiles() from the COSINE package. If you look at the whole code for the function, you'll see that the subfunctions are only defined at the beginning of get_quantiles() but are later called directly with specific arguments. Since the definitions don't require values and only use the name "vector" as a placeholder, the question of how a value is attained is actually put off till later in the function. Specifically, line 6 calls my.fun on each column of edges, which is merely a vector, while line 24 calls node_edge on the variable vector defined immediately preceeding. Does this clarify things? As an aside, I personally think it's not the best practice to use vector as an argument name, but here it's not a problem. Michael On Wed, Oct 12, 2011 at 4:28 PM, xingxing <zixingwang at hotmail.com> wrote:> as shown in the code, four variables input: diff_expr,diff_coex, klist, > Pop_size, are included in the function. However, within the function, > another variable "vector" is utilized in some subfunction. How they get its > value? > > thx > > get_quantiles <-function(diff_expr,diff_coex,klist,pop_size){ > > ##### The function to get the "node_score_term" and "edge_score_term" of a > sub-network denoted by "vector" > > my.fun<-function(vector){ > return(diff_coex[vector[1],vector[2]]) > } > > # "vector" is a binary vector with length equal to the size of the whole > network. > # An element of value "1" indicates the inclusion of that gene in the > selected sub-network. > > > node_edge<-function(vector){ > ? ?selected_subset<-which(vector==1) > ? ?n<-length(selected_subset) > ? ?node_score<-sum(diff_expr[selected_subset])/sqrt(n) > ? ?edges<-combn(selected_subset,2) > ? ?edge_score<-sum(apply(edges,2,my.fun))/sqrt(choose(n,2)) > ? ?return(c(node_score,edge_score)) > } > } > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-transfer-the-variable-into-function-in-this-code-tp3899576p3899576.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
xingxing
2011-Oct-13 15:09 UTC
[R] How to transfer the variable into function in this code?
I think you made it very clearly. thx -- View this message in context: http://r.789695.n4.nabble.com/How-to-transfer-the-variable-into-function-in-this-code-tp3899576p3901817.html Sent from the R help mailing list archive at Nabble.com.