Reputation: 2133
As the tittle reveals I would like to find out the unique values of a named vector with their corresponding names
library(dplyr)
temp <- runif(10000, 0, 10) %>% ceiling() %>% as.integer()
names(temp) <- c('a', 'b', "c", 'd', 'e','f', 'g', "s", "s", 'r') %>% rep(100)
The first 10 values of the named vector look like this.
temp[1:10]
a b c d e f g s s r
7 4 9 2 5 7 9 8 8 8
I could find the unique values or the unique names. But I do not know how to find both of them and know how to match them.
temp %>% unique()
names(temp) %>% unique()
Upvotes: 2
Views: 705
Reputation: 51582
For named vectors you can simply do,
temp[unique(names(temp))]
#or
temp[!duplicated(names(temp))]
#or
temp[match(unique(names(temp)), names(temp))]
Upvotes: 4
Reputation: 17369
I think you'd be better off making the names and the values separate columns in a data frame. You can then use distinct
to get the unique combinations.
library(dplyr)
temp <- c(7, 4, 9, 2, 5, 7, 9, 8, 8, 8)
names(temp) <- c('a', 'b', "c", 'd', 'e','f', 'g', "s", "s", 'r')
data.frame(temp = temp[1:10],
temp_name = names(temp[1:10])) %>%
distinct()
Upvotes: 2