Re: [R] Regrouping data

From: Sandeepa Ramakrishnan <sandeepa_ramakrishnan_at_yahoo.com>
Date: Fri, 08 Apr 2011 13:14:24 +0530 (IST)

Dear R helpers,

Thanks a lot for your kind help. The xtab function suggested by Mr. Henrique Dallazuanna produces following output for me

> xtabs(values ~ name + period + cy_date, rat_dat)
, , cy_date = 31-May-11

    period
name 29-Mar-11 30-Mar-11 31-Mar-11

   A     10.37     10.46     10.42
   B     12.61     12.65     12.62
   C     14.08     14.12     14.10
   D     11.17     11.15     11.11

# ---------------------------------------------------------------------------------------------

I am extremely sorry for framing my question in a wrong way and the output I wanted got scattered in my first mail. I am reframing my query as under.

I have a data.frame

rat_dat = data.frame(name = c("A", "A", "A", "B", "B", "B", "C", "C", "C", "D", "D", "D"),

                              period = c("31-Mar-11", "30-Mar-11", "29-Mar-11", "31-Mar-11", "30-Mar-11", "29-Mar-11",  "31-Mar-11", "30-Mar-11", "29-Mar-11", "31-Mar-11", "30-Mar-11", "29-Mar-11"),
values = c(10.42, 10.46, 10.37, 12.62, 12.65, 12.61, 14.10, 14.12, 14.08, 11.11, 11.15, 11.17)) 

> rat_dat

   name    period          values
1     A      31-Mar-11    10.42
2     A      30-Mar-11    10.46
3     A      29-Mar-11    10.37
4     B      31-Mar-11    12.62
5     B      30-Mar-11    12.65
6     B      29-Mar-11    12.61
7     C      31-Mar-11    14.10
8     C      30-Mar-11    14.12
9     C      29-Mar-11    14.08
10    D     31-Mar-11    11.11

11    D     30-Mar-11    11.15
12    D     29-Mar-11    11.17

I need to rearrange this data as new new data.frame

rat_dat_new as

period            A           B           C           D
31-Mar-11   10.42     12.62       14.10    11.11
30-Mar-11   10.46     12.65       14.12    11.15
29-Mar-11   10.37     12.61       14.08    11.17

I once again apologize for mistake on my part.

I tried to the solution suggested by Mr. Henrique Dallazuanna, however I was not able to read the output as a data.frame.

Kindly guide

From: Henrique Dallazuanna <wwwhsd_at_gmail.com> Subject: Re: [R] Regrouping data
To: "Sandeepa Ramakrishnan" <sandeepa_ramakrishnan_at_yahoo.com> Cc: r-help_at_r-project.org
Date: Thursday, 7 April, 2011, 6:05 PM

Try this:

1)
xtabs(values ~ name + period + cy_date, rat_dat)

2)
as.Date(rat_dat$cy_date, '%d-%b-%y') - as.Date(rat_dat$period, '%d-%b-%y')

On Thu, Apr 7, 2011 at 9:03 AM, Sandeepa Ramakrishnan <sandeepa_ramakrishnan_at_yahoo.com> wrote:

>
>
>
>
>
>
>
>
>

> Dear R forum
>

> I have just started my venture with R. While I am trying to learn R through the tutorials, I think the current problem I need to address to is beyond my knowledge about R.
>

> I have a dataframe as defined below -
>
>

> rat_dat = data.frame(name = c("A", "A", "A", "B", "B", "B", "C", "C", "C", "D", "D", "D"),
>                               period = c("31-Mar-11", "30-Mar-11", "29-Mar-11", "31-Mar-11", "30-Mar-11", "29-Mar-11",  "31-Mar-11", "30-Mar-11", "29-Mar-11", "31-Mar-11", "30-Mar-11", "29-Mar-11"),
> cy_date = c("31-May-11","31-May-11", "31-May-11", "31-May-11", "31-May-11", "31-May-11", "31-May-11", "31-May-11", "31-May-11", "31-May-11", "31-May-11", "31-May-11"),
> values = c(10.42, 10.46, 10.37, 12.62, 12.65, 12.61, 14.10, 14.12, 14.08, 11.11, 11.15, 11.17))
>
>
>> rat_dat

>    name    period   cy_date values
> 1     A 31-Mar-11 31-May-11  10.42
> 2     A 30-Mar-11 31-May-11  10.46
> 3     A 29-Mar-11 31-May-11  10.37
> 4     B 31-Mar-11 31-May-11  12.62
> 5     B 30-Mar-11 31-May-11  12.65
> 6     B 29-Mar-11 31-May-11  12.61
> 7     C 31-Mar-11 31-May-11  14.10
> 8     C 30-Mar-11 31-May-11  14.12
> 9     C 29-Mar-11 31-May-11  14.08
> 10    D 31-Mar-11 31-May-11  11.11
> 11    D 30-Mar-11 31-May-11  11.15
> 12    D 29-Mar-11 31-May-11  11.17
>

> My actual data is too large. I need to
>

> (1) rearrange this input as
>
>
>
>
>
>
>
>
>

> period
>   cy_date
>           A
>       B
>     C
>     D
>

> 31-Mar-11
>  31-May-11
> 10.42
>   12.62
> 14.10
> 11.11
>

> 30-Mar-11
>  31-May-11
> 10.46
>   12.65
> 14.12
> 11.15
>

> 29-Mar-11
>  31-May-11
> 10.37
>   12.61
> 14.08
> 11.17
>

> (2) Also, I need to find the difference between (rat_dat$cy_date) - (rat_dat$period)
>

> I hope I am able to put forward my requirement properly. If not, please forgive me.
>

> Sandeepa
>

>        [[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.
>
>
-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

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

Received on Fri 08 Apr 2011 - 07:47:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 08 Apr 2011 - 12:10:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive