[R] Working with data-frame
Bert Gunter
gunter.berton at gene.com
Sun Nov 9 21:50:05 CET 2014
Christopher:
If I understand correctly, see ?ave, or ?tapply, depending on what
form you want to be returned.
The trick is to first paste the columns together on whose unique you
want to split to form a singtl factor. e.g. of the form
lapply(split(yourcolumn,paste0(...)),FUN= sum)
However, the above functions already have this "built" in, so you
don't need to do this explicitly, although my impression is that it
may be a tad more efficient to do it the long way. But don't quote me
on this!
Cheers,
Bert
Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374
"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll
On Sun, Nov 9, 2014 at 11:58 AM, Christofer Bogaso
<bogaso.christofer at gmail.com> wrote:
> Hi again,
>
> Let say, I have following data frame:
>
>
> Dat <- structure(list(A1 = structure(c(3L, 3L, 1L, 3L, 3L, 3L, 3L, 2L,
> 3L, 3L, 1L, 2L, 3L, 2L, 1L, 1L, 3L, 3L, 2L, 3L, 2L, 2L, 3L, 3L,
> 3L, 2L, 3L, 1L, 1L, 3L), .Label = c("a", "b", "c"), class = "factor"),
> A2 = c(2, 3, 2, 1, 3, 3, 2, 2, 3, 1, 3, 1, 3, 3, 2, 2, 1,
> 2, 1, 2, 1, 3, 3, 2, 1, 2, 3, 2, 2, 2), C1 = 1:30), .Names = c("A1",
> "A2", "C1"), row.names = c(NA, -30L), class = "data.frame")
>
>
> Now my goal is :
> 1: Find all possible unique combinations of column 'A1' & column 'A2'.
> For example A1 = c, A2 = 2 is 1 unique combination.
>
> 2. For each such unique combination, calculate sum for 'A3'.
>
> Is there any direct R function to achieve this faster way? I have very
> large data-frame to handle with such calculation.
>
> I tried with spilt() function. However it looks to me that, it can
> split a data-frame w.r.t. only one column.
>
> Thanks for your suggestion
>
> ______________________________________________
> 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.
More information about the R-help
mailing list