How do I remove certain special character in my column

41 Views Asked by At

Currently I have a column containing these special characters:

N700��

N713��

And I was wondering if there was a way to remove them?

test_df is the name of the dataframe and I'm trying to remove the special characters from these rows using the following:

test_df$TailNum <- gsub("[^[:alnum:][:space:]]", "", test_df$TailNum)

Error in gsub("[^[:alnum:][:space:]]", "", test_df$TailNum) : input string 1 is invalid In addition: Warning message: In gsub("[^[:alnum:][:space:]]", "", test_df$TailNum) : unable to translate 'N700' to a wide string

test_df[!grepl("[^[:alnum:][:space:]]", test_df$TailNum), ]

Error in df[!grepl("[^[:alnum:][:space:]]", test_df$TailNum), ] : object of type 'closure' is not subsettable In addition: There were 50 or more warnings (use warnings() to see the first 50)

3

There are 3 best solutions below

2
Jay Bee On
test_df <- data.frame(
  id = c(1, 2, 3, 4, 5),
  value = c("N289", "N182", "N700��", "N713��", "N895")
)

test_df <- test_df %>%
  mutate(value = gsub("�+", "", value))

For output:

 id  value
1  1 N289
2  2 N182
3  3 N700
4  4 N713
5  5 N895
0
VinceGreg On

Since my comment got a good feedback from the original poster, here is an answer. This answer remove non-ASCII character with iconv(). With a dplyr::mutate():

data.frame(
  value = c("N289", "N182", "N700��", "N713��", "N895")
) %>% mutate( value2 = iconv(value, "latin1", "ASCII", sub="")) 
0
Chris Ruehlemann On

If the special chars you want to remove are non-ASCII chars, here's a nice pattern [^ -~] to get rid of them:

library(stringr)
test_df %>%
  mutate(value = str_remove_all(value, "[^ -~]"))