Reputation: 16081
I am new to R and most of my experience is in Java. I am trying to do the following:
clusterWeeks <- function()
{
kmV = list() #a list of kmeans objects from each week
for(i in 1:5)
{
windows()
kmV.append(clusterData(i)) #clusterData(i) returns a kmeans object
}
}
For some reason this does not work. I would like to then be able to access the objects via kmV[1], kmV[2], ... kmV[5]
Upvotes: 0
Views: 231
Reputation: 7469
Based on your code, here is what I would do:
clusterWeeks <- function(){
kmV <- c()
for(i in 1:5)
{
kmV <- c(kmV, i)
}
return(kmV)
}
small example:
test <- clusterWeeks()
test[2]
2
Upvotes: 0
Reputation: 263481
You need to create a list to hold each separate object. Otherwise you are just overwriting and returning only the last one:
clusterWeeks <- function()
{
kmV = vector("list", 5) # a 5 element list of empty items
for(i in 1:5)
{
windows()
kmV[[i]] <- clusterData(i) #clusterData(i) returns a kmeans object
}
}
You are obviously coming from a language where object.function
is an acceptable syntax, but that does not work in R.
Upvotes: 3