![](/img/trans.png)
[英]How can I count the number of times an entry is a repeat of the previous entry within a column while grouping by another column in R?
[英]In R, how can I label the first instance of a repeated value run within a column while grouping by another variable?
以下面的数据框为例:
Group<-c("AGroup", "AGroup", "AGroup", "AGroup", "BGroup", "BGroup", "BGroup", "BGroup", "CGroup", "CGroup", "CGroup", "CGroup")
Status<-c("Low", "Low", "High", "High", "High", "Low", "High", "Low", "Low", "Low", "High", "High")
df<-data.frame(Group, Status)
df$FirstHighRun<-c(0,0,1,1,1,0,0,0,0,0,1,1)
这将创建以下内容,其中“FirstHighRun”是我要创建的列:
Group Status FirstHighRun
AGroup Low 0
AGroup Low 0
AGroup High 1
AGroup High 1
BGroup High 1
BGroup Low 0
BGroup High 0
BGroup Low 0
CGroup Low 0
CGroup Low 0
CGroup High 1
CGroup High 1
正如你所看到的,我试图 label 第一次“高”,并直接重复出现此条目,出现在每个组的状态列中。
在“BGroup”中,有两个“High”条目。 但是,由于第二个实例没有直接跟在第一个实例之后,因此它没有标记为 1。
试试rle
library(dplyr)
df %>%
group_by(Group) %>%
mutate(FirstHighRun2 = +(inverse.rle(within.list(rle(Status ==
"High"), { values[which(values)[-1]] <- FALSE})))) %>%
ungroup
-输出
# A tibble: 12 × 4
Group Status FirstHighRun FirstHighRun2
<chr> <chr> <dbl> <int>
1 AGroup Low 0 0
2 AGroup Low 0 0
3 AGroup High 1 1
4 AGroup High 1 1
5 BGroup High 1 1
6 BGroup Low 0 0
7 BGroup High 0 0
8 BGroup Low 0 0
9 CGroup Low 0 0
10 CGroup Low 0 0
11 CGroup High 1 1
12 CGroup High 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.