提问人:Joel 提问时间:10/28/2023 最后编辑:gregor-faustoJoel 更新时间:10/28/2023 访问量:33
如何在 R 中创建一个循环,在数据帧中搜索包含某些值的文件,并将它们子集?
How can I create a loop in R that searchers a data frame for files containing certain values, and subsetting them?
问:
假设我有这个数据:
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")
)
乍一看,我在这里看到 4 个行的子集(以 start 开头,以 end 结尾)。我能做些什么来提取这 4 个子集,以便将它们单独作为数据帧?
我尝试创建一个 while 循环,因此如果出现某些值(开始和结束),它会逐行搜索,然后它们之间的所有内容(包括它们)都转换为新的数据框。虽然,我发现自己真的被困住了,因为我无法让它工作。
答:
0赞
gregor-fausto
10/28/2023
#1
如果我理解了这个问题,这里有一个可能的解决方案。
- 每当新序列开始时创建一个指标
- 计算指标变量的累计总和
- 用于根据具有累积总和的变量剪切数据框
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)
评论
0赞
Joel
10/28/2023
是的,我想做的是获取一组文件,使用每个设置列设置的条件(包含 start 的文件 + 包含 end 的文件 = 一个子集。在本例中,将有 4 个行子集
0赞
jkatam
10/28/2023
#2
或者,请尝试以下代码
步骤:
- 派生具有序列号的 var2
- 使用序列号创建向量
- map2 派生 4 个 DFS
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")
) %>% mutate(var1=ifelse(Setting=='start', rleid(Setting),NA)) %>% fill(var1) %>%
mutate(var2=rleid(var1))
vars <- levels(factor(data$var2))
map2('data',vars, \(x,y) {
x1 <- get(x)
x2 <- x1[x1$var2==y,]
assign(paste0('df',y),x2, envir = globalenv())
}
)
[[1]]
Setting Behaviour var1 var2
1 start grooming 1 1
2 continue focal sleep 1 1
3 end fight 1 1
[[2]]
Setting Behaviour var1 var2
4 start grooming 4 2
5 continue focal sleep 4 2
6 end fight 4 2
[[3]]
Setting Behaviour var1 var2
7 start grooming 7 3
8 continue focal sleep 7 3
9 end fight 7 3
[[4]]
Setting Behaviour var1 var2
10 start grooming 10 4
11 continue focal sleep 10 4
12 end fight 10 4
上一个:如何在 R 中执行嵌套子集
评论