extract unique combinations of subset of parameters from tidy data

跟風遠走 提交于 2019-12-10 23:38:11

问题


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)


来源:https://stackoverflow.com/questions/45555994/extract-unique-combinations-of-subset-of-parameters-from-tidy-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!