[R] Working with data-frame

William Dunlap wdunlap at tibco.com
Sun Nov 9 22:39:45 CET 2014


> I tried with spilt() function. However it looks to me that, it can
> split a data-frame w.r.t. only one column.

(I assume you you meant 'split', not 'spilt'.)

You did not show what you tried, but the following splits Dat by its "A1"
and "A2" columns (creating a list of data.frames):
   split(Dat, f=Dat[,c("A1","A2")])

aggregate(), in core R, combine the split and the lapply needed to
calculate groupwise sums.  E.g.,
  aggregate(Dat$C1, by=Dat[,c("A1","A2")], FUN=sum)
  aggregate(C1 ~ A1 + A2, data=Dat, FUN=sum)

The plyr and dplyr packages have other ways to do this sort of thing.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list