我感兴趣的是四个数字序列:
Clockwise
1->2
2->3
3->4
4->1
Counterclockwise
1->4
4->3
3->2
2->1
Opposite
1->3
3->1
2->4
4->2
Repeat
1->1
2->2
3->3
4->4
我想计算每个Choice
的数据在这些Person
列中出现的每个序列组的次数。
这是我的数据:
df <- structure(list(Time = 1:28, Person = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), Choice = c(1L, 2L, 1L, 2L, 1L, 1L, 3L,
4L, 1L, 4L, 4L, 1L, 1L, 1L, 4L, 4L, 3L, 4L, 1L, 4L, 2L, 3L, 1L,
4L, 4L, 1L, 1L, 4L)), class = "data.frame", row.names = c(NA,
-28L))
在此数据集中,将第1行->第2行视为Clockwise
,因此将人员1的计数加到Clockwise
中。第2行->第3行为Counterclockwise
等
如何在不为每个条件编写if-else的情况下实现这一目标?
预期结果
答案 0 :(得分:2)
您可以使用diff
来获取连续行的差。您的每个类别都对应于diff(Choice) %% 4
的特定情况。 0 =“重复”,1 =“顺时针”,2 =“对面”,3 =“逆时针”。然后,您可以使用cut
将其分类:
编辑:我已经修改了代码以匹配所需的输出
df %>%
group_by(Person) %>%
mutate(Sequence=
c(diff(Choice) %% 4,NA) %>%
cut(breaks=4,labels = c('Repeat','Clockwise','Opposite','Counterclockwise'))
) %>%
filter(!is.na(Sequence)) %>%
group_by(Person,Sequence)%>%
summarise(Count=n())
# # A tibble: 8 x 3
# # Groups: Person [?]
# Person Sequence Count
# <int> <fct> <int>
# 1 1 Repeat 4
# 2 1 Clockwise 5
# 3 1 Opposite 1
# 4 1 Counterclockwise 3
# 5 2 Repeat 3
# 6 2 Clockwise 4
# 7 2 Opposite 2
# 8 2 Counterclockwise 4
答案 1 :(得分:1)
这是我的尝试:还使用?diff
和base::
函数。
ans <- c(NA, diff(df$Choice))
ans[ans %in% c(1,-3)] <- "cw"; ans[ans %in% c(-1,3)] <- "ccw"; ans[ans %in% 0] <- "rep"; ans[ans %in% c(-2,2)] <- "op"
df$result <- ans
tapply(df$result, df$Person, table)
结果:
# Time Person Choice result
#1 1 1 1 <NA>
#2 2 1 2 cw
#3 3 1 1 ccw
#4 4 1 2 cw
#5 5 1 1 ccw
#6 6 1 1 rep
#7 7 1 3 op
#8 8 1 4 cw
#9 9 1 1 cw
#10 10 1 4 ccw
#11 11 1 4 rep
#12 12 1 1 cw
#13 13 1 1 rep
#14 14 1 1 rep
#15 15 2 4 ccw
#16 16 2 4 rep
#17 17 2 3 ccw
#18 18 2 4 cw
#19 19 2 1 cw
#20 20 2 4 ccw
#21 21 2 2 op
#22 22 2 3 cw
#23 23 2 1 op
#24 24 2 4 ccw
#25 25 2 4 rep
#26 26 2 1 cw
#27 27 2 1 rep
#28 28 2 4 ccw
和:
#$`1` <- person 1
#
#ccw cw op rep
# 3 5 1 4
#
#$`2` <- person 2
#
#ccw cw op rep
# 5 4 2 3