[R] getting data frame rows out of a by object
Ed L Cashin
ecashin at uga.edu
Thu Apr 8 03:24:44 CEST 2004
Julian Taylor <julian.taylor at adelaide.edu.au> writes:
...
> You are better off using other tools to give you the right subsets. Try
>
> d <- do.call("rbind", lapply(split(d, factor(paste(d$a, d$b, sep =
> ""))),
> function(el) el[el$c == max(el$c), ]))
That does work, thanks. I am a bit mystified by the use of paste,
factor, and split together like that. By concatenating the columns as
strings, you are coming up with values that aren't in any one column
of the data frame, but split doesn't care.
That's surprising. I thought this method was dangerous, so I tried to
fool split by adding a column-a value that was the same as the
concatenation of other column-a and column-b values, "41". Split
knows to work with both column a and column b, though, somehow.
I guess paste does more than I thought.
> d <- rbind(d, data.frame(a=41,b=0,c=0))
> split(d, factor(paste(d$a, d$b, sep = "")))
$"14"
a b c
1 1 4 31
11 1 4 41
$"23"
a b c
2 2 3 32
21 2 3 42
$"32"
a b c
3 3 2 33
31 3 2 43
$"41"
a b c
4 4 1 34
41 4 1 44
$"410"
a b c
12 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