[R] getting data frame rows out of a by object

Ed L Cashin ecashin at uga.edu
Thu Apr 8 17:14:25 CEST 2004


Douglas Bates <bates at stat.wisc.edu> writes:

> Ed L Cashin <ecashin at uga.edu> writes:
...
>> I think I need to look at the source of split when I have more time.
>
> ?split is probably a better place to start.

I've read that, so I think I need more nitty gritty.

>> > [1] If you really want to be cautious you could use an octal
>> > representation like sep="\007" to get a character that is very
>> > unlikely to occur in a factor level.
>> 
>> I definitely want to be cautious.  Instead of the bell character I
>> think I'll use the field separator character, "\034", just because
>> this is the first time I've been able to use it for it's intended
>> purpose!  ;)
>
> Yes, but with "\034" you don't get to make obscure James Bond
> references :-).

I hadn't thought of that.

One thing I notice is that I get funny row names using this method.  I
can change them back easily enough, though.

> d
    a b  c
1   1 4 31
2   2 3 32
3   3 2 33
4   4 1 34
11  1 4 41
21  2 3 42
31  3 2 43
41  4 1 44
12 41 0  0
> d2 <- do.call("rbind",
          lapply(split(d,
                       factor(paste(d$a,
                                    d$b,
                                    sep = "\034"))),
                 function(x) x[x$c == min(x$c),]))
> d2
      a b  c
14   1 4 31
23   2 3 32
32   3 2 33
41   4 1 34
410 41 0  0
> row.names(d2) <- 1:nrow(d2)
> d2
   a b  c
1  1 4 31
2  2 3 32
3  3 2 33
4  4 1 34
5 41 0  0
> 

-- 
--Ed L Cashin            |   PGP public key:
  ecashin at uga.edu        |   http://noserose.net/e/pgp/




More information about the R-help mailing list