Skip to content

How to import time-series data

6 messages · Gabor Grothendieck, RichardSmith

#
Sorry for this very basic question. I have time-series data, laid out as a
table (in csv) like this:



That is, the first column is the sample ID, and subsequent columns are the
data at time interval in days since the start. In Excel, this is a very
normal way to lay out time-series data, but I can't find any way to work
with it in R. I want to import the data to R so I can, for example, plot the
data for each sample. If I import as a matrix or table, R renames the day
numbers to e.g. X20 instead of 20. What is the correct way to do this?

Many thanks,
Richard

--
View this message in context: http://r.789695.n4.nabble.com/How-to-import-time-series-data-tp4381372p4381372.html
Sent from the R help mailing list archive at Nabble.com.
#
On Sun, Feb 12, 2012 at 10:52 AM, RichardSmith
<richardsmith404 at gmail.com> wrote:
Something seems to have gone wrong in the posting since we can't see
the sample data that seems to be intended to be part of the post.  At
any rate read.zoo in the zoo package can read series.  You may need to
use the split= argument if your file defines multiple series tagged by
an id column.  See ?read.zoo and the Reading Data in Zoo vignette
which is online here
http://cran.r-project.org/web/packages/zoo/index.html
and is an entire document devoted to reading time series.  Alternately
its available in R through the vignette("zoo-read") command.
#
Gabor Grothendieck wrote
Sorry, I posted via Nabble using the 'raw' command, which got stripped out
it seems. Here's a small sample of the data...
plant,aphid,1,2,3,4
pumpkin,1-2,0.065566,0.057844,0.08,0.086879
pumpkin,1-3,0.107612,0.097272,0.11663,0.160499
squash,1-4,0.126939,0.115003,0.140275,0.188829

columns named 1,2,3,4 etc. correspond to the weight of an aphid after that
many days on that plant. I want to be able to plot the growth of all aphids
on one plot, grouped by plant.


Gabor Grothendieck wrote
Thanks for the link. I can't see any example in the zoo.read vignette which
looks similar. Do I need to transpose my data first, so dates are in one
column? If I do that, how do I keep the plant grouping associated with each
aphid?

Many thanks


--
View this message in context: http://r.789695.n4.nabble.com/How-to-import-time-series-data-tp4381372p4381558.html
Sent from the R help mailing list archive at Nabble.com.
#
On Sun, Feb 12, 2012 at 12:35 PM, RichardSmith
<richardsmith404 at gmail.com> wrote:
Try this xyplot.zoo lattice graph.   Time series are represented in
columns so we transpose the data and convert it to zoo.  The screen=
argument available in xyplot.zoo groups series into panels:

Lines <- "plant,aphid,1,2,3,4
pumpkin,1-2,0.065566,0.057844,0.08,0.086879
pumpkin,1-3,0.107612,0.097272,0.11663,0.160499
squash,1-4,0.126939,0.115003,0.140275,0.188829"

library(zoo)
library(lattice)
DF <- read.csv(text = Lines)
z <- zoo(t(DF[3:6]))
colnames(z) <- DF$aphid
xyplot(z, screen = DF$plant)
#
Gabor Grothendieck wrote
Thank you! That gets the data in exactly the shape I was expecting. I was
hoping to get the graph showing all lines on one plot, but coloured
according to plant. I tried to change screen=DF$plant for groups=DF$plant,
but it doesn't work, and I can't figure out from the documentation why it
doesn't work (I think I need to more thoroughly understand data types
first). Could you point me in the right direction?

Thanks so much for your help so far.

--
View this message in context: http://r.789695.n4.nabble.com/How-to-import-time-series-data-tp4381372p4383740.html
Sent from the R help mailing list archive at Nabble.com.
#
On Mon, Feb 13, 2012 at 8:23 AM, RichardSmith <richardsmith404 at gmail.com> wrote:
1. Try this which uses lattice zoo graphics:

xyplot(z, screen = 1, col = DF$plant)

or with a legend:

key <- list(space = "top",  text = levels(DF$plant), points = FALSE,
	lines = TRUE, col = 1:nlevels(DF$plant))
xyplot(z, screen = 1, col = DF$plant, auto.key = key)

2. or using classic zoo graphics

plot(z, screen = 1, col = DF$plant)

To add a legend:

legend("topleft", legend = levels(DF$plant), col = 1:nlevels(DF$plant), lty = 1)