[R] Help with the 'reshape' package
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Aug 30 18:15:23 CEST 2011
On Tue, Aug 30, 2011 at 7:42 AM, Filipe Leme Botelho
<filipe.botelho at vpar.com.br> wrote:
> Hi all,
>
> I am reading previous posts and guidance on the 'reshape' package in order to solve the simple problem below. Thinking that this might be very trivial for most of you, I thought there could be a fast solution coming from you guys, and I´d be very thankful for that.
>
Here is a zoo solution:
# first set up input
Lines <- "Date Company Price
set-11 A 3
dez-11 A 3,2
jan-12 A 3,3
fev-12 A 2,7
mar-12 A 2,7
abr-12 A 2,8
mai-12 A 2,9
jun-12 A 3
jul-12 A 3,1
mar-12 B 5
abr-12 B 5,5
mai-12 B 5,7
jun-12 B 7
jul-12 B 6,6
dez-11 C 1
jan-12 C 1,1
fev-12 C 1,11
mar-12 C 1,2
abr-12 C 1,3"
Sys.setlocale("LC_ALL", "Portuguese")
library(zoo)
# The read.zoo command converts first column to yearmon class
# and reads it into a zoo object.
# in next line we would normally
# replace textConnection(Lines) with file name, e.g. "myfile.txt"
# but here we do it this way to keep it self contained
z <- read.zoo(textConnection(Lines), header = TRUE, dec = ",",
split = 2, FUN = function(x) as.yearmon(x, format = "%b-%y"))
This gives the following zoo object:
> z
A B C
set 2011 3.0 NA NA
dez 2011 3.2 NA 1.00
jan 2012 3.3 NA 1.10
fev 2012 2.7 NA 1.11
mar 2012 2.7 5.0 1.20
abr 2012 2.8 5.5 1.30
mai 2012 2.9 5.7 NA
jun 2012 3.0 7.0 NA
It may be more convenient to keep as a zoo object so you can do other
time series manipulations, e.g.
plot(z)
plot(z, screen = 1)
but you want it as a data frame:
DF <- data.frame(Date = time(z), coredata(z))
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list