korone
korone

Reputation: 125

Counting number of records with condition

I have a dataframe with an ID column and I want R to count the number of observations NOT containing the dot character.

Here is a small data sample:

df <- structure(list(ID =  c("1111.AA","2222.CC","7891.DD","0055","00111.ZZ","00235.WQ", "UUUT", "0057.A", "1100")), .Names=c("ID"), 
            row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9"), class = ("data.frame"))

R should return 3 based on this sample.

Upvotes: 0

Views: 53

Answers (2)

swiftg
swiftg

Reputation: 346

length(which(!grepl('\\.',df$ID)))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388962

Multiple ways to find this out.

We can find number of observations in ID that has a dot character using grepl and then invert the results and get the sum

sum(!grepl("\\.", df$ID))
#[1] 3

If we want the values instead

df[!grepl("\\.", df$ID),]
#[1] "0055" "UUUT" "1100"

Using grep with invert = TRUE

length(grep("\\.", df$ID, invert = TRUE))
#[1] 3

grep("\\.", df$ID, invert = TRUE, value = TRUE)
#[1] "0055" "UUUT" "1100"

Using str_count from stringr package

library(stringr)
sum(!str_count(df$ID, "\\."))
#[1] 3

df[!str_count(df$ID, "\\."), ]
#[1] "0055" "UUUT" "1100"

Upvotes: 3

Related Questions