简体   繁体   中英

How to create a vector based on a pattern match using R?

In R, I have a vector containing characters:

v <- c("X412A-Y423A", "X400A-Y405B", "X499A-Y448B", "X455A-Y213A")

I want to create a vector based on this one, depending on the last character in each string (A or B), the new vector will have a different value (red or blue) such as:

vnew <- c("red","blue","blue","red")

Any help would be appreciated.

With base R you can do:

v <- c("X412A-Y423A", "X400A-Y405B", "X499A-Y448B", "X455A-Y213A")
n <- nchar(v)
ifelse(substr(v, n, n)=="A", "red", "blue")

or you can use regular expressions (also base R ):

ifelse(grepl("A$", v), "red", "blue")

An elegant solution can be achieved using case_when from dplyr package and sub from base-R . The solution more suited for data.frame like scenario.

#data
v <- c("X412A-Y423A", "X400A-Y405B", "X499A-Y448B", "X455A-Y213A")

library(dplyr)
data.frame(v, stringsAsFactors = FALSE) %>% 
  mutate(value = sub('.*(?=.$)', '', v, perl=T)) %>%
  mutate(color = case_when(
    .$value == 'A' ~ "Red",
    .$value == 'B' ~ "Blue",
    TRUE ~ "Green"
  ))

#             v  value color
# 1 X412A-Y423A     A   Red
# 2 X400A-Y405B     B  Blue
# 3 X499A-Y448B     B  Blue
# 4 X455A-Y213A     A   Red

For just creating a vector with color solution could be:

last_char = sub('.*(?=.$)', '', v, perl=T)

case_when(
  last_char == 'A' ~ "Red",
  last_char == 'B' ~ "Blue",
  TRUE ~ "Green"
)
[1] "Red"  "Blue" "Blue" "Red

This will work:

> v <- c("X412A-Y423A", "X400A-Y405B", "X499A-Y448B", "X455A-Y213A")
> x <- substr(v,nchar(v),nchar(v))
> vnew <- ifelse(x == "A", "red", ifelse(x == "B", "blue", ""))
> vnew
[1] "red"  "blue" "blue" "red" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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