在 R 中生成组内的序列 [英] generate sequence within group in R

查看:59
本文介绍了在 R 中生成组内的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取类别中的序列.

I am trying to obtain a sequence within category.

我的数据是:

A B 
1 1 
1 2
1 2
1 3
1 3
1 3
1 4
1 4

我想得到变量c",比如我的数据:

and I want to get variable "c" such as my data look like:

A B C
1 1 1
1 2 1
1 2 2
1 3 1
1 3 2
1 3 3
1 4 1
1 4 2

推荐答案

Use ave with seq_along:

Use ave with seq_along:

> mydf$C <- with(mydf, ave(A, A, B, FUN = seq_along))
> mydf
  A B C
1 1 1 1
2 1 2 1
3 1 2 2
4 1 3 1
5 1 3 2
6 1 3 3
7 1 4 1
8 1 4 2

如果您的数据已经被排序(就像在这种情况下一样),您还可以使用 sequencerle (mydf$C <- sequence(rle(do.call(paste, mydf))$lengths)),但是 ave 没有这个限制.

If your data are already ordered (as they are in this case), you can also use sequence with rle (mydf$C <- sequence(rle(do.call(paste, mydf))$lengths)), but you don't have that limitation with ave.

如果你是 data.table 的粉丝,你可以使用 .N 如下:

If you're a data.table fan, you can make use of .N as follows:

library(data.table)
DT <- data.table(mydf)
DT[, C := sequence(.N), by = c("A", "B")]
DT
#    A B C
# 1: 1 1 1
# 2: 1 2 1
# 3: 1 2 2
# 4: 1 3 1
# 5: 1 3 2
# 6: 1 3 3
# 7: 1 4 1
# 8: 1 4 2

这篇关于在 R 中生成组内的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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