Skip to content
Back to formatted view

Raw Message

Message-ID: <BANLkTinQNXz01zb01eKHXXwWMTVJpfKbpA@mail.gmail.com>
Date: 2011-04-08T07:58:15Z
From: Juan Carlos BorrĂ¡s
Subject: Avoiding a loop
In-Reply-To: <BANLkTikw=3T1mdZ=_59aWAQ7eA-FVDaKBw@mail.gmail.com>

Kenn,
I find your solution more elegant.

2011/4/8 Kenn Konstabel <lebatsnok at gmail.com>:
> 2011/4/8 Juan Carlos Borr?s <jcborras at gmail.com>:
>> #Use the indexes of S in a sapply function.
>>
>> N <- 10
>> S <- sample(c(0,1), size=N, replace=TRUE)
>> v1 <- sapply(c(1:N-1), function(i) S[i]&&S[i+1])
>
> You can achieve the same v1 using
>
> v1.2 <- ?S[2:N-1] & S[2:N]
>
> .. or if you insist on having NA as the first element, -- c(NA, v1.2)
>
> Vectorization is more efficient than loops but this need not be true
> for the *apply functions.
>
>>
>> # Then
>> v2 <- (P > m)
>>