mercredi 8 août 2018

Recognising specific occurrences across columns to create a new variable in R

I have a data set that looks like the following

year  sh1  sh2  sh3  sh4  sh5
2011   0    1    1    0    0
2012   1    1    0    1    1
2013   0    0    0    0    0
2014   1    1    0    0    0
2015   1    1    1    1    1

I'm looking to create a new column that recognises when the number 1 appears in two or more consecutive columns between sh1 and sh5. If this pattern is recognised, it needs to sum up how many times the pattern occurs.

The output I'm after should look something like this:

year  sh1  sh2  sh3  sh4  sh5  newVariable
2011   0    1    1    0    0        1
2012   1    1    0    1    1        2
2013   0    0    0    0    0        0
2014   1    1    0    0    0        1
2015   1    1    1    1    1        1

Any help would be fantastic.

Thank you

Aucun commentaire:

Enregistrer un commentaire