Question

after a lot of trial/error and the search function I am still somewhat clueless about an I-thought-simple-thing (as always, hrmpf):

I have a column in a data frame x$question and within that column, there is an expression 'A/V' every once in a while, and I simply want it to be changed to 'A / B'.

I tried a little here and there, and thought this should work:

 x$question[agrep('A/V',x$question)]<-'A / B'

but I get the error:

In `[<-.factor`(`*tmp*`, agrep('A/V',  :
invalid factor level, NAs generated    

or I could do this

agrep('A/V','A / B', x$question).

But here I get the error:

Error in .amatch_bounds(max.distance) : 
match distance components must be non-negative

Since I am quite out of ideas, I would be very thankful, if you had a suggestions, or maybe an even simpler way of replacing a string with another string.

Was it helpful?

Solution

Does this work?

 gsub("A/V","A/B",x$question)

Example:

x<-c("A/V", "A/V", "A/V")
x<-gsub("A/V","A/B",x)
>x
[1] "A/B" "A/B" "A/B"

Note: You can use ifelse for that too.

> ifelse(x=="A/B","A/V",x)
[1] "A/V" "A/V" "A/V"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top