Reputation: 321
I have two vectors,
v <- (TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)
named.v <- (TRUE = "#FF0000FF", FALSE="#00FF00FF", NA="0000FFFF")
I am trying to get the color associated to the values in V.
I have tried doing this
named.v[v]
But all this does is returns
TRUE FALSE <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
"#FF0000FF" "#00FF00FF" "#0000FFFF" NA NA NA NA NA NA NA
I'm really stuck and can't figure it out. Thanks for the help!
Upvotes: 2
Views: 40
Reputation: 887223
We need to match with a character set instead of the logical. Here, the names
of 'named.v' is character
class (though there is inconsistencies in the example created - in general it is the case)
named.v[as.character(v)]
# TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#"#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF"
Now, let's look at the issue in the OP's code, 'v' being logical
, TRUEs implying should select that value, But, the 'named.v' is of length 3, while the logical vector is length 10, so after the first 3 is selected from 'named.v', there is nothing left, so it is missing and NA
represents missing value
v <- c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)
named.v <- c(`TRUE` = "#FF0000FF", `FALSE`="#00FF00FF", `NA`="0000FFFF")
Upvotes: 5