本文介绍了R,如何按行值分组?分裂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有以下格式的矢量的数据框我想将所有行都保留在开始"和停止" val 列中的值(包括它们),并删除不在 start-stop
I have a dataframe with a vector in the follwing formatI want to keep all rows, within the "START" and "STOP" values in the column val (including them), and remove rows that is not inside start-stop
id = str_c("x",1:20)
val = c(rep("NO1", 3), "START", rep("yes1", 3), "STOP",
"NO2", "START", rep("yes2", 3), "STOP", rep("NO3",6))
data = data.frame(id,val)
我的预期输出是
data = data.frame(id = c(str_c("x", 4:8), str_c("x", 10:14)),
val = c("START", rep("yes1", 3), "STOP",
"START", rep("yes2", 3), "STOP"))
推荐答案
您可以获取'START'
和'STOP'
值的索引,并使用mapply
在它们之间创建一个序列,然后从data
中选择那些行.
You can get the index of 'START'
and 'STOP'
values and create a sequence between them with mapply
and select those rows from data
.
ind1 <- which(data$val == 'START')
ind2 <- which(data$val == 'STOP')
data[sort(unique(unlist(mapply(`:`, ind1, ind2)))), ]
# id val
#4 x4 START
#5 x5 yes1
#6 x6 yes1
#7 x7 yes1
#8 x8 STOP
#10 x10 START
#11 x11 yes2
#12 x12 yes2
#13 x13 yes2
#14 x14 STOP
这篇关于R,如何按行值分组?分裂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!