简体   繁体   English

在R中使用ifelse提取字符串中模式的位置

[英]Extracting position of pattern in a string using ifelse in R

I have a set of strings x for example: 我有一组字符串x例如:

[1] "0000000000000000000000000000000000000Y"   "9000000000D00000000000000000000Y"        
[3] "0000000000000D00000000000000000000X"      "000000000000000000D00000000000000000000Y"
[5] "000000000000000000D00000000000000000000Y" "000000000000000000D00000000000000000000Y"
[6]"000000000000000000000000D0000000011011D1X"

I want to extract the last position of a particular character like 1. I am running this code: 我想提取特定字符的最后位置,例如1。我正在运行以下代码:

ifelse(grepl("1",x),rev(gregexpr("1",x)[[1]])[1],50)

But this is returning -1 for all elements. 但这对于所有元素都返回-1。 How do I correct this? 我该如何纠正?

在此处输入图片说明

In base R, the following returns the position of the last matched "1" . 在基数R中,以下内容返回最后匹配的"1"

# Make some toy data
toydata <- c("001", "007", "00101111Y", "000AAAYY")

# Find last postion
last_pos <- sapply(gregexpr("1", toydata), function(m) m[length(m)])
print(last_pos)
#[1]  3 -1  8 -1

It returns -1 whenever the pattern is not matched. 只要模式不匹配,它就会返回-1

We can use stri_locate_last from stringi . 我们可以使用stri_locate_laststringi If there are no matches, it will return NA . 如果没有匹配项,它将返回NA

library(stringi)
r1 <- stri_locate_last(v1, fixed=1)[,1]
r1
#[1] NA NA NA NA NA NA 40
nchar(v1)
#[1] 38 32 35 40 40 40 41

If we need to replace the NA values with number of characters 如果我们需要用字符数替换NA值

ifelse(is.na(r1), nchar(v1), r1)

data 数据

v1 <-  c("0000000000000000000000000000000000000Y", 
      "9000000000D00000000000000000000Y", 
      "0000000000000D00000000000000000000X",
      "000000000000000000D00000000000000000000Y",
      "000000000000000000D00000000000000000000Y", 
      "000000000000000000D00000000000000000000Y", 
      "000000000000000000000000D0000000011011D1X")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM