Reputation: 879
Suppose I have a named character vector like this:
> class(colors)
[1] "character"
> colors
9074 8778 8577 7148 <NA> 3310 0050169 8893 50156 9008 9778
"#FF0000FF" "#FF7600FF" "#FFEB00FF" "#9DFF00FF" "#27FF00FF" "#00FF4EFF" "#00FFC4FF" "#00C4FFFF" "#004EFFFF" "#2700FFFF" "#9D00FFFF"
5295 0080162
"#FF00EBFF" "#FF0076FF"
where the names are IDs, and the values are colors. If I give an ID to the colors
vector, I get the corresponding color.
> colors["9074"]
9074
"#FF0000FF"
However for the one case where the name is <NA>
, I do not know how I can return the corresponding color. Simply providing NA
does not work
> colors[NA]
<NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
NA NA NA NA NA NA NA NA NA NA NA NA NA
> colors["NA"]
<NA>
NA
Any suggestions?
Upvotes: 4
Views: 1470
Reputation: 76402
Since colors
already is the name of a base R
function, I will create a vector x
.
x <- 1:5
names(x) <- c("A", "B", NA, "D", "E")
x[is.na(names(x))]
#<NA>
# 3
Upvotes: 1
Reputation: 645
This should work, but would return multiple values if multiple colors don't have names.
colors[which(is.na(names(colors)))]
Upvotes: 0