Shaxi Liver Shaxi Liver - 1 year ago 56
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:

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
. As you see
appear 3 times (odd number), therefore, they should be removed.

Answer Source

Using table

EvenNames = names(table(DF$Name))[as.vector(table(DF$Name))%%2==0]
DF = DF[DF$Name %in% EvenNames,]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download