I have data with 3 relevant variables: The first is an activity (x1), the second is the respondents rating of that activity (x2), and the third is the proper name of the activity in x1 (x3). The x1 variables are respondent written, and very close matches to the reference variable of the activity x3, but all a little bit different. I would like to match and replace all x1’s with the reference x3 – I was thinking of using a loop referring to each reference activity x3 and replacing the x1 respondent written activity using a program like agrep. However, agrep seems only to tell me what the matches are. How can I replace the x1 variables with the “correct” string title in x3?
Share
in R, the function
agrepreturns the indices where it found a match, not the number of matchesIf you would like to have the value instead of the index, you can pass
value=TRUE.EDIT after your update:
If
x1andx3are in phase (for each index you have the names of the same activity) here is a snippet that will do the trick.If they are not in phase you can create the
oldandnewvectors as follows withagrep.