Skip to content
Back to formatted view

Raw Message

Message-ID: <CA+8X3fXi+F2yKveAEPvFT7j=ZhYepKvxJ=297YMuAy12t=nOcA@mail.gmail.com>
Date: 2016-08-23T23:01:35Z
From: Jim Lemon
Subject: aggregate
In-Reply-To: <CAHmzXO7tGBRevkQbcM_F31exmMtCgM-7HDW7d3VMzSFzO+HcJg@mail.gmail.com>

Hi Gang Chen,
If I have the right idea:

for(zval in levels(myData$Z))
crossprod(as.matrix(myData[myData$Z==zval,c("X","Y")]))

Jim

On Wed, Aug 24, 2016 at 8:03 AM, Gang Chen <gangchen6 at gmail.com> wrote:
> This is a simple question: With a dataframe like the following
>
> myData <- data.frame(X=c(1, 2, 3, 4), Y=c(4, 3, 2, 1), Z=c('A', 'A', 'B', 'B'))
>
> how can I get the cross product between X and Y for each level of
> factor Z? My difficulty is that I don't know how to deal with the fact
> that crossprod() acts on two variables in this case.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.