我有一个data.frame ystr:
v1
1 a
2 B
3 B
4 C
5 d
6 a
7 B
8 D
Run Code Online (Sandbox Code Playgroud)
我想在CAPS中找到每组字母的开头和结尾,所以我的输出将是:
groupId startPos endPos
1 1 2 4
2 2 7 8
Run Code Online (Sandbox Code Playgroud)
通过按顺序查看每个元素并将其与之前的元素进行比较,我能够通过for循环来完成它,如下所示:
currentGroupId <-0
for (i in 1:length(ystr[,1])){
if (grepl("[[:upper:]]", ystr[i,]))
{
if (startCounter == 0)
{
currentGroupId <- currentGroupId +1
startCounter <-1
mygroups[currentGroupId,] <- c(currentGroupId, i, 0)
}
}else if (startCounter == 1){
startCounter <-0
mygroups[currentGroupId,3]<- i-1
}
}
Run Code Online (Sandbox Code Playgroud)
在R中有一种简单的方法吗?
这可能类似于Mark的开始和结束,但我无法弄清楚它在这种情况下是如何应用的.