Shaxi Liver Shaxi Liver - 3 months ago 8
R Question

Calculate the number of the same strings in the column - remove odd counts

I would like to calculate number of the same strings in the column and if the number is odd remove all the rows with such string. For example:

set.seed(1)
df <- data.frame(
Name = c(rep("Mark", 6), rep("Tom", 8), rep("Tim", 12), rep("Greg", 3), rep("Matt", 3)),
rand = rnorm(32, 0, 1))


Take a look on the column
Name
. As you see
Greg
and
Matt
appear 3 times (odd number), therefore, they should be removed.

Answer

Using table

EvenNames = names(table(DF$Name))[as.vector(table(DF$Name))%%2==0]
DF = DF[DF$Name %in% EvenNames,]
Comments