使用group_by时,是否有一些函数可以保持R dplyr中的唯一值?

使用group_by时,是否有一些函数可以保持R dplyr中的唯一值?,r,dplyr,group-by,tidyverse,R,Dplyr,Group By,Tidyverse,我有一个带有id变量的data.frame(或tiible或任何东西)。我经常用dplyr::group_by对这个id做一些操作,所以 数据%>% 分组依据(id)%>% 总结/变异/…() 通常,对于每个id,我有其他唯一的非数字变量,例如id所属的项目或国家,以及id的其他特征(如性别等)。当我使用上面的summary函数时,这些其他变量将丢失,除非我指定 数据%>% 分组依据(id)%>% 总结(跨越(c(项目、国家、性别等)、独特),…) 或 数据%>% 分组依据(id、项目、国家

我有一个带有
id
变量的
data.frame
(或
tiible
或任何东西)。我经常用
dplyr::group_by
对这个
id
做一些操作,所以

数据%>%
分组依据(id)%>%
总结/变异/…()
通常,对于每个
id
,我有其他唯一的非数字变量,例如
id
所属的
项目或
国家
,以及
id
的其他特征(如性别等)。当我使用上面的
summary
函数时,这些其他变量将丢失,除非我指定

数据%>%
分组依据(id)%>%
总结(跨越(c(项目、国家、性别等)、独特),…)

数据%>%
分组依据(id、项目、国家、性别等)%>%
总结()
是否有一些函数可以检测这些变量,这些变量对于每个id都是唯一的,这样就不必指定它们了

谢谢大家!


PS:我问的主要是
dplyr
groupby
相关函数,但其他环境,如
R-base
数据。表
也不错。

我没有对它进行广泛测试,但它应该可以完成这项工作

library(dplyr)

myData <- tibble(X = c(1, 1, 2, 2, 2, 3),
                 Y = LETTERS[c(1, 1, 2, 2, 2, 3)],
                 R = rnorm(6))
myData
#> # A tibble: 6 x 3
#>       X Y          R
#>   <dbl> <chr>  <dbl>
#> 1     1 A      0.463
#> 2     1 A     -0.965
#> 3     2 B     -0.403
#> 4     2 B     -0.417
#> 5     2 B     -2.28 
#> 6     3 C      0.423

group_by_id_vars <- function(.data, ...) {
  # group by the prespecified ID variables
  .data <- .data %>% group_by(...)
  
  # how many groups do these ID determine
  ID_groups <- .data %>% n_groups()
  
  # Get the number of groups if the initial grouping variables are combined
  # with other variables
  groupVars <- sapply(substitute(list(...))[-1], deparse) #specified grouping Variable
  nms <- names(.data) # all variables in .data
  res <- sapply(nms[!nms %in% groupVars], 
                function(x) {
                  .data %>%
                    # important to specify add = TRUE to combine the variable 
                    # with the IDs
                    group_by(across(all_of(x)), .add = TRUE) %>% 
                    n_groups()})
  
  # which combinations are identical, i.e. this variable does not increase the
  # number of groups in the data if combined with IDvars
  v <- names(res)[which(res == ID_groups)]
  
  # group the data accordingly
  .data <- .data %>% ungroup() %>% group_by(across(all_of(c(groupVars, v))))
  return(.data)
}

myData %>% 
  group_by_id_vars(X) %>% 
  summarise(n = n())
#> `summarise()` regrouping output by 'X' (override with `.groups` argument)
#> # A tibble: 3 x 3
#> # Groups:   X [3]
#>       X Y         n
#>   <dbl> <chr> <int>
#> 1     1 A         2
#> 2     2 B         3
#> 3     3 C         1
库(dplyr)
myData#A tible:6 x 3
#>X Y R
#>      
#>1 A 0.463
#>2 1A-0.965
#>3.2 B-0.403
#>4.2 B-0.417
#>5.2 B-2.28
#>6 3 C 0.423
按id分组变量%
总结(n=n())
#>`summary()`按'X'重新分组输出(用'.groups'参数重写)
#>#tibble:3 x 3
#>#组:X[3]
#>X Y n
#>     
#>1 A 2
#>2 B 3
#>3 C 1

这在应用程序中有点高级,但您需要的是分组变量的线性组合。你可以把这些转换成因子,然后使用一些线性代数

您可以使用
findLinearCombos()
from
caret
来定位这些。不过,要把一切安排得井井有条,还需要一点功夫。我想你是想要这样做的

像这样的事情可能会奏效。我也没有对此进行广泛的测试

套餐

库(dplyr)
图书馆(插入符号)
图书馆(purrr)
功能


group\u by\u lc您是否考虑过
ungroup()
-对数据进行分组,或迭代您想要分组的变量,例如使用
map()
?恐怕答案是否定的,没有自动检测到此类变量。你已经拥有的解决方案是一条路要走。1) 要么在
组中提到它们,要么在
组中使用
组中使用
组中使用
组中的
组中使用
组中的
组中首先使用
组中的
,将它们保存在数据中。是否希望检查答案?@mnist我已经看到了答案。感谢您的支持。您是否希望提供任何形式的feedbakc,如评论/投票/接受?