[R] Loading in Large Dataset + variables via loop

Rui Barradas ruipbarradas at sapo.pt
Sun Jul 15 14:12:54 CEST 2012


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.


# 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, 
varNames <- as.vector(apply(colNames, 1, paste, collapse=""))
varNames <- c(varNames, "Year")
names(dat) <- varNames

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

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:
> Hello, I'm new to R with a (probably elementary) question.
> Suppose I have a dataset called /A/ with /n/ locations, and each location
> contains within it 3 time series of different variables (all of 100 years
> length); each time series is of a weather variable (for each location there
> is a temperature, precipitation, and pressure).  For instance, location 1
> has a temperature1 time series, a precip1 time series, and a pressure1 time
> series; location two has a temperature2, precip2, and pressure2
> timeseries...That is, there are 100 rows, and (/n/*3)+1 columns.  The extra
> column is the time.
> I want to load in this dataset and declare a variable for each time series.
> The columns are in order of location, so it goes temp1, precip1,pressure1,
> temp2,... and so forth in increasing column order.  There are always 100
> rows.  Manually, Id have to do:
> temp1=A[,1]
> precip1=A[,2]
> pressure1=A[,3]
> temp2=A[,4]
> precip2=A[,5]
> pressure2=A[,6]
> temp3=A[,7]
> and so forth.....
> Problem is, n is large, so I don't want to repeat this pattern forever.  I
> figure I need a loop both for the variable name (ie.., the variable at a
> particular location) as well as for what column it reads from.
> Any help...?
> --
> View this message in context: http://r.789695.n4.nabble.com/Loading-in-Large-Dataset-variables-via-loop-tp4636501.html
> Sent from the R help mailing list archive at Nabble.com.
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list