[R] How to transfer the variable into function in this code?
R. Michael Weylandt
michael.weylandt at gmail.com
Thu Oct 13 05:00:45 CEST 2011
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.
>
More information about the R-help
mailing list