[R] Splitting Data Into Different Series

2012-08-06 Thread Henrique Andrade
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.


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


[R] Saving Splitted Series to Excel via XLConnect

2012-08-08 Thread Henrique Andrade
Dear R Discussion List,

I would like to save my data as a xlsx file. But at first
I need to split it and then save each series into a Excel
column. Please take a look at the following code:

dados <- data.frame(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"

dados2 <- split(dados, dados$code)
dados2

library(XLConnect)

wb <- loadWorkbook("Teste.xlsx", create = TRUE)
createSheet(wb, name = "Teste1")
writeWorksheet(wb, dados2, sheet = "Teste1")
saveWorkbook(wb)

With this code I only get the "433" series. How could I
fix my code? How could I include the other series?

Many thanks in advance,
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] Saving Splitted Series to Excel via XLConnect

2012-08-08 Thread Henrique Andrade
Dear Arun,

Thanks a lot for your help, but I really need to save the splitted
data in a Excel file (xlsx or xls). This is because I will need to
make links between different Excel workbooks.

Best regards,
Henrique Andrade

Enviado via iPhone

Em 08/08/2012, às 17:25, arun  escreveu:

> Hi,
>
> I think saving it as .csv file will be much easier.  I am getting all the 
> series in the wide format.
>
> write.csv(dados2,"dados2.csv")
>
>
> X2005.date X2005.value X2005.code X3939.date X3939.value X3939.code X433.date 
> X433.value X433.code
> 7 2012-01-01 152136 2005 2012-01-01 33.47 3939 2012-01-01 0.56 433
> 8 2012-02-01 153081 2005 2012-02-01 34.48 3939 2012-02-01 0.45 433
> 9 2012-03-01 155872 2005 2012-03-01 35.24 3939 2012-03-01 0.21 433
> 10 2012-04-01 158356 2005 2012-04-01 38.42 3939 2012-04-01 0.64 433
> 11 2012-05-01 162157 2005 2012-05-01 35.33 3939 2012-05-01 0.36 433
> 12 2012-06-01 166226 2005 2012-06-01 34.43 3939 2012-06-01 0.08 433
>
> A.K.
>
>
>
> - Original Message -
> From: Henrique Andrade 
> To: R Discussion List 
> Cc:
> Sent: Wednesday, August 8, 2012 4:10 PM
> Subject: [R] Saving Splitted Series to Excel via XLConnect
>
> Dear R Discussion List,
>
> I would like to save my data as a xlsx file. But at first
> I need to split it and then save each series into a Excel
> column. Please take a look at the following code:
>
> dados <- data.frame(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"
>
> dados2 <- split(dados, dados$code)
> dados2
>
> library(XLConnect)
>
> wb <- loadWorkbook("Teste.xlsx", create = TRUE)
> createSheet(wb, name = "Teste1")
> writeWorksheet(wb, dados2, sheet = "Teste1")
> saveWorkbook(wb)
>
> With this code I only get the "433" series. How could I
> fix my code? How could I include the other series?
>
> Many thanks in advance,
> 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] Saving Splitted Series to Excel via XLConnect

2012-08-08 Thread Henrique Andrade
Dear Rui and David,

Thanks a lot for your help and advices. Now finally I have
what I want ;-) The final code looks like this:



dados <- data.frame(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"

dados2 <- split(dados, dados$code)

library(XLConnect)

wb <- loadWorkbook("Henrique.xlsx", create = TRUE)
series <- seq_along(dados2)
createSheet(wb, name = "Planilha")

lapply(series, function(i){
column <- (3*i) - 2
writeWorksheet(wb, dados2[[i]], sheet = "Planilha",
startCol = column)})
saveWorkbook(wb)



I did not use the option to build a new sheet for each "i"
because I have more than 200 objects in my real problem.

Again, many thanks to you guys!

Best regards (or "Um abraço"),
Henrique Andrade


2012/8/8 Rui Barradas :
> Hello,
>
> First of all, apologies to Henrique, he'll receive th same answer twice, but
> I forgot to Cc the list.
>
>
> In order to write 3 worksheets you need to create 3 worksheets. What
> happened is that you were overwriting the previous sheets and ended up just
> with the last one. So adopt a different method: lapply().
>
>
> wb <- loadWorkbook("Teste.xlsx", create = TRUE)
>
> series <- seq_along(dados2)
> sheet <- paste0("Teste", series)
> lapply(series, function(i){
> createSheet(wb, name = sheet[i])
> writeWorksheet(wb, dados2[[i]], sheet = sheet[i])})
>
> saveWorkbook(wb)
>
> This worked with me.
>
> Hope this helps,
>
> Rui Barradas
> Em 08-08-2012 21:10, Henrique Andrade escreveu:
>>
>> Dear R Discussion List,
>>
>>
>> I would like to save my data as a xlsx file. But at first
>> I need to split it and then save each series into a Excel
>> column. Please take a look at the following code:
>>
>> dados <- data.frame(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"
>>
>> dados2 <- split(dados, dados$code)
>> dados2
>>
>> library(XLConnect)
>>
>> wb <- loadWorkbook("Teste.xlsx", create = TRUE)
>> createSheet(wb, name = "Teste1")
>> writeWorksheet(wb, dados2, sheet = "Teste1")
>> saveWorkbook(wb)
>>
>> With this code I only get the "433" series. How could I
>> fix my code? How could I include the other series?
>>
>> Many thanks in advance,
>> 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.