ConanG
ConanG

Reputation: 749

R: Count number of rows in data frame, with matching character in specified position of string

I have a data frame with a column with characters:

  strings
1 a;b;c;d
2 g;h;i;j
3     k;m
4       o

I would like to get a count of the number of strings(rows) with a certain specified characters at a certain position within the string.

Eg.

Get count of number of strings with 3rd character as one of the characters in this set: {a,b,m}.

The output should be 2 in this case, since only the 1st and 3rd row have any characters in {a,b,m} as their 3rd character within the string.

I could only use this code to find any strings that contains 'b':

sum(grepl("b",df))

However, this is not good enough for the above task. Please advice.

Upvotes: 4

Views: 9420

Answers (2)

Colonel Beauvel
Colonel Beauvel

Reputation: 31171

You can try grepl:

x = c('a;b;c;d','g;h;i;j','k;m','o')

sum(grepl('^.{2}[abm]', x))
#[1] 2

Upvotes: 4

Sam Dickson
Sam Dickson

Reputation: 5239

Try this:

sum(substr(df$strings,3,3) %in% c("a","b","m"))

Alternatively, if you want to use a ; as the delimeter you can do:

sum(sapply(strsplit(df$strings,";"),function(x) x[2] %in% c("a","b","m")))

Upvotes: 1

Related Questions