如何使用 dplyr::group_by 为数据帧的每组提供数字? [英] How to give numbers to each group of a dataframe with dplyr::group_by?

查看:15
本文介绍了如何使用 dplyr::group_by 为数据帧的每组提供数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为数据框中的每个组提供数字.例如,我有以下数据框:

I want to give numbers to each group in a dataframe. For example, I have the following dataframe:

df = data.frame( from = c('a', 'a', 'b'), dest = c('b', 'c', 'd') )
#> df
    #from dest
#1    a    b
#2    a    c
#3    b    d

我想按 from 值分组并为每个组指定一个组号.这是预期的结果:

I want to group by from values and give a group number to each group. This is the expected result:

result = data.frame( from = c('a', 'a', 'b'), dest = c('b', 'c', 'd'), group_no = c(1,1,2) )
#> result
    #from dest group_no
#1    a    b        1
#2    a    c        1
#3    b    d        2

我可以使用 for 循环解决这个问题,如下所示:

I can solve this problem using a for loop as follows:

groups = df$from %>% unique
i = 0
df$group_no = NA
for ( g in groups ) {
    i = i + 1
    df[ df$from == g, ]$group_no = i
}
#> df
    #from dest group_no
#1    a    b        1
#2    a    c        1
#3    b    d        2

我想知道是否可以在不使用 for 循环的情况下以更优雅和更实用的方式解决这个问题?具体来说,我想知道这是否可以使用 dplyr::group_by 函数来完成?

I wonder if it is possible to solve this problem in a more elegant and functional way without using for loops? Specifically, I wonder if this can be done using dplyr::group_by function?

推荐答案

使用 mutate 添加一个列,该列只是 from 的数字形式作为因子:

Use mutate to add a column which is just a numeric form of from as a factor:

df %>% mutate(group_no = as.integer(factor(from)))

#   from dest group_no
# 1    a    b        1
# 2    a    c        1
# 3    b    d        2

注意 group_by 在这里不是必需的,除非您将其用于其他目的.如果你想按新列分组以备后用,可以使用group_by代替mutate来添加列.

Note group_by isn't necessary here, unless you're using it for other purposes. If you want to group by the new column for use later, you can use group_by instead of mutate to add the column.

这篇关于如何使用 dplyr::group_by 为数据帧的每组提供数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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