如果一行与前一行不同,则制作一个计数器

Making a counter if a row is different than the previous one

我知道你可以在 R 中使用

tail()

head()

当特定值与前一个值不同时进行计数,但我可以对多列执行此操作吗?如果当前行(仅最后 4 列)与前一行不同,我需要创建一个计数为 1 的列。

这是我的数据示例:

>    Standing Stepping Cycling New_Sitting
1         0        0       0           1
2         0        0       0           1
3         0        0       0           1
4         0        0       0           1
5         0        0       0           1
6         0        0       0           1
7         0        0       0           1
8         0        0       0           1
9         0        0       0           1
10        0        0       0           1
11        0        0       0           1
12        0        0       0           1
13        0        0       0           1
14        0        0       0           1
15        0        0       0           1
16        0        0       0           1
17        0        0       0           1
18        0        0       0           1
19        0        0       0           1
20        0        0       0           1
21        0        0       0           1
22        0        0       0           1
23        0        0       0           1
24        0        0       0           1
25        0        0       0           1
26        0        0       0           1
27        0        0       0           1
28        0        0       0           1
29        0        0       0           1
30        0        0       0           1
31        0        0       0           1
32        0        0       0           1
33        0        0       0           1
34        0        0       0           1
35        0        0       0           1
36        0        0       0           1
37        0        0       0           1
38        0        0       0           1
39        0        0       0           1
40        0        0       0           1
41        0        0       0           1
42        0        0       0           1
43        0        0       0           1
44        0        0       0           1
45        0        0       0           1
46        0        0       0           1
47        0        0       0           1
48        0        0       0           1
49        0        0       1           0
50        0        1       0           0

所以基本上,最后 4 列代表人的姿势,我需要有一个计数列(命名为 counter,或任何其他逻辑)当他们改变位置时为 1,当他们不改变时为 0 .如果您需要更多信息,请告诉我。

在这种情况下,答案应该是:

count = 2

如果我们要检查所选列中的any行是否满足条件,请使用if_any或者如果我们希望all列满足,则为if_all - 遍历使用 last_col() 选择的列,然后检查当前值是否不等于先前的值 (lag),并使用 as.integer 或将逻辑值转换为二进制值+

library(dplyr)
df1 <- df1 %>%
    mutate(Counter = +(if_any((last_col()-3):last_col(), 
        ~ .x != lag(.x, default= first(.x)))))

数据

df1 <- structure(list(Date = 1:50, Time = c("2022-03-08 2022-03-08 15:46:00", 
"2022-03-08 2022-03-08 15:46:01", "2022-03-08 2022-03-08 15:46:02", 
"2022-03-08 2022-03-08 15:46:03", "2022-03-08 2022-03-08 15:46:04", 
"2022-03-08 2022-03-08 15:46:05", "2022-03-08 2022-03-08 15:46:06", 
"2022-03-08 2022-03-08 15:46:07", "2022-03-08 2022-03-08 15:46:08", 
"2022-03-08 2022-03-08 15:46:09", "2022-03-08 2022-03-08 15:46:10", 
"2022-03-08 2022-03-08 15:46:11", "2022-03-08 2022-03-08 15:46:12", 
"2022-03-08 2022-03-08 15:46:13", "2022-03-08 2022-03-08 15:46:14", 
"2022-03-08 2022-03-08 15:46:15", "2022-03-08 2022-03-08 15:46:16", 
"2022-03-08 2022-03-08 15:46:17", "2022-03-08 2022-03-08 15:46:18", 
"2022-03-08 2022-03-08 15:46:19", "2022-03-08 2022-03-08 15:46:20", 
"2022-03-08 2022-03-08 15:46:21", "2022-03-08 2022-03-08 15:46:22", 
"2022-03-08 2022-03-08 15:46:23", "2022-03-08 2022-03-08 15:46:24", 
"2022-03-08 2022-03-08 15:46:25", "2022-03-08 2022-03-08 15:46:26", 
"2022-03-08 2022-03-08 15:46:27", "2022-03-08 2022-03-08 15:46:28", 
"2022-03-08 2022-03-08 15:46:29", "2022-03-08 2022-03-08 15:46:30", 
"2022-03-08 2022-03-08 15:46:31", "2022-03-08 2022-03-08 15:46:32", 
"2022-03-08 2022-03-08 15:46:33", "2022-03-08 2022-03-08 15:46:34", 
"2022-03-08 2022-03-08 15:46:35", "2022-03-08 2022-03-08 15:46:36", 
"2022-03-08 2022-03-08 15:46:37", "2022-03-08 2022-03-08 15:46:38", 
"2022-03-08 2022-03-08 15:46:39", "2022-03-08 2022-03-08 15:46:40", 
"2022-03-08 2022-03-08 15:46:41", "2022-03-08 2022-03-08 15:46:42", 
"2022-03-08 2022-03-08 15:46:43", "2022-03-08 2022-03-08 15:46:44", 
"2022-03-08 2022-03-08 15:46:45", "2022-03-08 2022-03-08 15:46:46", 
"2022-03-08 2022-03-08 15:46:47", "2022-03-08 2022-03-08 15:46:48", 
"2022-03-08 2022-03-08 15:46:49"), Axis1 = c(0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 105L, 34L, 98L, 81L, 
69L, 44L, 36L, 70L, 75L, 33L, 34L, 16L, 0L, 6L, 10L, 0L, 0L, 
84L, 53L), Axis2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 65L, 42L, 15L, 15L, 108L, 87L, 89L, 90L, 
119L, 92L, 21L, 11L, 0L, 17L, 59L, 40L, 28L, 30L, 46L), Axis3 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 14L, 
1L, 54L, 19L, 93L, 98L, 209L, 40L, 48L, 14L, 19L, 40L, 22L, 14L, 
36L, 47L, 8L, 116L, 17L), VM = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
124.28, 54.05, 112.89, 84.54, 158.35, 138.24, 230, 120.83, 148.63, 
98.74, 44.25, 44.46, 22, 22.83, 69.84, 61.72, 29.12, 146.33, 
72.21), Standing = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L), Stepping = c(0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Cycling = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 
1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 
1L), New_Sitting = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
-50L))