Assuming the following data, I want to count the unique characters per row.
test <- data.frame(oe = c("A-1", "111", "-", "Sie befassen sich intensiv damit"))
So I thought I'm using the [:graph:] helper to capture letters, numbers and punctuation. However, it gives the wrong results, see below:
library(tidyverse)
test %>%
mutate(unique_chars_correct = sapply(tolower(oe), function(x) sum(str_count(x, c(letters, 0:9, "-")) > 0)),
unique_chars_wrong = sapply(tolower(oe), function(x) sum(str_count(x, "[:graph:]") > 0)))
which gives:
oe unique_chars_correct unique_chars_wrong
1 A-1\\. 3 1
2 111 1 1
3 - 1 1
4 Sie befassen sich intensiv damit 13 1
I assume, using [:graph:] kind of checks if any of the chars satisfies being part of [:graph:], but want to do is to check every element that is part of [:graph:].