Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate list names in R

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?

like image 416
Megatron Avatar asked Oct 20 '15 18:10

Megatron


People also ask

How to get duplicated rows in r?

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.

How to deal with duplicates in r?

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.

How does duplicate work in r?

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.


1 Answers

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.

like image 167
Rich Scriven Avatar answered Sep 30 '22 02:09

Rich Scriven