Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicated group dplyr r

I have the following data frame:

df <- structure(list(GENE= c("ENS1", "ENS2", 
"ENS3", "ENS4", "ENS1",  "ENS2", "ENS3"), group= c(1L, 
1L, 1L, 2L, 3L, 3L, 3L)), 
class = "data.frame", row.names = c(NA, -7L))

GENE  group
ENS1  1
ENS2  1
ENS3  1
ENS4  2
ENS1  3
ENS2  3
ENS3  3

Since groups 1 and 3 are identical I would like to remove one of them. How can I do that?

Thank you

like image 510
Rachel Rap Avatar asked Dec 01 '22 13:12

Rachel Rap


1 Answers

A base R option using stack + unstack + duplicated

setNames(
    type.convert(
        stack((u <- unstack(df))[!duplicated(u)]),
        as.is = TRUE
    ), names(df)
)

which gives

  GENE group
1 ENS1     1
2 ENS2     1
3 ENS3     1
4 ENS4     2
like image 57
ThomasIsCoding Avatar answered Dec 04 '22 03:12

ThomasIsCoding