[英]Extract character elements in a vector in R
In character vectors like a,b,..., e
below, I wonder how to extract the two character elements ie, "bmi"
and "ch"
?在下面a,b,..., e
等字符向量中,我想知道如何提取两个字符元素,即"bmi"
和"ch"
? (ie, desired_output_in_this_case = c("bmi","ch")
) (即, desired_output_in_this_case = c("bmi","ch")
)
The example below is just a toy example, the character elements can be anything else other than ch
and bmi
.下面的示例只是一个玩具示例,字符元素可以是ch
和bmi
以外的任何其他元素。
a <- "bmi + ch | study"
b <- "bmi * ch | study"
c <- "bmi * ch - 1 | study"
d <- "bmi * ch + 0 | study"
e <- "bmi:ch + 0 | study"
This?这个?
a <- "bmi + ch | study"
b <- "bmi * ch | study"
c <- "bmi * ch - 1 | study"
d <- "bmi * ch + 0 | study"
e <- "bmi:ch + 0 | study"
df <- c(a,b,c,d,e)
# desired_output_list <- stringr::str_extract_all(df, "bmi|ch") # all
desired_output_vector <- unlist(stringr::str_extract_all(a, "bmi|ch"))
desired_output_vector
"bmi" "ch"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.