Merging vectors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Merging vectors

Mago84
Dear R addicts,

I´m trying to merge two vectors like these and I don´t want to use loops.

EV
 [1]   9  16  19  21  67  71  82  87 110 112 141 156 201 211 248 264 272 298 331
[20] 341 347 361 373 468 490 493 504 508 806 830 833 883 885 897 953 955

SV
 [1]   3  21  28  33 229 379 396 532 535 541 630 634 643 645 647 654 656 677 680
[20] 685 687 718 727 735 738 746 748 751 766 768 903 926 936 995 999

I need to obtain something like this:
9, 21, 21, 28, 67, 229, 248, 379..

Taking one by one of each vector in order.  EV is always the first one.

I have never use the function merge, I tried to look for a piece of information, but I´m not sure that it´s what I should use.

Could somebody help me?

Thanks a lot!
Reply | Threaded
Open this post in threaded view
|

Re: Merging vectors

William Dunlap
Does the following do what you want?  You didn't
describe the rule and the example output was truncated:

EV <- c(
   9,16,19,21,67,71,82,87,110,112,141,156,201,211,248,264,272,298,331,
   341,347,361,373,468,490,493,504,508,806,830,833,883,885,897,953,955)

SV <- c(
   3,21,28,33,229,379,396,532,535,541,630,634,643,645,647,654,656,677,680,
   685,687,718,727,735,738,746,748,751,766,768,903,926,936,995,999)

f1 <- function(EV, SV) {
   names(EV) <- rep("EV", length(EV))
   SV <- SV[SV >= min(EV)]
   names(SV) <- rep("SV", length(SV))
   z <- sort(c(SV, EV))
   nms <- names(z)
   unname(z[c(TRUE, nms[-1] != nms[-length(z)])])
}

f1(EV,SV)
# gives [1]   9  21  21  28  67 229 248 379 468 532 806 903 953 995
# wanted 9, 21, 21, 28, 67, 229, 248, 379..

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of Mago84
> Sent: Friday, December 30, 2011 9:20 AM
> To: [hidden email]
> Subject: [R] Merging vectors
>
> Dear R addicts,
>
> I´m trying to merge two vectors like these and I don´t want to use loops.
>
> EV
>  [1]   9  16  19  21  67  71  82  87 110 112 141 156 201 211 248 264 272 298
> 331
> [20] 341 347 361 373 468 490 493 504 508 806 830 833 883 885 897 953 955
>
> SV
>  [1]   3  21  28  33 229 379 396 532 535 541 630 634 643 645 647 654 656 677
> 680
> [20] 685 687 718 727 735 738 746 748 751 766 768 903 926 936 995 999
>
> I need to obtain something like this:
> 9, 21, 21, 28, 67, 229, 248, 379..
>
> Taking one by one of each vector in order. * EV is always the first one*.
>
> I have never use the function merge, I tried to look for a piece of
> information, but I´m not sure that it´s what I should use.
>
> Could somebody help me?
>
> Thanks a lot!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Merging-vectors-tp4246255p4246255.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.