假设我有这些数据:
数据<- data.frame( Setting = c("start", "continue focal", "end", "start", "continue focal", "end", "start", "continue focal", "end", "start", "continue focal", "end"), Behaviour = c("grooming", "sleep", "fight", "grooming", "sleep", "fight", "grooming", "sleep", "fight", "grooming", "sleep", "fight") )
乍一看,我在这里看到 4 个行子集(以 start 开始,以 end 结束)。我可以做什么来提取这 4 个子集,以便将它们单独作为数据框?
我尝试创建一个 while 循环,因此如果出现某些值(开始和结束),它会逐行搜索,然后它们之间的所有内容(包括它们)都将转换为新的数据帧。尽管如此,我发现自己真的陷入困境,因为我无法让它工作。
如果我理解了这个问题,这是一个可能的解决方案。
split
根据累加和的变量来切割数据框data <- data.frame( Setting = c("start", "continue focal", "end", "start", "continue focal", "end", "start", "continue focal", "end", "start", "continue focal", "end"),
Behaviour = c("grooming", "sleep", "fight", "grooming", "sleep", "fight", "grooming", "sleep", "fight", "grooming", "sleep", "fight") )
dataGrouped<-data %>%
dplyr::mutate(newStart = ifelse(Setting=="start",1,0)) %>%
dplyr::mutate(cumulativeSum = cumsum(newStart))
split(dataGrouped,dataGrouped$cumulativeSum)