which type of object to use for assigning several s4 class objects to it?

StackOverflow https://stackoverflow.com/questions/9895898

  •  27-05-2021
  •  | 
  •  

Pregunta

I'd like to do this:

library(party)

# cts <- ??? 

n <- c(50, 100, 116)

for(i in 1:length(n)) {
  data <- airq[1:n[i], ]
  airct <- ctree(Ozone ~ ., data)
  cts[i] <- airct
  }

..but don't know which type of object I can use for assigning the ctree results to.

Thanks for any pointers, Kay

¿Fue útil?

Solución

A list is usually the answer.

library(party)
airq <- na.omit(airquality) # Prunes NA rows down to 111 rows...

n <- c(50, 100, 111) # 116 is outside 
cts <- vector('list', length(n))

for(i in 1:length(n)) {
  data <- airq[1:n[i], ]
  airct <- ctree(Ozone ~ ., data)
  cts[[i]] <- airct
}

But a better way is to use lapply (list-apply) here. No for-loop required and a list is returned.

library(party)
airq <- na.omit(airquality) # Prunes NA rows down to 111 rows...
n <- c(50, 100, 111) # 116 is outside 
cts <- lapply(n, function(ni) ctree(Ozone ~ ., data=airq[1:ni,]))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top