[R] replacing a character string

Rui Barradas ruipbarradas at sapo.pt
Sun Aug 12 17:27:08 CEST 2012


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:
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list