R - count matches between characters of one string and another, no replacement -



R - count matches between characters of one string and another, no replacement -

i have keyword (e.g. 'green') , text ("i not them sam am!").

i'd see how many of characters in keyword ('g', 'r', 'e', 'e', 'n') occur in text (in order).

in illustration reply 3 - text doesn't have g or r has 2 es , n.

my problem arises if character in text matched character in keyword, can't used match different character in keyword.

for example, if keyword 'greeen', number of "matching characters" still 3 (one n , 2 es) because there 2 es in text, not 3 (to match 3rd e in keyword).

how can write in r? ticking @ border of memory - sense it's mutual problem worded differently (sort of sampling no replacement, "matches no replacement"?).

e.g.

keyword <- strsplit('greeen', '')[[1]] text <- strsplit('idonotlikethemsamiam', '')[[1]] # how many characters in keyword have matches in text, # no replacement? # effort 1: sum(keyword %in% text) # problem: returns 4 (all 3 es match, 2 in text)

more examples of expected input/outputs (keyword, text, expected output):

'green', 'idonotlikethemsamiam', 3 (g, e, e) 'greeen', 'idonotlikethemsamiam', 3 (g, e, e) 'red', 'idonotlikethemsamiam', 2 (e , d)

the function pmatch() great this. though instinctual utilize length here, length has no na.rm option. work around nuisance, sum(!is.na()) used.

keyword <- unlist(strsplit('greeen', '')) text <- unlist(strsplit('idonotlikethemsamiam', '')) sum(!is.na(pmatch(keyword, text))) # [1] 3 keyword2 <- unlist(strsplit("red", '')) sum(!is.na(pmatch(keyword2, text))) # [1] 2

r

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -