1

A column has different values, one of which includes value *. I want to replace that with U.

data looks something like this : col f f g h * t *

i

I have tried below options:

replace( df$col, "*", "U")
data$PROP_TYPE[df$col=='*'] <- 'U'

Both do not work

Rudra
  • 149
  • 1
  • 2
  • 11
  • 1
    Please include at least a few rows of sample data from your data frame. – Tim Biegeleisen Apr 12 '19 at 13:20
  • 2
    Possible duplicate of [How do I deal with special characters like \^$.?\*|+()\[{ in my regex?](https://stackoverflow.com/questions/27721008/how-do-i-deal-with-special-characters-like-in-my-regex) – NelsonGon Apr 12 '19 at 13:33

2 Answers2

0

replace needs to know which elements to replace (the x == "*"):

x <- c("*", "a", "*", "a", "*", "a", "*", "a", "B")
replace(x, x == "*", "U")
#[1] "U" "a" "U" "a" "U" "a" "U" "a" "B"

In data.frame

df <- data.frame(x = x, stringsAsFactors = F)
df[df$x == "*", "x"] <- "U"
Patrik_P
  • 3,066
  • 3
  • 22
  • 39
0

Not sure how often * shows up in your data frame, but you can use gsub to replace all instances of it.

Say you have a column like this:

df <- c("asd*asd", "432*432", "*")
[1] "asd*asd" "432*432" "*" 

If you want to replace all the * with U then you can run this

gsub("\\*", "U", df)
[1] "asdUasd" "432U432" "U"   

Since * is a special character, you have to escape it using \\ or else it will not give you what you're looking for.

Seb
  • 11
  • 4