[R] Splitting Data Into Different Series

arun smartpink111 at yahoo.com
Mon Aug 6 20:10:29 CEST 2012


HI,
You can subset the data


 dados433<-subset(dados,dados[,3]=="433")
 is.matrix(dados433)
#[1] TRUE
 dados433
  date         value  code 
1 "2012-01-01" "0.56" "433"
2 "2012-02-01" "0.45" "433"
3 "2012-03-01" "0.21" "433"
4 "2012-04-01" "0.64" "433"
5 "2012-05-01" "0.36" "433"
6 "2012-06-01" "0.08" "433"

dados2005<-subset(dados,dados[,3]=="2005")
dados3939<-subset(dados,dados[,3]=="3939")

#or split the data

dados1<-as.data.frame(dados)
dados2<-split(dados1,dados1$code)



----- Original Message -----
From: Henrique Andrade <henrique.coelho at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, August 6, 2012 1:06 PM
Subject: [R] Splitting Data Into Different Series

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 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