R: Removing Duplicate Elements in a Vector

How to remove repeated elements in a vector, similar to 'set' in Python

You can check out unique function.

 > v = c(1, 1, 5, 5, 2, 2, 6, 6, 1, 3)
> unique(v)
[1] 1 5 2 6 3

Remove duplicates from lists within a vector in R

We can loop over the list with map and apply unique

library(dplyr)
library(purrr)
z %>%
mutate(x = map(x, unique))

In base R, it would be

z$x <- lapply(z$x, unique)

R, Removing duplicate along with the original value

We can use

df1[!(duplicated(df1$col)|duplicated(df1$col, fromLast=TRUE)),, drop=FALSE]
# col
#3. Food

How to remove duplicate vectors from a list?

We can apply negated duplicated also on lists.

list_of_vector[!duplicated(list_of_vector)]
# [[1]]
# [1] 4000519 4000521 4000523 4000525 4000527 4000529
# [7] 4000531 4000533 4000535 4000537 4000539 4000541
# [13] 4000542 4000544 4000545 4000546 4000548 4000550
# [19] 4000552 4000554 4000556 4000558 4000560 4000562
# [25] 4000564 4000566 4000568
#
# [[2]]
# [1] 4000541 4000542 4000544 4000545 4000546 4000548
# [7] 4000550 4000552 4000554 4000556 4000558
#
# [[3]]
# [1] 4000569 4000567 4000565 4000563 4000561 4000559
# [7] 4000557 4000555 4000553 4000551 4000549 4000547
# [13] 4000570 4000571 4000572 4000543 4000540 4000538
# [19] 4000536 4000534 4000532 4000530 4000528 4000526
# [25] 4000524 4000522 4000520
#
# [[4]]
# [1] 4000559 4000557 4000555 4000553 4000551 4000549
# [7] 4000547 4000570 4000571 4000572 4000543
#
# [[5]]
# [1] 4000543 4000540 4000538 4000536 4000534 4000532
# [7] 4000530 4000528 4000526 4000524 4000522 4000520
#
# [[6]]
# [1] 4000559 4000557 4000555 4000553 4000551 4000549
# [7] 4000547 4000570 4000571 4000572 4000543 4000540
# [13] 4000538 4000536 4000534 4000532 4000530 4000528
# [19] 4000526 4000524 4000522 4000520

How to delete duplicated numeric vector elements based on name attribute?

You can use the duplicated function and negation on the names of the vector to remove the elements with duplicate names:

x <- c(A = 1, B = 2, A = 3, C = 4, D = 4)
x[!duplicated(names(x))]

gives:

A B C D 
1 2 4 4

Another option as suggested by @lmo in the comments:

x[unique(names(x))]

which will give the same result.

remove duplicates and it's intrinsec value

Using duplicated in forward and reverse to return two logical vectors, then use OR (|) when either one of them is TRUE, negate (!) and subset the vector

just_a_random_vector[!(duplicated(just_a_random_vector)|
duplicated(just_a_random_vector, fromLast = TRUE))]
[1] "A" "D"

Or another option is table to create a logical vector based on the frequency count i.e. count equal to 1 is returned

just_a_random_vector [just_a_random_vector %in%
names(which(table(just_a_random_vector) == 1))]
[1] "A" "D"

Remove duplicated elements from list

We can unlist the list, get a logical list using duplicated and extract the elements in 'my.list' based on the logical index

un <- unlist(my.list)
res <- Map(`[`, my.list, relist(!duplicated(un), skeleton = my.list))
identical(res, res.list)
#[1] TRUE

R - How to remove duplicate strings from a character vector

you can use the stringr package for this:

 library(stringr)
library(dplyr)

sapply(original, stringr::str_split, " ") %>%
lapply(unique) %>%
lapply(paste, collapse = " ") %>%
unlist() %>%
unname()
# Output
# "hi there" "hello you" "you good"


Related Topics



Leave a reply



Submit