从整洁数据中提取参数子集的唯一组合 [英] extract unique combinations of subset of parameters from tidy data

查看:24
本文介绍了从整洁数据中提取参数子集的唯一组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下虚拟数据集

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 ...

给定数据d,我怎样才能取回原始参数?

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

有什么建议吗?

推荐答案

也许您正在寻找 dplyr::distinct()?

    d %>% distinct(a, b)

这篇关于从整洁数据中提取参数子集的唯一组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆