Dear Robin,
It is rather easy if you combine melt() and cast() from the reshape
package.
dataset <- expand.grid(year = 1940:1942, cell = 1:2)
dataset$Q1 <- rnorm(nrow(dataset))
dataset$Q2 <- rnorm(nrow(dataset))
dataset$Q3 <- rnorm(nrow(dataset))
dataset$Q4 <- rnorm(nrow(dataset))
library(reshape)
datasetMelt <- melt(dataset, id.var = c("year", "cell"))
cast(datasetMelt, year + variable ~ ...)
HTH,
Thierry
------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Robin Hankin
Verzonden: donderdag 22 januari 2009 10:52
Aan: r-help at r-project.org
Onderwerp: [R] reshape() problems
Hi
I have a data frame with timeseries information like this:
year cell Q1 Q2 Q3 Q4
1940 1 1.2 1.4 1.4 1.9
1941 1 2.9 2.1 3.4 2.4
1942 1 2.7 3.2 1.5 2.6
1940 2 1.4 2.1 2.6 2.4
1941 2 2.4 1.4 1.4 3.4
1942 2 1.4 2.4 2.5 4.4
where the Qs mean 'quarter'. I want to extract from this
a dataframe with a timeseries for each cell:
year quarter cell1 cell2
1940 1 1.2 1.4
1940 2 1.4 2.1
1940 3 1.4 2.6
1940 4 1.9 2.4
1941 1 2.9 2.4
1941 2 2.1 1.4
1941 3 3.4 1.4
1942 4 2.4 3.4
1942 1 2.7 1.4
1942 2 3.2 2.4
1942 3 1.5 2.5
1942 4 2.6 4.4
Thus the third and fourth columns are the timeserieses for
cell 1 and cell 2.
Is there a nice vectorized way to do this?
I can't quite make reshape() do what I want.
[the real dataset is months, not quarters, has ~2000 cells
and ~60 years]
--
Robin K. S. Hankin
Uncertainty Analyst
University of Cambridge
19 Silver Street
Cambridge CB3 9EP
01223-764877
______________________________________________
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.
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.