Hello,

Try the following.

split(data.frame(dados), dados[, "code"])

Also, it's better to have data like 'dados' in a data.frame, like this you would have dates of class Date, and numbers of classes numeric or integer:


dados2 <- data.frame(dados)
dados2$date <- as.Date(dados2$date)
dados2$value <- as.numeric(dados2$value)
dados2$code <- as.integer(dados2$code)

#See the STRucture
str(dados2)

The code above would be simplified to split(dados2, dados2$code)

And it's also better to keep the result in a list, they are all in one place and you can access the components as

result[[ "433" ]]  # etc.

Hope this helps

Rui Barradas

Em 06-08-2012 18:06, Henrique Andrade escreveu:
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

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to