samedi 12 septembre 2020

How to efficiently check the value of multiple variables against a list of values in R?

I want to create a variable that flags whether one or more of multiple variables has a particular value.

week  Mon  Tues  Weds  Thurs  Fri  Sat
1     jon  jon   jon   jon    mary mary
2     jane jane  jane  jane   jane jane
3     mary mary  mary  mary   mary jane

I want to create a binary variable that flags for each week whether any day that week == "jon" or "mary" Is there a way to do this without creating a long ifelse statement that checks each variable individually?

week  Mon  Tues  Weds  Thurs  Fri  Sat  flag
1     jon  jon   jon   jon    mary mary 1
2     jane jane  jane  jane   jane jane 0
3     mary mary  mary  mary   mary jane 1

Aucun commentaire:

Enregistrer un commentaire