[R] Data Rearrangement

arun smartpink111 at yahoo.com
Fri Feb 28 17:35:43 CET 2014


HI Dila,
You have to order it afterwards.
res <- transform(melt(dat,id.var=c("Year","Day")),Month=match(variable,month.abb),Amount=value)[,-c(3:4)]


res1 <- res[with(res,order(Year,Month,Day)),]
A.K.




On Friday, February 28, 2014 2:16 AM, dila radi <dilaradi21 at gmail.com> wrote:

Dear Arun,

Thank you so much for your concern.

It is slightly differ from what I want after I applied it on my data. My bad... It works by rearranging the data month by month. 

    Year Day Month Amount
1  2012   1     1    0.0
2  2012   2     1    0.0
3  2012   3     1    0.0
4  2012   4     1    0.0
5  2012   5     1    0.0
6  2012   6     1    0.0
7  2012   7     1   11.0
8  2012   8     1    0.0
9  2013   1     1    0.0
10 2013   2     1    0.0
11 2013   3     1    0.0
12 2013   4     1    0.0
13 2013   5     1    0.0
14 2013   6     1    0.0
15 2013   7     1   11.0
16 2013   8     1    0.0
17 2012   1     2    2.5
18 2012   2     2    6.5
19 2012   3     2    9.5
20 2012   4     2    0.0
21 2012   5     2    5.0
22 2012   6     2    4.0
23 2012   7     2    0.0
24 2012   8     2    5.0
25 2013   1     2    2.5
26 2013   2     2    6.5
27 2013   3     2    9.5
28 2013   4     2    0.0
29 2013   5     2    5.0
30 2013   6     2    4.0
31 2013   7     2    0.0
32 2013   8     2    5.0
33 2012   1     3    0.5
34 2012   2     3    0.0
35 2012   3     3    0.0
36 2012   4     3    8.0
37 2012   5     3    0.5
38 2012   6     3    3.5
39 2012   7     3   12.5
40 2012   8     3    8.0
41 2013   1     3    0.5
42 2013   2     3    0.0
43 2013   3     3    0.0
44 2013   4     3    8.0
45 2013   5     3    0.5
46 2013   6     3    3.5
47 2013   7     3   12.5
48 2013   8     3    8.0
49 2012   1     4    2.0
50 2012   2     4   29.0
51 2012   3     4    0.0
52 2012   4     4    0.5
53 2012   5     4  110.5
54 2012   6     4   22.0
55 2012   7     4    3.5
56 2012   8     4   36.5
57 2013   1     4    2.0
58 2013   2     4   29.0
59 2013   3     4    0.0
60 2013   4     4    0.5
61 2013   5     4  110.5
62 2013   6     4   22.0
63 2013   7     4    3.5
64 2013   8     4   36.5

But, I want it to be year by year.  I meant 2012 (Jan-April) followed by 2013 (Jan - April)

Your help is really helpful, but I have to do some alteration which is I stuck in rearranging it year by year.

Regards, 
Dila




On 27 February 2014 22:10, arun <smartpink111 at yahoo.com> wrote:

HI DIla,
>
>Isn't this the same question you asked earlier?
>
>
>transform(melt(dat,id.var=c("Year","Day")),Month=match(variable,month.abb),Amount=value)[,-c(3:4)]
>A.K.
>
>
>
>
>
>On Thursday, February 27, 2014 10:24 PM, dila radi <dilaradi21 at gmail.com> wrote:
>Hi all,
>
>I know this is easy, but I really do not have any idea to solve it.
>
>I have this kind of data set:
>
>dat <- read.table(text="Day Year Jan Feb Mar Apr
>  1  2012 0 2.5 0.5 2
>  2  2012 0 6.5 0 29
>  3  2012 0 9.5 0 0
>  4  2012 0 0 8 0.5
>  5  2012 0 5 0.5 110.5
>  6  2012 0 4 3.5 22
>  7  2012 11 0 12.5 3.5
>  8  2012 0 5 8 36.5
>  1  2013 0 2.5 0.5 2
>  2  2013 0 6.5 0 29
>  3  2013 0 9.5 0 0
>  4  2013 0 0 8 0.5
>  5  2013 0 5 0.5 110.5
>  6  2013 0 4 3.5 22
>  7  2013 11 0 12.5 3.5
>  8  2013 0 5 8 36.5",sep="",header=TRUE)
>
>and I want it to be in this form:
>
>    Year       Day Month Amount  2012 1 1 0  2012 2 1 0  2012 3 1 0  2012 4
>1 0  2012 5 1 0  2012 6 1 0  2012 7 1 11  2012 8 1 0  2012 1 2 2.5  2012 2 2
>6.5  2012 3 2 9.5  2012 4 2 0  2012 5 2 5  2012 6 2 4  2012 7 2 0  2012 8 2
>5  2012 1 3 0.5  2012 2 3 0  2012 3 3 0  2012 4 3 8  2012 5 3 0.5  2012 6 3
>3.5  2012 7 3 12.5  2012 8 3 8  2012 1 4 2  2012 2 4 29  2012 3 4 0  2012 4
>4 0.5  2012 5 4 110.5  2012 6 4 22  2012 7 4 3.5  2012 8 4 36.5     2013 1 1
>0  2013 2 1 0  2013 3 1 0  2013 4 1 0  2013 5 1 0  2013 6 1 0  2013 7 1 11
>2013 8 1 0  2013 1 2 2.5  2013 2 2 6.5  2013 3 2 9.5  2013 4 2 0  2013 5 2 5
>2013 6 2 4  2013 7 2 0  2013 8 2 5  2013 1 3 0.5  2013 2 3 0  2013 3 3 0
>2013 4 3 8  2013 5 3 0.5  2013 6 3 3.5  2013 7 3 12.5  2013 8 3 8  2012 1 4
>2  2012 2 4 29  2012 3 4 0  2012 4 4 0.5  2012 5 4 110.5  2012 6 4 22  2012
>7 4 3.5  2012 8 4 36.5  2013 1 4 2  2013 2 4 29  2013 3 4 0  2013 4 4 0.5
>2013 5 4 110.5  2013 6 4 22  2013 7 4 3.5  2013 8 4 36.5
>I want to rearrange the data according to the YEAR (year by year)
>
>Thank you.
>
>Regards,
>Dila
>
>
>    [[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