R,如何按行值分组?分裂? [英] R, how to group by row value? Split?

查看:117
本文介绍了R,如何按行值分组?分裂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下格式的矢量的数据框 我想将所有行都保留在开始"和停止" val 列中的值(包括它们),并删除不在 start-stop

I have a dataframe with a vector in the follwing format I 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,如何按行值分组?分裂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆