Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete rows with value frequencies lesser than x in R

I got a data frame in R like the following:

V1 V2 V3
1  2  3
1  43 54
2  34 53
3  34 51
3  43 42
...

And I want to delete all rows which value of V1 has a frequency lower then 2. So in my example the row with V1 = 2 should be deleted, because the value "2" only appears once in the column ("1" and "3" appear twice each).

I tired to add a extra column with the frequency of V1 in it to delete all rows where the frequency is > 1 but with the following I only get NAs in the extra column.

data$Frequency <- table(data$V1)[data$V1]

Thanks

like image 810
dju Avatar asked Dec 31 '25 05:12

dju


1 Answers

You can try this:

library(dplyr)
df %>% group_by(V1) %>% filter(n() > 1)
like image 200
Gopala Avatar answered Jan 01 '26 18:01

Gopala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!