[R] probably need to se sapply but i can't get it

Marc Schwartz (via MN) mschwartz at mn.rr.com
Mon Jun 26 19:54:15 CEST 2006


On Mon, 2006-06-26 at 12:40 -0500, markleeds at verizon.net wrote:
> Hi : I think I need to use sapply but I can't figure this out.
> 
> Suppose I have two vectors : tempa ( 4, 6,10 ) and  tempb 
> (  11,23 ,39 ) 
> 
> 
> I want a function that returns 4:11,6:23 and 10:39 as vectors.
> 
> I tried :
> 
> sapply(1:length(tempa) function (z) seq(tempa[z],tempb[z])
> 
> but i got 3 really strange vectors back in the sense that the numbers
> in them did not make no sense to me. obviously,
> i must be doing something wrong.  thanks a lot.
> 
>                                        mark


Mark,

Try this using mapply():

 tempa <- c(4, 6, 10)

 tempb <- c(11, 23, 39)


> mapply(seq, from = tempa, to = tempb)
[[1]]
[1]  4  5  6  7  8  9 10 11

[[2]]
 [1]  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

[[3]]
 [1] 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
[23] 32 33 34 35 36 37 38 39


You will get a list back in this case and you can then deal with the 3
vectors as you require. Each vector is a different length, so a list is
about the only way to return them here.

See ?mapply for more info.

HTH,

Marc Schwartz



More information about the R-help mailing list