extract unique combinations of subset of parameter

2019-07-24 19:45发布

问题:

Consider the following dummy data set

library(plyr)

dummy_model <- function(...){
  data.frame(x = rnorm(100), y = rnorm(100))
}

params <- expand.grid(a=1:10, b=letters[1:4])

d <- mdply(params, dummy_model)
str(d)
# 'data.frame': 4000 obs. of  4 variables:
#   $ a: int  1 1 1 1 1 1 1 1 1 1 ...
# $ b: chr  "a" "a" "a" "a" ...
# $ x: num  0.812 1.183 2.839 -0.928 -1.427 ...
# $ y: num  -0.796 0.137 0.976 1.118 0.4 ...

Given the data d, how can I get back the original params?

My current strategy would be to split the data and select the first row, but that doesn't feel very elegant.

library(dplyr)
d %>% group_by(a,b) %>% slice(1) %>% select(-x,-y)
# # A tibble: 40 x 2
# # Groups:   a, b [40]
# a     b
# <int> <chr>
#   1     1     a
# 2     1     b
# 3     1     c
# 4     1     d
# 5     2     a
# 6     2     b
# 7     2     c
# 8     2     d
# 9     3     a
# 10     3     b

Any suggestion?

回答1:

Perhaps you're looking for dplyr::distinct()?

    d %>% distinct(a, b)


标签: r tidyverse