Home > other >  how to keep those strings that are repeated more than once
how to keep those strings that are repeated more than once

Time:11-04

I have a data like this

mydf<- structure(list(rn = c("AAAA", "CDSD", "HUMN", "AAAA", "CRAT", 
"CAMSAP3", "RRR", "RRR", "AAAA", "AAAA", "NPRL2", "CDSD", "RAD18", 
"THAL", "BRO"), values = c(0.744760161, 0.741827081, 0.737886634, 
0.72658262, 0.71792587, 0.71787455, 0.674992626, 0.674716174, 
0.670139778, 0.668368874, 0.668332846, 0.663282622, 0.659781148, 
0.656606592, 0.656403592)), class = "data.frame", row.names = c(NA, 
-15L))

I am trying to remove those that are repeated 1 time and keep those that are more than once based on r column

I was trying something like this

mydf$value_count <- rowSums(!is.na(mydf[-1])) 
mydf2 <- df[df$value_count > 1,]

I cannot figure it out

CodePudding user response:

We may use subset from base R

subset(mydf, rn %in% names(which(table(rn) > 1)))

Or filter from dplyr

library(dplyr)
mydf %>%
    filter(rn %in% names(which(table(rn) > 1)))
  •  Tags:  
  • r
  • Related