在r中的dplyr :: group_by之后将组变量转换为组名

时间:2017-08-10 18:46:27

标签: r dplyr

我想将数据拆分为单独的组并查看它。

mtcars %>% group_by(cyl) %>% select(mpg, hp)

输出:

# A tibble: 32 x 3
# Groups:   cyl [3]
     cyl   mpg    hp
 * <dbl> <dbl> <dbl>
 1     6  21.0   110
 2     6  21.0   110
 3     4  22.8    93
 4     6  21.4   110
 5     8  18.7   175
 6     6  18.1   105
 7     8  14.3   245
 8     4  24.4    62
 9     4  22.8    95
10     6  19.2   123
# ... with 22 more rows

分组变量仍然是数据框中的一列。有没有办法让组变量成为一种组名(每组一个条目),使用dplyr? 像下面的东西。以这种方式可视化每个小组要容易得多。

      4  21.0   110
         22.8    93
         21.4   110
      6  18.7   175
         18.1   105
      8  14.3   245
      ........

3 个答案:

答案 0 :(得分:1)

我们可以使用Vue.component('contact-page', { ... Contact page specific code here... }); 更改值

replace

答案 1 :(得分:1)

  

有没有办法让组变量成为一种组名(每组一个条目),使用dplyr? [...]通过这种方式可视化每个小组要容易得多。

您可以创建一个自定义功能进行打印,并使用magrittr的%T>%进行调用以进行浏览。

这是一个使用data.table / dtplyr的例子,因为它有一个很好的keep.by参数:

library(magrittr)
library(data.table)
library(dtplyr)
library(dplyr)

print_by0 = function(x){
    gvars = as.character(groups(x))
    if (length(gvars)) x %>% split(by = gvars, keep.by = FALSE) %>% print
    else print(x)
} 

# or ... better?
print_by = function(x){
    gvars = as.character(groups(x))
    ovars = setdiff(names(x), gvars)
    y = copy(x)[, .g := replace(rep("", .N), 1L, paste(.BY, collapse = "; ")), keyby=gvars]
    y[, (gvars) := NULL ]
    setcolorder(y, c(".g", ovars))
    setnames(y, ".g", sprintf("GRP: {%s}", paste(gvars, collapse = "; ")))
    print(data.table(y), nrow=Inf)
} 

用法。请注意res仍然具有正确的结构,因为%T>%使其保持不变。

> DT = data.table(mtcars)
> res = DT %>% group_by(am, cyl) %>% select(mpg, hp) %T>% print_by
    GRP: {am; cyl}  mpg  hp
 1:           0; 4 24.4  62
 2:                22.8  95
 3:                21.5  97
 4:           0; 6 21.4 110
 5:                18.1 105
 6:                19.2 123
 7:                17.8 123
 8:           0; 8 18.7 175
 9:                14.3 245
10:                16.4 180
11:                17.3 180
12:                15.2 180
13:                10.4 205
14:                10.4 215
15:                14.7 230
16:                15.5 150
17:                15.2 150
18:                13.3 245
19:                19.2 175
20:           1; 4 22.8  93
21:                32.4  66
22:                30.4  52
23:                33.9  65
24:                27.3  66
25:                26.0  91
26:                30.4 113
27:                21.4 109
28:           1; 6 21.0 110
29:                21.0 110
30:                19.7 175
31:           1; 8 15.8 264
32:                15.0 335
    GRP: {am; cyl}  mpg  hp
> res
Source: local data table [32 x 4]
Groups: am, cyl

# A tibble: 32 x 4
      am   cyl   mpg    hp
   <dbl> <dbl> <dbl> <dbl>
 1     1     6  21.0   110
 2     1     6  21.0   110
 3     1     4  22.8    93
 4     0     6  21.4   110
 5     0     8  18.7   175
 6     0     6  18.1   105
 7     0     8  14.3   245
 8     0     4  24.4    62
 9     0     4  22.8    95
10     0     6  19.2   123
# ... with 22 more rows

dplyr guru可以将print_by翻译成与sibbles一起工作,但是......我会将其作为练习。

答案 2 :(得分:0)

ls = ['a','b','c','d'] # letters
fns = [] # functions

for l in ls:
    fns.append(eval(f"lambda:print('{l}')")) # The eval command allows me to replace l with l on creation

然后您可以根据需要用“”替换NA。

相关问题