Skip to content
Prev 300161 / 398503 Next

Loading in Large Dataset + variables via loop

Hello,

Why do you need 9 variables in your environment if they are time series 
that correspond to the same period? You should use time series functions.

#install.packages('zoo')
library(zoo)

# Make up a dataset
Year <- seq(from=as.Date("1901-01-01"), by="year", length.out=100)
dat <- data.frame(matrix(rnorm(100*9), ncol=9), Year)

# assign names.
varNames <- expand.grid(c("temp", "precip", "pressure"), 1:3, 
stringsAsFactors=FALSE)
varNames <- as.vector(apply(colNames, 1, paste, collapse=""))
varNames <- c(varNames, "Year")
names(dat) <- varNames
head(dat)

# and transform it into a time series of class 'zoo'
z <- zoo(dat[, 1:9], order.by=dat$Year)
str(z)
head(z)


Another way would be, like you say, to use a loop to put the variables 
in a list. Something like

lst <- list()
for(i in 1:9) lst[[i]] <- dat[, i]
names(lst) <- varNames


Note that I've used a dataset called 'dat' n place of your 'A'. You 
should post a data example, like the posting guide says. Using dput().

Hope this helps,

Rui Barradas



Em 14-07-2012 03:44, cmc0605 escreveu: