-3

Want to remove all special symbol in data frame in r

The data is:

No str count

1 car. 2

1 .car 1

2 fish# 1

2 car 1

It should be:

No str count

1 car 3

1 . 2

2 fish 1

2 car 1

2 # 1

I have tried to split it but it doesn't work

Can anyone help?

David
  • 7
  • 2
  • https://stackoverflow.com/a/10294818/7547327 – mrhellmann Oct 11 '20 at 13:02
  • 1
    Does this answer your question? [Remove all special characters from a string in R?](https://stackoverflow.com/questions/10294284/remove-all-special-characters-from-a-string-in-r) – mrhellmann Oct 11 '20 at 13:02
  • How did your data go from 4 rows to 5? And why would you remove the period in "car." but not ".car", and introduce a special character in the last row when you say you want to remove them? – camille Oct 12 '20 at 23:20

2 Answers2

0

Will this work:

> df
  No   str count
1  1  car.     2
2  1  .car     1
3  2 fish#     1
4  2   car     1
> df$str <- gsub('[.#]','',df$str)
> df
  No  str count
1  1  car     2
2  1  car     1
3  2 fish     1
4  2  car     1
> 

We can also use Duck's code without gsub:

> df
  No   str count
1  1  car.     2
2  1  .car     1
3  2 fish#     1
4  2   car     1
> df$str <- trimws(df$str, whitespace = '[\\.#]')
> df
  No  str count
1  1  car     2
2  1  car     1
3  2 fish     1
4  2  car     1
> 
Karthik S
  • 11,348
  • 2
  • 11
  • 25
0

You can also try this base R solution:

#Code
df$str <- trimws(gsub('#','',df$str,fixed = T),whitespace = '\\.')

Output:

df
  No  str count
1  1  car     2
2  1  car     1
3  2 fish     1
4  2  car     1

Some data used:

#Data
df <- structure(list(No = c(1L, 1L, 2L, 2L), str = c("car.", ".car", 
"fish#", "car"), count = c(2L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-4L))

Or this:

#Code2
df$str <- gsub('[[:punct:]]','',df$str)

Output:

df
  No  str count
1  1  car     2
2  1  car     1
3  2 fish     1
4  2  car     1
Duck
  • 39,058
  • 13
  • 42
  • 84