Tej Pradhan
Tej Pradhan

Reputation: 1

How do I filter a column of character strings based on their first and 3rd characters?

I've got a dataset of ICD codes, 5 digit codes which describe the diagnosis of a hospital patients, which looks like this (but with 16000 patients):

df <- c('S48.10', 'H38.13', 'R40.12')

I need to filter them based on the first character being an S and the third character being an 8 to filter out amputation patients. Any advice on how to do this?

Upvotes: -1

Views: 695

Answers (1)

akrun
akrun

Reputation: 887711

Here is an option with grepl on the ICD column. The pattern used is 'S' followed by any character (.) followed by 8 from the start (^) of the string to filter the rows of the datasest

subset(df1, grepl('^S.8', ICD))

Upvotes: 0

Related Questions