[R] replacing a character string

David Winsemius dwinsemius at comcast.net
Sun Aug 12 20:44:06 CEST 2012


On Aug 10, 2012, at 7:18 AM, toehanus wrote:

> I am working on modifying a REDCap survey.  The data dictionary  
> column for
> the response field has the following value.
>
> 1, Strongly disagree | 2, Disagree | 3, Agree | 4, Strongly Agree |  
> 5, Don't
> Know | 6, Refuse to Answer | 7, Not Applicable
>
> I am wanting to convert this so that it looks as follows:
>
> 1, A. Strongly disagree | 2, B. Disagree | 3, C. Agree | 4, D.  
> Strongly
> Agree | 5, E. Don't Know | 6, F. Refuse to Answer | 7, G. Not  
> Applicable
>
> Not all responses have these same values.  Some questions are Yes/No  
> answers
> and some have up to 13 different response options.  I have been  
> trying to
> use the sub function to do this, but with no real success.  I would
> appreciate any help that you can give.  If you need more  
> information, let me
> know.  Thanks!
>

txt <- "1, Strongly disagree | 2, Disagree | 3, Agree | 4, Strongly  
Agree | 5, Don't Know | 6, Refuse to Answer | 7, Not Applicable"

(If your reader inserts carriage returns, you will need to take them  
out by hand.)

strsplit(txt, split="[[:digit:]]\\,\\s" )
[[1]]
[1] ""                     "Strongly disagree | " "Disagree |  
"          "Agree | "
[5] "Strongly Agree | "    "Don't Know | "        "Refuse to Answer |  
"  "Not Applicable"

 > stxt <- .Last.value

  paste0( seq_along(stxt[[1]][-1]), ", ",
          LETTERS[seq_along(stxt[[1]][-1])], ". ",
          stxt[[1]][-1], collapse="")

[1] "1, A. Strongly disagree | 2, B. Disagree | 3, C. Agree | 4, D.  
Strongly Agree | 5, E. Don't Know | 6, F. Refuse to Answer | 7, G. Not  
Applicable"

If you didn't want it all as one string, then take out the collapse  
argument:

 > paste0( seq_along(stxt[[1]][-1]), ", ", LETTERS[seq_along(stxt[[1]] 
[-1])], ". ", stxt[[1]][-1])
[1] "1, A. Strongly disagree | " "2, B. Disagree | "          "3, C.  
Agree | "
[4] "4, D. Strongly Agree | "    "5, E. Don't Know | "        "6, F.  
Refuse to Answer | "
[7] "7, G. Not Applicable"

You might need to do another pass to take out the "|" characters,  
although that could also have been accomplished by splitting on a  
pattern that included it:

strsplit(txt, split="(\\|\\s)?[[:digit:]]\\,\\s" )

-- 

David Winsemius, MD
Alameda, CA, USA



More information about the R-help mailing list