I am trying to filter the Symbol column based on whether it's of the form \uxxxx
This is easy visually, that is, some look like $
, ¢
, £
, and others like \u058f
, \u060b
, \u07fe
.
But I cannot seem to figure it out using stringi
/ dplyr
library(dplyr)
library(stringi)
df <- structure(list(Character = c("\\u0024", "\\u00A2", "\\u00A3",
"\\u00A4", "\\u00A5", "\\u058F", "\\u060B", "\\u07FE", "\\u07FF",
"\\u09F2", "\\u09F3", "\\u09FB", "\\u0AF1", "\\u0BF9", "\\u0E3F",
"\\u17DB", "\\u20A0", "\\u20A1", "\\u20A2", "\\u20A3"),
Symbol = c("$", "¢", "£", "¤", "¥", "\u058f", "\u060b", "\u07fe", "\u07ff",
"৲", "৳", "\u09fb", "\u0af1", "\u0bf9", "฿", "៛", "₠",
"₡", "₢", "₣")), row.names = c(NA, 20L), class = "data.frame")
Character Symbol
1 \\u0024 $
2 \\u00A2 ¢
3 \\u00A3 £
4 \\u00A4 ¤
5 \\u00A5 ¥
6 \\u058F \u058f
7 \\u060B \u060b
8 \\u07FE \u07fe
9 \\u07FF \u07ff
10 \\u09F2 ৲
11 \\u09F3 ৳
12 \\u09FB \u09fb
13 \\u0AF1 \u0af1
14 \\u0BF9 \u0bf9
15 \\u0E3F ฿
16 \\u17DB ៛
17 \\u20A0 ₠
18 \\u20A1 ₡
19 \\u20A2 ₢
20 \\u20A3 ₣
What I've tried
I have tried using variations on nchar
but haven't had luck
df$Symbol %>% nchar
# [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
df$Symbol %>% stri_unescape_unicode %>% nchar
# [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
df$Symbol %>% stri_escape_unicode %>% nchar
# [1] 1 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
Question
How can I filter on the Symbol column for all the rows of the form $
, ¢
, £
etc (and conversely for rows like \u058f
, \u060b
, \u07fe
)?
Symbol
column (I feel like it should be enough, but I can't work out how to distinguish - which is interesting because it's so easy for human eyes to see) – SceptreTRUE
/FALSE
as to whether it's valid unicode, or is there another way? – Sceptreutf8::utf8_valid()
but this may not distinguish between existing valid unicode and unicode that is valid but unassigned. Can you expand a little on what you're ultimately trying to achieve? – Impassion