I'd like to count a defined pattern (here: 'Y') in a string for each row of a dataframe. Ideally, I'd like to get a number of occurrences in V3 and length in V4.
Input:
V1 V2
A XXYYYYY
B XXYYXX
C XYXXYX
D XYYXYX
Output:
V1 V2 V3 V4
A XXYYYYY 1 5
B XXYYXX 1 2
C XYXXYX 2 1,1
D XYYXYX 2 2,1
I tried different modifications of the function below, with no success.
dict <- setNames(nm=c("Y"))
seqs <- df$V2
sapply(dict, str_count, string=seqs)
Thanks in advance!