[R] avoiding for loops
R. Michael Weylandt
michael.weylandt at gmail.com
Fri Apr 6 18:42:00 CEST 2012
Usually you can just use cor() and it will do all the possibilities directly:
x <- matrix(rnorm(100), ncol = 10)
cor(x)
But that works on the columns, so you'll need to transpose things if
you want all possible row combinations: cor(t(x))
Hope this helps,
Michael
On Fri, Apr 6, 2012 at 9:57 AM, Cserháti Mátyás <cs_matyi at yahoo.com> wrote:
> Hello everyone,
>
> My name is Matthew and I'm new to this list. greetings to everyone.
> Sorry if I'm asking an old question, but I have an m x n matrix where the rows are value profiles and the columns are conditions.
> What I want to do is calculate the correlation between all possible pairs of rows.
>
> That is, if there are 10 rows in my matrix, then I want to calculate 10 x 10 = 100 correlation values (all against all).
> Now R is slow when I use two for loops.
> What kind of other function or tool can I use to get the job done more speedily?
> I've heard of tapply, lapply, etc. and by and aggregate.
>
> Any kind of help is gladly appreciated.
>
> Thanks,
>
>
> Matthew
>
[Deleted the unnecessary digest]
More information about the R-help
mailing list