我正在处理日志数据;试图找到每个事件的整数。一轮的开始由 action=="start" 发出信号。我想创建一个"action.round"列,告诉我每个事件对应于哪个回合。
我有这样的数据:
data <- read_table2("Id action
A start
A na
A start
A na
A na
A na
A na
A start
B start
B na
B start
B na
B start
B na"
我正在尝试创建如下输出:
output <- read_table2("Id action action.round
A start 1
A na 1
A start 2
A na 2
A na 2
A na 2
A na 2
A start 3
B start 1
B na 1
B start 2
B na 2
B start 3
B na 3")
到目前为止,我已经能够使用 row_number(( 获取部分输出,如下所示:
` data %>%
mutate(round.start=case_when(actionValue=="start"~"start",TRUE~"NA")) %>%
ungroup() %>%
group_by(Id,round.start) %>%
mutate(action.round=row_number())`
但是现在,我想将对应于round.start=="start"的整数填充到列中,以便我知道每列实际上对应于哪个整数(请参阅上面的所需输出(。
您可以在按Id
分组后使用cumsum
。
library(dplyr)
data %>% group_by(Id) %>% mutate(action.round = cumsum(action == "start"))
# Id action action.round
# <chr> <chr> <int>
# 1 A start 1
# 2 A na 1
# 3 A start 2
# 4 A na 2
# 5 A na 2
# 6 A na 2
# 7 A na 2
# 8 A start 3
# 9 B start 1
#10 B na 1
#11 B start 2
#12 B na 2
#13 B start 3
#14 B na 3
这可以在基础 R 中完成
data$action.round <- with(data, ave(action == "start", Id, FUN = cumsum))
以及data.table
library(data.table)
setDT(data)[, action.round := cumsum(action == "start"), Id]
数据
data <- structure(list(Id = c("A", "A", "A", "A", "A", "A", "A", "A",
"B", "B", "B", "B", "B", "B"), action = c("start", "na", "start",
"na", "na", "na", "na", "start", "start", "na", "start", "na",
"start", "na")), row.names = c(NA, -14L), spec = structure(list(
cols = list(Id = structure(list(), class = c("collector_character",
"collector")), action = structure(list(), class = c("collector_character",
"collector")), action.round = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))