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

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Tue 27 Jun 2006 - 03:59:30 EST

On Mon, 26 Jun 2006, markleeds@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.

An easier way to do this is

   mapply(seq,tempa,tempb)

Your approach should have worked. It's hard to tell why it didn't because there are two syntax errors in your example so it clearly isn't actually what you did. Fixing them, I get
> sapply(1:length(tempa), function (z) seq(tempa[z],tempb[z]))
[[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 32 33 34 [26] 35 36 37 38 39

as you wanted.

         -thomas



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jun 27 05:21:49 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 27 Jun 2006 - 06:12:51 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.