0

I want to replace char value in R with another, it should be like this:

The key word to work is "Texas". And there is any way that I can do it with multiple files at once.

I have tried like this: df[df=="121 Texas"]<-"Texas" but it's clearly not smart at all when it comes to work with many dataframe at the same time.

D.J
  • 1,180
  • 1
  • 8
  • 17
Cothd
  • 7
  • 3

1 Answers1

0

Let's say you had a dataframe with a column named "Original" and you wanted to replae all the entries in that column that contained the letters "Texas" as a substring with just the character value "Texas":

  df$Original[ grepl("Texas", df$Original) ] <- "Texas"

The grepl is one of the regex functions. It returns a logical value. The "[" function on the LHS of "<-" (technically the "[<-" function) can accept a logical index and only do the replacements for those items that register TRUE in the grepl test.

IRTFM
  • 258,963
  • 21
  • 364
  • 487