Skip to content

replacing a character string

3 messages · toehanus, Rui Barradas, David Winsemius

#
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!

Thomas



--
View this message in context: http://r.789695.n4.nabble.com/replacing-a-character-string-tp4639903.html
Sent from the R help mailing list archive at Nabble.com.
2 days later
#
Hello,

I believe I don't understand your problem. Is this your input datum?

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

If so the following will do it:

(search <- paste("(", 1:7, ",)", sep = ""))
(replace <- paste("\\1 ", LETTERS[1:7], ".", sep = ""))
xx <- x
for(i in 1:7)
     xx <- gsub(search[i], replace[i], xx)
xx

If not, say so.
My doubt is in the input. Is the vertical bar a newline or row marker? 
And the comma?

Hope this helps,

Rui Barradas

Em 10-08-2012 15:18, toehanus escreveu:
#
On Aug 10, 2012, at 7:18 AM, toehanus wrote:

            
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" )