lundi 18 novembre 2019

Using an element's relevant position in a vector to find its index

Let's say I have a column in one set of data like

raw[[1]]$Item.Name
 [1] Day 1 (1/18/15)         Breakfast           Morning Snack       Lunch               Afternoon Snack
 [6]     Dinner              Evening Snack   Day 2 (1/19/15)         Breakfast           Morning Snack  
[11]     Lunch               Afternoon Snack     Dinner              Evening Snack   Day 3 (1/20/15)    
[16]     Breakfast           Morning Snack       Lunch               Afternoon Snack     Dinner         
[21]     Evening Snack   Average     

and in other sets, this column is missing eating periods for certain days (e.g. Afternoon Snack for Day 2 will not be there for Participant 5; hence there will be 21 elements).

Can I do something like grep where I can stipulate: I only want the Afternoon Snack if it is between Day 1 and 2? (The way I have already done this is to use if statements where I compare grep's index for afternoon snack by its index for Day X. However this is not elegant and I had to work around problems where there may be no Day X).

Another way to approach this: is there a way/function to append Day 1 to all the eating periods between Day 1 and 2?

Aucun commentaire:

Enregistrer un commentaire