[R] How to append to a list dynamically?
Nick Angelou
nikolay12 at yahoo.com
Wed Jun 17 13:52:24 CEST 2009
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.
More information about the R-help
mailing list