[R] Dataset Transformation

jim holtman jholtman at gmail.com
Mon Oct 11 15:42:06 CEST 2010


try this:

> x <- read.table(textConnection("Date            TICKER          Price
+ 11/10/2010              A               0.991642
+ 11/10/2010              B               0.475023
+ 11/10/2010              C               0.218642
+ 11/10/2010              D               0.365135
+ 12/10/2010              A               0.687873
+ 12/10/2010              B               0.47006
+ 12/10/2010              C               0.533542
+ 12/10/2010              D               0.812439
+ 13/10/2010              A               0.210848
+ 13/10/2010              B               0.699799
+ 13/10/2010              C               0.546003
+ 13/10/2010              D               0.152316"), header = TRUE,
as.is = TRUE)
> closeAllConnections()
> x.m <- melt(x)
Using Date, TICKER as id variables
> cast(x.m, Date ~ TICKER)
        Date        A        B        C        D
1 11/10/2010 0.991642 0.475023 0.218642 0.365135
2 12/10/2010 0.687873 0.470060 0.533542 0.812439
3 13/10/2010 0.210848 0.699799 0.546003 0.152316
>


On Mon, Oct 11, 2010 at 9:35 AM, Santosh Srinivas
<santosh.srinivas at gmail.com> wrote:
> Repost .. since the previous msg had problems
>
> I need to transpose the following input dataset  into an output dataset like
> below
>
> Input
> Date            TICKER          Price
> 11/10/2010              A               0.991642
> 11/10/2010              B               0.475023
> 11/10/2010              C               0.218642
> 11/10/2010              D               0.365135
> 12/10/2010              A               0.687873
> 12/10/2010              B               0.47006
> 12/10/2010              C               0.533542
> 12/10/2010              D               0.812439
> 13/10/2010              A               0.210848
> 13/10/2010              B               0.699799
> 13/10/2010              C               0.546003
> 13/10/2010              D               0.152316
>
> Output needed
>
> Date    A       B       C       D
> 11/10/2010      0.991642        0.475023        0.218642        0.365135
> 12/10/2010      0.687873        0.47006 0.533542        0.812439
> 13/10/2010      0.210848        0.699799        0.546003        0.152316
>
> I tried using the aggregate function but not quite getting the method.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list