[R] matching element of a vector to i-2nd element

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Jul 23 21:50:53 CEST 2017


You can compare the elements that make sense to compare, and fill in the ones that don't make sense to compare yourself using the c function. 

Hint: no looping or if function are necessary.  

v[ seq( 2, length( v ) ] == v[ seq.int( length( v ) - 2 ) ]
-- 
Sent from my phone. Please excuse my brevity.

On July 23, 2017 12:04:00 PM PDT, Davide Piffer <pifferdavide at gmail.com> wrote:
>No homework. Just a genuine question
>
>On 23 July 2017 at 22:00, Bert Gunter <bgunter.4567 at gmail.com> wrote:
>> Homework?? There is a no homework policy on this list.
>>
>> Cheers,
>> Bert
>> 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 Sun, Jul 23, 2017 at 11:43 AM, Davide Piffer
><pifferdavide at gmail.com> wrote:
>>> I have a df with a vector v. For each element of the vector, I want
>to
>>> know whether the i-2nd element is the same as the ith element. For
>>> example:
>>> given
>>> v=c(A,C,D,C) the result should be:
>>> FALSE,FALSE,FALSE,TRUE.
>>>
>>> I attempted something using indexing in a for loop such as (bad,
>>> incorrect example):
>>>  for (i in v){
>>>   if [i]==[i-2] print T
>>> else print F
>>> }
>>>
>>> However, this is obviously wrong.
>>> Can someone provide a nice way to solve this?
>>>
>>> ______________________________________________
>>> 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.
>
>______________________________________________
>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