[R] Splitting Data Into Different Series

Henrique Andrade henrique.coelho at gmail.com
Mon Aug 6 19:06:41 CEST 2012


Dear R Community,

I'm trying to write a loop to split my data into different series. I
need to make a
new matrix (or series) according to the series code.

For instance, every time the "code" column assumes the value "433" I need to
save "date", "value", and "code" into the "dados433" matrix.

Please take a look at the following example:

dados <- matrix(c("2012-01-01","2012-02-01","2012-03-01","2012-04-01","2012-05-01","2012-06-01",

"2012-01-01","2012-02-01","2012-03-01","2012-04-01","2012-05-01","2012-06-01",

"2012-01-01","2012-02-01","2012-03-01","2012-04-01","2012-05-01","2012-06-01",

0.56,0.45,0.21,0.64,0.36,0.08,152136,153081,155872,158356,162157,166226,

33.47,34.48,35.24,38.42,35.33,34.43,433,433,433,433,433,433,2005,2005,2005,
                        2005,2005,2005,3939,3939,3939,3939,3939,3939),
nrow=18, ncol=3, byrow=FALSE,

dimnames=list(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18),
                        c("date", "value", "code")))

dados433 <- matrix(data = NA, nrow = 6, ncol = 3, byrow= FALSE)
dados2005 <- matrix(data = NA, nrow = 6, ncol = 3, byrow= FALSE)
dados3939 <- matrix(data = NA, nrow = 6, ncol = 3, byrow= FALSE)

for(i in seq(along=dados[,3])) {
    if(dados[i,3] == 433) {dados433[i,1:3] <- dados[i,1:3]}
}

for(i in seq(along=dados[,3])) {
    if(dados[i,3] == 2005) {dados2005[i,1:3] <- dados[i,1:3]}
}

for(i in seq(along=dados[,3])) {
    if(dados[i,3] == 3939) {dados3939[i,1:3] <- dados[i,1:3]}
}

Best regards,
Henrique Andrade



More information about the R-help mailing list