I have a data frame as follows:
a <- c(1, 2, 3, 4)
b <- c("AA; AA; BC", "BC; DE", "AA; BC; BC", "DE; DE")
df <- data.frame(a,b)
I want to count the number of unique two-letter combinations in each string in column b. So the correct answer would be 2, 2, 2, 1.
If I create a vector outside of the df
test <- c("AA", "AA", "BC")
then
y <- length(stri_unique(test))
y correctly returns 2. But if I try to implement that in the df:
df <- mutate(df, new_column = length(stri_unique(df$b)))
It returns an integer of 1024 for every row, which is definitely not right; the right answer would be 2, 2, 2, 1. Trying to understand why it breaks like this. Have tried specifying sep = ";" but then I just get an error that 2 arguments are passed to length which takes one argument. Any advice appreciated.