[Rd] split.data.frame

From: Romain Francois <romain.francois_at_dbmail.com>
Date: Tue, 15 Dec 2009 10:06:22 +0100


Hello,

I very much enjoy "with" and "subset" semantics for data frames and was wondering if we could have something similar with split, basically by evaluating the second argument "with" the data frame :

split.data.frame
function(x, f, drop = FALSE, ...){

         call <- match.call( )
         fcall <- call( "with", data = call[["x"]], expr = call[["f"]] )
         ff <- eval( fcall, parent.frame(1) )

         lapply(split(seq_len(nrow(x)), ff, drop = drop, ...), 
function(ind) x[ind, , drop = FALSE])
}

> split( df, y )

$`1`

   x y
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1

$`2`

     x y
6 6 2
7 7 2
8 8 2
9 9 2
10 10 2

> split( df, x > 3 )

$`FALSE`
   x y
1 1 1
2 2 1
3 3 1

$`TRUE`
     x y
4 4 1
5 5 1
6 6 2
7 7 2
8 8 2
9 9 2
10 10 2

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/HlX9 : new package : bibtex
|- http://tr.im/Gq7i : ohloh
`- http://tr.im/FtUu : new package : highlight

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Tue 15 Dec 2009 - 09:11:22 GMT

This archive was generated by hypermail 2.2.0 : Wed 16 Dec 2009 - 00:01:10 GMT