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
How to Color Bar Plots When Using ..Prop.. in Ggplot
R - Identify Consecutive Sequences
Bar Plot for Count Data by Group in R
Separate a Column into Multiple Columns Using Tidyr::Separate with Sep=""
Click on Cross Domain Iframe Element Using Rselenium
Geom_Bar + Geom_Line: with Different Y-Axis Scale
Adding Manual Legend in Ggplot
Dist Function with Large Number of Points
Ggplot2: How to Rotate a Graph in a Specific Angle
Create a New Column with Non-Null Columns' Names
How to Check If Multiple Strings Exist in Another String
Counting the Number of Values Greater Than 0 in R in Multiple Columns
Writing a Function to Calculate the Mean of Columns in a Dataframe in R
Calculate Difference Between Dates by Group in R
Display Different Time Elements at Different Speeds in Gganimate
Generate Id for Each Group with Repeated and Missing Observations