[R] Merge records in the same dataframe

jim holtman
Thu, 05 Nov 2009 21:28:26 -0000

Try something like this:

# get column numbers of 'wk' columns
col.num <- grep("^wk", names(yourDF))
# split out only the 'wk' columns
result <- lapply(split(yourDF[, col.num], paste(yourDF$x2,
paste(yourDF$x3))), function(.mrg){
    colSums(.mrg)
})


On Thu, Nov 5, 2009 at 4:15 PM, Vadlamani, Satish {FLNA}
<SATISH.VADLAMANI at fritolay.com> wrote:
> Hi:
>
> Suppose that I have a data frame as below
>
> x1 x2 x3 ... x10 wk1 wk2 ... Wk208 (these are the column names)
>
> For each record, x1, x2, x3 ... x10 are attributes. and wk1, wk2, ...,
wk208 are the sales recoreded for this attribute combination. Suppose that now,
that I want to do the following
>
> 1. Merge the data frame so that I have a new data frame grouped by values
of x2 and x3 (for example). That is, if two records have the same values of x2
and x3, they should be summed.
>
> I tried to look at merge, tapply etc. but did not see a fit with I want to
do above.
>
> Thanks in advance.
>
> Satish
>
> ______________________________________________
> 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.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?