[R] Changing the order of months within a year
Rui Barradas
ruipbarradas at sapo.pt
Tue Feb 12 14:28:18 CET 2013
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.
>
More information about the R-help
mailing list