Dear Gregory,

Is there an easier, cleaner way to do this?  Thanks.
There are of course several ways...

(assuming yearmonth to be a data.frame)

--- 1 ---

year <- colnames (yearmonth) [-1]
year <- gsub ("^[^[:digit:]]*([[:digit:]]*[^[:digit:]]*$)", "\\1", year)
year <- as.numeric (year)

month <- yearmonth$month

precip <- as.matrix (yearmonth [, -1])

long.df <- data.frame (month = rep (month, length (year)),
                       year = rep (year, each = nrow (yearmonth)),
                       precipitation = as.numeric (precip))


If you're about to do this more often:
--- 2 ---
package hyperSpec (outdated on CRAN, if you want to install it use the version on rforge)
has a function array2df which helps with this transformation:

long.df <- array2df (precip, label.x = "precipitation",
                     levels = list (month = month, year = year)

--- 3 ---
depending on your file (are the column names numbers without the Xs?)
you may be able to abuse a hyperSpec object to read your data easily:
x <- read.txt.wide ("filename", ...more options...)
then
as.long.df (x)
is about what you want.
(You'd probably want to rename the columns)

HTH Claudia


Gregory A. Graves, Lead Scientist
Everglades REstoration COoordination and VERification (RECOVER)
Restoration Sciences Department
South Florida Water Management District
Phones:  DESK: 561 / 682 - 2429
                    CELL:  561 / 719 - 8157

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


--
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 6/a
I-34127 Trieste

phone: +39 0 40 5 58-37 68
email: cbelei...@units.it

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

Reply via email to