R 根据条件从向量中提取一些元素

R 根据条件从向量中提取一些元素,r,R,输入向量是 in <- c("Aone1","BZtwo2","AZtwenty4Btwo5","Cthirty22HIfour9") 中的是否只是试图提取字符串的小写部分?如果是,请尝试: v1 <- c("Aone1", "BZtwo2", "AZtwenty4Btwo5", "Cthirty22HIfour9") regmatches(v1, gregexpr("[a-z]+", v1)) # [[1]] # [1] "one" # # [[2]] # [1] "two"

输入向量是

in <- c("Aone1","BZtwo2","AZtwenty4Btwo5","Cthirty22HIfour9")

中的
是否只是试图提取字符串的小写部分?如果是,请尝试:

v1 <- c("Aone1", "BZtwo2", "AZtwenty4Btwo5", "Cthirty22HIfour9")
regmatches(v1, gregexpr("[a-z]+", v1))
# [[1]]
# [1] "one"
# 
# [[2]]
# [1] "two"
# 
# [[3]]
# [1] "twenty" "two"   
# 
# [[4]]
# [1] "thirty" "four" 

v1谢谢,代码又短又简单。但是,像“CZtwenty48two”这样的元素也将提取不需要的“two”。
v1 <- c("Aone1", "BZtwo2", "AZtwenty4Btwo5", "Cthirty22HIfour9")
regmatches(v1, gregexpr("[a-z]+", v1))
# [[1]]
# [1] "one"
# 
# [[2]]
# [1] "two"
# 
# [[3]]
# [1] "twenty" "two"   
# 
# [[4]]
# [1] "thirty" "four"