Re: [R] Splitting Data Into Different Series

2012-08-06 Thread Rui Barradas

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.


Re: [R] Splitting Data Into Different Series

2012-08-06 Thread Henrique Andrade
Dear Rui and Arun,

Thanks a lot for your help. I will test all the proposed solutions ;-)

Best regards,
Henrique Andrade

2012/8/6 Rui Barradas ruipbarra...@sapo.pt:
 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.





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


Re: [R] Splitting Data Into Different Series

2012-08-06 Thread arun
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.coe...@gmail.com
To: r-help@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@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.