gpt4 book ai didi

r - 根据条件求和相邻行

转载 作者:行者123 更新时间:2023-12-02 05:30:14 26 4
gpt4 key购买 nike

我有一个与此类似的数据框

id <- c(1,1,1,2,2,3,3,3,3,3)
action <- c("for","l","for","f","l","l","for","for","for","f")
time <- c(45,35,24,56,100,121,30,10,35,143)
dframe <- data.frame(id,action,time)

只有“for”操作会在每个唯一 ID 的连续行中重复。我想将这些行折叠成一行,对“for”操作的时间进行求和。我只想在每个唯一的 id 内并且当它们彼此跟随时执行此操作(如 id==3,而不是 id==1)

我尝试了以下代码,但这并不能区分一个接一个的操作,而是对唯一 id 中所有出现的“for”进行求和。

aggregate(action_time ~ id + act, data=mean.event, FUN=sum)

感谢您的宝贵时间。

最佳答案

使用rle()inverse.rle()data.table包:

## Reproduce example data, naming it df and setting stringsAsFactors=FALSE    
id <- c(1,1,1,2,2,3,3,3,3,3)
action <- c("for","l","for","f","l","l","for","for","for","f")
time <- c(45,35,24,56,100,121,30,10,35,143)
df <- data.frame(id,action,time, stringsAsFactors=FALSE)

## Use rle() and inverse.rle() to give each run of "for"s a distinct name
r <- rle(df$action)
r$values <- paste0(r$values, seq_along(r$values))
(r <- inverse.rle(r))
# [1] "for1" "l2" "for3" "f4" "l5" "l5" "for6" "for6" "for6" "f7"

## Use data.table to subset by run of "for"s *and* by id, collapsing only
## sub-data.tables consisting of consecutive "for"s within an id.
library(data.table)
dt <- data.table(df)

dt[ , if(action[1]=="for") {
X <- .SD[1,]
X$time <- sum(time)
X
} else {.SD},
by=list(r, id)][,-1,with=FALSE]
# id action time
# 1: 1 for 45
# 2: 1 l 35
# 3: 1 for 24
# 4: 2 f 56
# 5: 2 l 100
# 6: 3 l 121
# 7: 3 for 75
# 8: 3 f 143

关于r - 根据条件求和相邻行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20084688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com