r - Remove duplicated elements from list -
i have list of character vectors:
my.list <- list(e1 = c("a","b","c","k"),e2 = c("b","d","e"),e3 = c("t","d","g","a","f")) and i'm looking function character appears more once across list's vectors (in each vector character can appear once), keep first appearance.
the result list example therefore be:
res.list <- list(e1 = c("a","b","c","k"),e2 = c("d","e"),e3 = c("t","g","f")) note possible entire vector in list eliminated number of elements in resulting list doesn't have equal input list.
we can unlist list, logical list using duplicated , extract elements in 'my.list' based on logical index
un <- unlist(my.list) res <- map(`[`, my.list, relist(!duplicated(un), skeleton = my.list)) identical(res, res.list) #[1] true
Comments
Post a Comment