1
votes

I'm using R for data analysis and I have a string variable that lists the order in which measures were administered in a survey. Here is the string variable for one respondent:

"pimgrwelcomerealstartnamelessTaskinstruct_itemshealth_itemsinstruct_selfsexp_instiat_esteemdebriefing1lastpage"

Is there some way I can use "grepl" to test the order of these measures? For example, can I see if "health_items" came earlier in the string than "instruct_self" (which is true in the string above)? If so, I'd like to create a dummy variable so I can test order effects.

Thanks everyone.

1

1 Answers

2
votes

I'm not writing this as an answer but just to share my thinking on this problem: Hope someone helps with grepl

You can extract the location of a substring using 2 ways which can be used to check if first string came ahead of second :

regexpr("health_items", s). #where s is your string
# or
library(stringr)
str_locate(s, "health_items")

So to know which came first:

sapply( c("health_items", "instruct_self"), function(x) str_locate(s, x))
# this should return a vector with the start index.