Skip to content
Prev 303185 / 398503 Next

replacing a character string

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: