Problem with allp ossible combination.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with allp ossible combination.

Arun.stat
Dear R Users,

Suppose I have a dataset like this:

  a       b

39700   485.00
39300   485.00
39100   480.00
38800   487.00
38800   492.00
39300   507.00
39500   493.00
39400   494.00
39500   494.00
39100   494.00
39200   490.00

Now I want get a-b for all possible combinations of a and b. Using two 'for'
loop it is easy to calculate. But problem arises when row length of the data
set is large eg. 1000 or more. Then R takes lot of time to do that. Can
anyone please tell me whether there is any R-function to do such kind of job
quickly?

Thanks and regards,

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with allp ossible combination.

Ido Tamir

>Now I want get a-b for all possible combinations of a and b.

outer(a,b,"-")

hth
ido

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with allp ossible combination.

Jacques Veslot-2
In reply to this post by Arun.stat
diffs <- do.call(expand.grid, dt)
diffs$delta <- rowSums(expand.grid(dt$a, -dt$b))
-------------------------------------------------------------------
Jacques VESLOT

CNRS UMR 8090
I.B.L (2ème étage)
1 rue du Professeur Calmette
B.P. 245
59019 Lille Cedex

Tel : 33 (0)3.20.87.10.44
Fax : 33 (0)3.20.87.10.31

http://www-good.ibl.fr
-------------------------------------------------------------------


Arun Kumar Saha a écrit :

> Dear R Users,
>
> Suppose I have a dataset like this:
>
>   a       b
>
> 39700   485.00
> 39300   485.00
> 39100   480.00
> 38800   487.00
> 38800   492.00
> 39300   507.00
> 39500   493.00
> 39400   494.00
> 39500   494.00
> 39100   494.00
> 39200   490.00
>
> Now I want get a-b for all possible combinations of a and b. Using two 'for'
> loop it is easy to calculate. But problem arises when row length of the data
> set is large eg. 1000 or more. Then R takes lot of time to do that. Can
> anyone please tell me whether there is any R-function to do such kind of job
> quickly?
>
> Thanks and regards,
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.