[R] How to split data frame into groups by a list of factors?

jim holtman jholtman at gmail.com
Sun Jan 31 21:19:34 CET 2010


This seems to work fine for me:

> x <- read.table(textConnection("      Date trade_day      IV.b      IV.a
+ 1 2003-03-02        19 0.1724818 0.1815687
+ 2 2003-03-02        18 0.1733542 0.1763827
+ 3 2003-03-02        19 0.1753308 0.1783653
+ 4 2003-03-03        16 0.1751217 0.1781624
+ 5 2003-03-03        16 0.1738580 0.1768961
+ 6 2003-03-03        19 0.1733346 0.1763800"), header=TRUE)
> closeAllConnections()
> split(x, list(x$Date, x$trade_day), drop=TRUE)
$`2003-03-03.16`
        Date trade_day      IV.b      IV.a
4 2003-03-03        16 0.1751217 0.1781624
5 2003-03-03        16 0.1738580 0.1768961

$`2003-03-02.18`
        Date trade_day      IV.b      IV.a
2 2003-03-02        18 0.1733542 0.1763827

$`2003-03-02.19`
        Date trade_day      IV.b      IV.a
1 2003-03-02        19 0.1724818 0.1815687
3 2003-03-02        19 0.1753308 0.1783653

$`2003-03-03.19`
        Date trade_day      IV.b    IV.a
6 2003-03-03        19 0.1733346 0.17638

>


On Sun, Jan 31, 2010 at 2:25 PM, Ted Zeng (曾振兴) <zengzhenxing at gmail.com> wrote:
>
> Dear R users:
>
> I am dealing a data frame x as followings:
>
>       Date trade_day      IV.b      IV.a
> 1 2003-03-02        19 0.1724818 0.1815687
> 2 2003-03-02        18 0.1733542 0.1763827
> 3 2003-03-02        19 0.1753308 0.1783653
> 4 2003-03-03        16 0.1751217 0.1781624
> 5 2003-03-03        16 0.1738580 0.1768961
> 6 2003-03-03        19 0.1733346 0.1763800
> ......
>
> I want to split the data flame into groups according to the variable "Date"
> and "trade_day"
> I tried: split(x,f=list(a=x$Date,b=x$trade_day)), but it doesn't work. How
> can I solve this problem?
>
> Thanks advance
>
> Ted
> --
> View this message in context: http://n4.nabble.com/How-to-split-data-frame-into-groups-by-a-list-of-factors-tp1458171p1458171.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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