What's going on here when there are duplicate list names in R?
l <- list()
l[["A"]] <- 5
l[["B"]] <- 7
l[["C"]] <- 9
names(l) <- c("B", "B", "C")
Typing l[["B"]]
returns
$B
[1] 5
Typing l
returns
$B
[1] 5
$B
[1] 7
$C
[1] 9
Is there a standard way to retrieve all values for the key "B", when "B" is duplicated?
We can find the rows with duplicated values in a particular column of an R data frame by using duplicated function inside the subset function. This will return only the duplicate rows based on the column we choose that means the first unique value will not be in the output.
There are other methods to drop duplicate rows in R one method is duplicated() which identifies and removes duplicate in R. The other method is unique() which identifies the unique values. Get distinct Rows of the dataframe in R using distinct() function.
duplicated() in R The duplicated() is a built-in R function that determines which elements of a vector or data frame are duplicates of elements with smaller subscripts and returns a logical vector indicating which elements (rows) are duplicates.
When you have duplicate names and you call a subset by name, only the first element is returned. In fact, [[
will only ever give you one element anyway, so let's look at [
instead.
l["B"]
# $B
# [1] 5
We can also see that trying c("B", "B")
as the subset won't even give us the right result because R goes back and gets the first B
again.
l[c("B", "B")]
# $B
# [1] 5
#
# $B
# [1] 5
One of the safest ways to retrieve all the B
elements is to use a logical subset of the names()
vector. This will give us the correct elements.
l[names(l) == "B"]
# $B
# [1] 5
#
# $B
# [1] 7
This is a great example of why duplicate names should be avoided.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With