I am trying to split a string and then check if all its elements are unique. So far I have done the following:
df <- data.frame(
"id" = c("001;002;003", "001;001;001", "001;001;002"),
"v1" = c("a", "b", "c")
)
sapply(strsplit(df$id, ";"), function(x) unique(x) %in% df$id)
My intention is to have an output of FALSE
, TRUE
, and FALSE
, respectively for "001;002;003"
, "001;001;001"
, and "001;001;002"
Thanks in advance!!
We can check on the unique
length
sapply(strsplit(df$id, ";"), function(x) length(unique(x))== 1)
[1] FALSE TRUE FALSE
or with n_distinct
from dplyr
library(dplyr)
library(tidyr)
df %>%
separate_rows(id) %>%
group_by(v1) %>%
summarise(flag = n_distinct(id) == 1)
-output
# A tibble: 3 x 2
v1 flag
<chr> <lgl>
1 a FALSE
2 b TRUE
3 c FALSE
Or using table
sapply(strsplit(df$id, ";"), function(x) length(table(x)) == 1)
[1] FALSE TRUE FALSE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With