简体   繁体   中英

Keep assigned objects in workspace through a function

I am trying to keep an assigned object from a function (building a ts function to begin to model a univariate process, simple I know!). I am having trouble finding a method to keep objects in my workspace. It works fine just using a for loop but I would like to parameterize the following:

ts.builder<-function(x,y,z){
  for(i in 9:13){
    assign(paste(x,i,sep="_"),ts(yardstick[1:528,i], freq=24))
    assign(paste(y,i,sep="_"),ts(yardstick[529:552,i], freq=24))
    assign(paste(z,i,sep="_"),ts(yardstick[1:552,i], freq=24))
  }
}

ts.builder("yard.book.training","yard.book.small.valid", "yard.book.valid")

Any pointers? I am thinking it may need a return statement, yet I have not found this to be of use yet.

Untested (a reproducible example helps a lot):

ts.builder <- function() {
  xd <- list()
  yd <- list()
  zd <- list()

  for (i in 9:13) {
    xd[[i]] <- ts(yardstick[1:528,i], freq=24)
    yd[[i]] <- ts(yardstick[529:552,i], freq=24)
    zd[[i]] <- ts(yardstick[1:552,i], freq=24)
  }
  list(yard.book.training=xd, yard.book.small.valid=yd, yard.book.valid=zd)
}

l <- ts.builder()

Then here are the returned values:

l$yard.book.training[[9]]

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM