Reputation: 486
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.
Upvotes: 2
Views: 455
Reputation: 20085
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
Upvotes: 1
Reputation: 12684
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"
Upvotes: 0
Reputation: 12559
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")
Upvotes: 3