Function parameters in R are passed by value, not by reference. In order to resolve it, just remove "clusters" from the parameter list, and use "clusters[i] <<- ..." to change the value of global variable.
On Wed, Jun 17, 2009 at 7:52 PM, Nick Angelou<nikola...@yahoo.com> wrote: > > Hi, > > I have a problem with dynamic appending to a list. Here is the list > variable: > > > clusters <- vector("list", 0) > > > I extended in the function below: > > > cluster <- function (pair, clusters) > { > found <- FALSE > for (i in length(clusters)) > { > if (length(intersect(pair, clusters[i])) > 0) > { > clusters[i] <- union(clusters[i], pair) > found <- TRUE > } > } > if (!found) > { > clusters <- list(clusters, as.vector(pair)) > } > } > > > The function is executed in a loop: > > > for (i in 1:nrow(adjMatrix)) > { > for (j in 1:nrow(adjMatrix)) > { > if ((i != j) && adjMatrix[i,j] >0) # the matrix element has to be non-zero > in order to be clustered > { > cat(rownames(adjMatrix)[i], colnames(adjMatrix)[j], "\n") > cluster(as.vector(c(rownames(adjMatrix)[i], colnames(adjMatrix)[j])), > clusters) > } > } > } > > > But the list variable remains empty (i.e. length(clusters) = 0) even though > it should not. Somehow the dynamic extension of the list does not work in > this case. Any suggestions? > > Best regards, > > Nick > -- > View this message in context: > http://www.nabble.com/How-to-append-to-a-list-dynamically--tp24071794p24071794.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. > ______________________________________________ R-help@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.