[R] Find the first occurrence in a list

Bert Gunter bgunter.4567 at gmail.com
Mon Mar 14 19:09:41 CET 2016


1.Avoid using the name "data" -- it's already a commonly used function in R.

2. Why do you want it to be a list instead of a vector? Is there a
good reason for this?


> d<- c(rep(0, 20), rep(1, 340))
> which(d>0)[1]
[1] 21

3. If you haven't already done so, spend some time with an R tutorial
or two. They typically cover things like this. There are many good
ones on the Web.


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Mon, Mar 14, 2016 at 10:41 AM, Glenn Schultz <glennmschultz at me.com> wrote:
> All,
> I am looking to find the first principal payment date on structured MBS cash flow.  I am testing the below to make into a function the correct index is returned.  I am a little unsure. Is this the correct way to think about this problem or is there something already in R that can help?
> Glenn
>  data <- as.list(c(rep(0, 20), rep(1, 340)))
>   f <- function(x){x != 0}
>   which(sapply(data, f ) == TRUE)[1]
>         [[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

More information about the R-help mailing list