An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130211/f2486e96/attachment.pl>
Changing the order of months within a year
5 messages · nick pardikes, Rui Barradas, arun +2 more
Hello,
Maybe there's some date trick, but I think the following will do the job.
dat <- read.table(text = "
site date year precipitation temp_max temp_min
1 castlepeak Jan-71 1971 26.2903226 38.29032 18.06452
2 castlepeak Feb-71 1971 9.1071429 39.60714 17.50000
3 castlepeak Mar-71 1971 36.3548387 38.87097 17.77419
4 castlepeak Apr-71 1971 14.8333333 44.06667 22.56667
5 castlepeak May-71 1971 11.0967742 47.87097 29.22581
6 castlepeak Jun-71 1971 7.9000000 62.20000 34.16667
7 castlepeak Jul-71 1971 0.4516129 75.45161 44.93548
8 castlepeak Aug-71 1971 1.1612903 76.54839 45.96774
9 castlepeak Sep-71 1971 0.9333333 68.30000 36.86667
10 castlepeak Oct-71 1971 10.1612903 56.22581 31.74194
11 castlepeak Nov-71 1971 58.1000000 43.13333 28.43333
12 castlepeak Dec-71 1971 56.0645161 30.87097 16.29032
13 castlepeak Jan-72 1972 16.7741935 33.90323 15.41935
14 castlepeak Feb-72 1972 30.4137931 37.62069 19.82759
15 castlepeak Mar-72 1972 12.5161290 46.45161 24.06452
16 castlepeak Apr-72 1972 20.9333333 41.23333 22.10000
17 castlepeak May-72 1972 3.3870968 53.06452 28.77419
18 castlepeak Jun-72 1972 3.7666667 65.60000 37.93333
19 castlepeak Jul-72 1972 0.0000000 74.25806 43.45161
20 castlepeak Aug-72 1972 0.2258065 73.35484 43.03226
21 castlepeak Sep-72 1972 5.4000000 65.66667 37.90000
22 castlepeak Oct-72 1972 5.1612903 51.00000 27.38710
23 castlepeak Nov-72 1972 23.0666667 40.10000 23.43333
24 castlepeak Dec-72 1972 50.7096774 27.00000 10.16129
", header = TRUE, stringsAsFactors = FALSE)
str(dat)
fun <- function(x){
sp <- strsplit(x, "-")
m <- sapply(sp, `[`, 1)
m <- ordered(m, levels = c("Sep", "Oct", "Nov", "Dec", "Jan", "Feb",
"Mar", "Apr", "May", "Jun", "Jul", "Aug"))
y <- sapply(sp, `[`, 2)
order(y, m)
}
idx <- fun(dat$date)
dat[idx, ]
Hope this helps,
Rui Barradas
Em 12-02-2013 06:09, nick pardikes escreveu:
I have data that looks like below and I would like to re-order the values within the "date" column. I would like to have each year organized like so:
Sep-71
Oct-71
Nov-71
Dec-71
Jan-71
Feb-71
Mar-71
Apr-71
May-71
Jun-71
Jul-71
Aug-71
Sep-72
Oct-72
etc...
Is there any way I can order the column in my own fashion and just move Sep-Dec to the beginning of each year? I am planning on doing some time series analysis with this data and think that the order of months is very important for the time series. I appreciate any suggestions and thank you in advance.
site date year precipitation temp_max temp_min
1 castlepeak Jan-71 1971 26.2903226 38.29032 18.06452
2 castlepeak Feb-71 1971 9.1071429 39.60714 17.50000
3 castlepeak Mar-71 1971 36.3548387 38.87097 17.77419
4 castlepeak Apr-71 1971 14.8333333 44.06667 22.56667
5 castlepeak May-71 1971 11.0967742 47.87097 29.22581
6 castlepeak Jun-71 1971 7.9000000 62.20000 34.16667
7 castlepeak Jul-71 1971 0.4516129 75.45161 44.93548
8 castlepeak Aug-71 1971 1.1612903 76.54839 45.96774
9 castlepeak Sep-71 1971 0.9333333 68.30000 36.86667
10 castlepeak Oct-71 1971 10.1612903 56.22581 31.74194
11 castlepeak Nov-71 1971 58.1000000 43.13333 28.43333
12 castlepeak Dec-71 1971 56.0645161 30.87097 16.29032
13 castlepeak Jan-72 1972 16.7741935 33.90323 15.41935
14 castlepeak Feb-72 1972 30.4137931 37.62069 19.82759
15 castlepeak Mar-72 1972 12.5161290 46.45161 24.06452
16 castlepeak Apr-72 1972 20.9333333 41.23333 22.10000
17 castlepeak May-72 1972 3.3870968 53.06452 28.77419
18 castlepeak Jun-72 1972 3.7666667 65.60000 37.93333
19 castlepeak Jul-72 1972 0.0000000 74.25806 43.45161
20 castlepeak Aug-72 1972 0.2258065 73.35484 43.03226
21 castlepeak Sep-72 1972 5.4000000 65.66667 37.90000
22 castlepeak Oct-72 1972 5.1612903 51.00000 27.38710
23 castlepeak Nov-72 1972 23.0666667 40.10000 23.43333
24 castlepeak Dec-72 1972 50.7096774 27.00000 10.16129
Nick Pardikes
PhD Student
Program in Ecology, Evolution and Conservation Biology
University of Nevada, Reno
303-550-1072
http://wolfweb.unr.edu/homepage/npardikes/MySite/Welcome.html
[[alternative HTML version deleted]]
______________________________________________ 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.
Hi,
You could try:
dat1<- read.table(text="
??????? site????????? date year precipitation temp_max temp_min
1? castlepeak Jan-71 1971??? 26.2903226 38.29032 18.06452
2? castlepeak Feb-71 1971??? 9.1071429 39.60714 17.50000
3? castlepeak Mar-71 1971??? 36.3548387 38.87097 17.77419
4? castlepeak Apr-71 1971??? 14.8333333 44.06667 22.56667
5? castlepeak May-71 1971??? 11.0967742 47.87097 29.22581
6? castlepeak Jun-71 1971??? 7.9000000 62.20000 34.16667
7? castlepeak Jul-71 1971??? 0.4516129 75.45161 44.93548
8? castlepeak Aug-71 1971??? 1.1612903 76.54839 45.96774
9? castlepeak Sep-71 1971??? 0.9333333 68.30000 36.86667
10 castlepeak Oct-71 1971??? 10.1612903 56.22581 31.74194
11 castlepeak Nov-71 1971??? 58.1000000 43.13333 28.43333
12 castlepeak Dec-71 1971??? 56.0645161 30.87097 16.29032
13 castlepeak Jan-72 1972??? 16.7741935 33.90323 15.41935
14 castlepeak Feb-72 1972??? 30.4137931 37.62069 19.82759
15 castlepeak Mar-72 1972??? 12.5161290 46.45161 24.06452
16 castlepeak Apr-72 1972??? 20.9333333 41.23333 22.10000
17 castlepeak May-72 1972??? 3.3870968 53.06452 28.77419
18 castlepeak Jun-72 1972??? 3.7666667 65.60000 37.93333
19 castlepeak Jul-72 1972??? 0.0000000 74.25806 43.45161
20 castlepeak Aug-72 1972??? 0.2258065 73.35484 43.03226
21 castlepeak Sep-72 1972??? 5.4000000 65.66667 37.90000
22 castlepeak Oct-72 1972??? 5.1612903 51.00000 27.38710
23 castlepeak Nov-72 1972??? 23.0666667 40.10000 23.43333
24 castlepeak Dec-72 1972??? 50.7096774 27.00000 10.16129
",sep="",header=TRUE,stringsAsFactors=FALSE)
library(zoo)
vec1<- c("Sep","Oct","Nov","Dec","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug")
?dat2<-do.call(rbind,lapply(split(dat1,dat1$year), function(x)x[match(vec1,format(as.yearmon(x$date,"%b-%y"),"%b")),]))
?row.names(dat2)<- 1:nrow(dat2)
dat2
#???????? site?? date year precipitation temp_max temp_min
#1? castlepeak Sep-71 1971???? 0.9333333 68.30000 36.86667
#2? castlepeak Oct-71 1971??? 10.1612903 56.22581 31.74194
#3? castlepeak Nov-71 1971??? 58.1000000 43.13333 28.43333
#4? castlepeak Dec-71 1971??? 56.0645161 30.87097 16.29032
#5? castlepeak Jan-71 1971??? 26.2903226 38.29032 18.06452
#6? castlepeak Feb-71 1971???? 9.1071429 39.60714 17.50000
#7? castlepeak Mar-71 1971??? 36.3548387 38.87097 17.77419
#8? castlepeak Apr-71 1971??? 14.8333333 44.06667 22.56667
#9? castlepeak May-71 1971??? 11.0967742 47.87097 29.22581
#10 castlepeak Jun-71 1971???? 7.9000000 62.20000 34.16667
#11 castlepeak Jul-71 1971???? 0.4516129 75.45161 44.93548
#12 castlepeak Aug-71 1971???? 1.1612903 76.54839 45.96774
#13 castlepeak Sep-72 1972???? 5.4000000 65.66667 37.90000
#14 castlepeak Oct-72 1972???? 5.1612903 51.00000 27.38710
#15 castlepeak Nov-72 1972??? 23.0666667 40.10000 23.43333
#16 castlepeak Dec-72 1972??? 50.7096774 27.00000 10.16129
#17 castlepeak Jan-72 1972??? 16.7741935 33.90323 15.41935
#18 castlepeak Feb-72 1972??? 30.4137931 37.62069 19.82759
#19 castlepeak Mar-72 1972??? 12.5161290 46.45161 24.06452
#20 castlepeak Apr-72 1972??? 20.9333333 41.23333 22.10000
#21 castlepeak May-72 1972???? 3.3870968 53.06452 28.77419
#22 castlepeak Jun-72 1972???? 3.7666667 65.60000 37.93333
#23 castlepeak Jul-72 1972???? 0.0000000 74.25806 43.45161
#24 castlepeak Aug-72 1972???? 0.2258065 73.35484 43.03226
A.K.
----- Original Message -----
From: nick pardikes <npardikes at hotmail.com>
To: "r-help at R-project.org" <r-help at r-project.org>
Cc:
Sent: Tuesday, February 12, 2013 1:09 AM
Subject: [R] Changing the order of months within a year
I have data that looks like below and I would like to re-order the values within the "date" column. I would like to have each year organized like so:
Sep-71
Oct-71
Nov-71
Dec-71
Jan-71
Feb-71
Mar-71
Apr-71
May-71
Jun-71
Jul-71
Aug-71
Sep-72
Oct-72
etc...
Is there any way I can order the column in my own fashion and just move Sep-Dec to the beginning of each year? I am planning on doing some time series analysis with this data and think that the order of months is very important for the time series. I appreciate any suggestions and thank you in advance.
? ? ? ? site? ? ? ? ? date year precipitation temp_max temp_min
1? castlepeak Jan-71 1971? ? 26.2903226 38.29032 18.06452
2? castlepeak Feb-71 1971? ? 9.1071429 39.60714 17.50000
3? castlepeak Mar-71 1971? ? 36.3548387 38.87097 17.77419
4? castlepeak Apr-71 1971? ? 14.8333333 44.06667 22.56667
5? castlepeak May-71 1971? ? 11.0967742 47.87097 29.22581
6? castlepeak Jun-71 1971? ? 7.9000000 62.20000 34.16667
7? castlepeak Jul-71 1971? ? 0.4516129 75.45161 44.93548
8? castlepeak Aug-71 1971? ? 1.1612903 76.54839 45.96774
9? castlepeak Sep-71 1971? ? 0.9333333 68.30000 36.86667
10 castlepeak Oct-71 1971? ? 10.1612903 56.22581 31.74194
11 castlepeak Nov-71 1971? ? 58.1000000 43.13333 28.43333
12 castlepeak Dec-71 1971? ? 56.0645161 30.87097 16.29032
13 castlepeak Jan-72 1972? ? 16.7741935 33.90323 15.41935
14 castlepeak Feb-72 1972? ? 30.4137931 37.62069 19.82759
15 castlepeak Mar-72 1972? ? 12.5161290 46.45161 24.06452
16 castlepeak Apr-72 1972? ? 20.9333333 41.23333 22.10000
17 castlepeak May-72 1972? ? 3.3870968 53.06452 28.77419
18 castlepeak Jun-72 1972? ? 3.7666667 65.60000 37.93333
19 castlepeak Jul-72 1972? ? 0.0000000 74.25806 43.45161
20 castlepeak Aug-72 1972? ? 0.2258065 73.35484 43.03226
21 castlepeak Sep-72 1972? ? 5.4000000 65.66667 37.90000
22 castlepeak Oct-72 1972? ? 5.1612903 51.00000 27.38710
23 castlepeak Nov-72 1972? ? 23.0666667 40.10000 23.43333
24 castlepeak Dec-72 1972? ? 50.7096774 27.00000 10.16129
Nick Pardikes
PhD Student
Program in Ecology, Evolution and Conservation Biology
University of Nevada, Reno
303-550-1072
http://wolfweb.unr.edu/homepage/npardikes/MySite/Welcome.html
??? ??? ??? ? ??? ??? ?
??? [[alternative HTML version deleted]]
______________________________________________
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.
On Feb 11, 2013, at 10:09 PM, nick pardikes wrote:
I have data that looks like below and I would like to re-order the values within the "date" column. I would like to have each year organized like so: Sep-71 Oct-71 Nov-71 Dec-71 Jan-71 Feb-71 Mar-71 Apr-71 May-71 Jun-71 Jul-71 Aug-71 Sep-72 Oct-72 etc...
dat1[ order(dat1$year, match(substr(dat1$date, 1,3), month.abb[c(9:12,1:8)] ) ) , ]
David. > Is there any way I can order the column in my own fashion and just move Sep-Dec to the beginning of each year? I am planning on doing some time series analysis with this data and think that the order of months is very important for the time series. I appreciate any suggestions and thank you in advance. > > > > site date year precipitation temp_max temp_min > 1 castlepeak Jan-71 1971 26.2903226 38.29032 18.06452 > 2 castlepeak Feb-71 1971 9.1071429 39.60714 17.50000 > 3 castlepeak Mar-71 1971 36.3548387 38.87097 17.77419 > 4 castlepeak Apr-71 1971 14.8333333 44.06667 22.56667 > 5 castlepeak May-71 1971 11.0967742 47.87097 29.22581 > 6 castlepeak Jun-71 1971 7.9000000 62.20000 34.16667 > 7 castlepeak Jul-71 1971 0.4516129 75.45161 44.93548 > 8 castlepeak Aug-71 1971 1.1612903 76.54839 45.96774 > 9 castlepeak Sep-71 1971 0.9333333 68.30000 36.86667 > 10 castlepeak Oct-71 1971 10.1612903 56.22581 31.74194 > 11 castlepeak Nov-71 1971 58.1000000 43.13333 28.43333 > 12 castlepeak Dec-71 1971 56.0645161 30.87097 16.29032 > 13 castlepeak Jan-72 1972 16.7741935 33.90323 15.41935 > 14 castlepeak Feb-72 1972 30.4137931 37.62069 19.82759 > 15 castlepeak Mar-72 1972 12.5161290 46.45161 24.06452 > 16 castlepeak Apr-72 1972 20.9333333 41.23333 22.10000 > 17 castlepeak May-72 1972 3.3870968 53.06452 28.77419 > 18 castlepeak Jun-72 1972 3.7666667 65.60000 37.93333 > 19 castlepeak Jul-72 1972 0.0000000 74.25806 43.45161 > 20 castlepeak Aug-72 1972 0.2258065 73.35484 43.03226 > 21 castlepeak Sep-72 1972 5.4000000 65.66667 37.90000 > 22 castlepeak Oct-72 1972 5.1612903 51.00000 27.38710 > 23 castlepeak Nov-72 1972 23.0666667 40.10000 23.43333 > 24 castlepeak Dec-72 1972 50.7096774 27.00000 10.16129 > > Nick Pardikes > PhD Student > Program in Ecology, Evolution and Conservation Biology > University of Nevada, Reno > 303-550-1072 > http://wolfweb.unr.edu/homepage/npardikes/MySite/Welcome.html > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. David Winsemius Alameda, CA, USA
I'm sure the OP has reasons to reorder the months as shown but his data
seems to argue that he shouldn't split the wet season (Oct - Apr) but
should, instead, tack the 1972 months (Jan - Aug after the 1971 months
(Sep - Dec).
Clint
Clint Bowman INTERNET: clint at ecy.wa.gov
Air Quality Modeler INTERNET: clint at math.utah.edu
Department of Ecology VOICE: (360) 407-6815
PO Box 47600 FAX: (360) 407-7534
Olympia, WA 98504-7600
USPS: PO Box 47600, Olympia, WA 98504-7600
Parcels: 300 Desmond Drive, Lacey, WA 98503-1274
On Tue, 12 Feb 2013, David Winsemius wrote:
On Feb 11, 2013, at 10:09 PM, nick pardikes wrote:
I have data that looks like below and I would like to re-order the values within the "date" column. I would like to have each year organized like so: Sep-71 Oct-71 Nov-71 Dec-71 Jan-71 Feb-71 Mar-71 Apr-71 May-71 Jun-71 Jul-71 Aug-71 Sep-72 Oct-72 etc...
dat1[ order(dat1$year, match(substr(dat1$date, 1,3), month.abb[c(9:12,1:8)] ) ) , ] -- David.
Is there any way I can order the column in my own fashion and just move Sep-Dec to the beginning of each year? I am planning on doing some time series analysis with this data and think that the order of months is very important for the time series. I appreciate any suggestions and thank you in advance.
site date year precipitation temp_max temp_min
1 castlepeak Jan-71 1971 26.2903226 38.29032 18.06452
2 castlepeak Feb-71 1971 9.1071429 39.60714 17.50000
3 castlepeak Mar-71 1971 36.3548387 38.87097 17.77419
4 castlepeak Apr-71 1971 14.8333333 44.06667 22.56667
5 castlepeak May-71 1971 11.0967742 47.87097 29.22581
6 castlepeak Jun-71 1971 7.9000000 62.20000 34.16667
7 castlepeak Jul-71 1971 0.4516129 75.45161 44.93548
8 castlepeak Aug-71 1971 1.1612903 76.54839 45.96774
9 castlepeak Sep-71 1971 0.9333333 68.30000 36.86667
10 castlepeak Oct-71 1971 10.1612903 56.22581 31.74194
11 castlepeak Nov-71 1971 58.1000000 43.13333 28.43333
12 castlepeak Dec-71 1971 56.0645161 30.87097 16.29032
13 castlepeak Jan-72 1972 16.7741935 33.90323 15.41935
14 castlepeak Feb-72 1972 30.4137931 37.62069 19.82759
15 castlepeak Mar-72 1972 12.5161290 46.45161 24.06452
16 castlepeak Apr-72 1972 20.9333333 41.23333 22.10000
17 castlepeak May-72 1972 3.3870968 53.06452 28.77419
18 castlepeak Jun-72 1972 3.7666667 65.60000 37.93333
19 castlepeak Jul-72 1972 0.0000000 74.25806 43.45161
20 castlepeak Aug-72 1972 0.2258065 73.35484 43.03226
21 castlepeak Sep-72 1972 5.4000000 65.66667 37.90000
22 castlepeak Oct-72 1972 5.1612903 51.00000 27.38710
23 castlepeak Nov-72 1972 23.0666667 40.10000 23.43333
24 castlepeak Dec-72 1972 50.7096774 27.00000 10.16129
Nick Pardikes
PhD Student
Program in Ecology, Evolution and Conservation Biology
University of Nevada, Reno
303-550-1072
http://wolfweb.unr.edu/homepage/npardikes/MySite/Welcome.html
[[alternative HTML version deleted]]
______________________________________________ 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.
David Winsemius Alameda, CA, USA
______________________________________________ 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.