Home > Net >  R, charater with <U 200B>
R, charater with <U 200B>

Time:06-07

I imported a table from an excel.

If I open this table (as dataframe) from the environment it looks fine:

Name  Ser_NR 
ABC   123
DEF   458
EGS   954

However, I noted that for some rows, the filter function doesn't work. Then I put this dataframe into Console, It shows me this:

Name  Ser_NR 
ABC   123
DEF   458<U 200B>
EGS   954 <U 200B>

The type of this column "Ser_NR" is character. I want "remove" <U 200B> or change the type to normal character.

Can anyone help me? Thanks!

CodePudding user response:

Try

df$Ser_NR <- as.integer(df$Ser_NR)
df

CodePudding user response:

Here's a solution with regex:

library(dplyr)

df <- tibble::tribble(
         ~Name,       ~Ser_NR,
         "ABC",         "123",
         "DEF", "458<U 200B>",
         "EGS", "954<U 200B>"
         )

df %>% 
  mutate(Ser_NR = gsub("<([[:alpha:]][[:alnum:]]*)(.[^>]*)>([.^<]*)", "\\3", Ser_NR))

Which gives us:

# A tibble: 3 x 2
  Name  Ser_NR
  <chr> <chr> 
1 ABC   123   
2 DEF   458   
3 EGS   954
  • Related