基于“开始和停止”的子集行被分配给子集行。弦 [英] Subset rows based on "start and stop" strings

查看:85
本文介绍了基于“开始和停止”的子集行被分配给子集行。弦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望编写一个R脚本,该脚本将在列中搜索特定值并开始子设置行,直到达到特定的文本值。

looking to write an R script that will search a column for a specific value and begin sub setting rows until a specific text value is reached.

示例:

    X1  X2 
[1,] "a" "1"
[2,] "b" "2"
[3,] "c" "3"
[4,] "d" "4"
[5,] "e" "5"
[6,] "f" "6"
[7,] "c" "7"
[8,] "k" "8"

我想做的是搜索 X1 直到字母'c找到',开始对行进行子集化,直到找到另一个字母'c',此时子集过程将停止。使用上面的示例,结果应为包含 c(3,4,5,6,7)的向量。

What I'd like to do is search through X1 until the letter 'c' is found, and begin to subset rows until another letter 'c' is found, at which point the subset procedure would stop. Using the above example, the result should be a vector containing c(3,4,5,6,7).

假设不超过2行,其中 X1 等于'c'

Assume there will be no more than 2 rows where X1 equals 'c'

非常感谢您的帮助。

推荐答案

您可以使用函数查找值所在的位置哪个,然后将其用作索引以获取所需的值。如果您想要从第一个 c到第二个 c的所有内容,则应如下所示:

You can lookup where a value is with the function which, and use that as in index to get the values you are looking for. If you want everything from the first to the second "c", it would look like this:

indices <- which(df$X1=='c')
range <- indices[1]:indices[2]
df$X2[range]

这篇关于基于“开始和停止”的子集行被分配给子集行。弦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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