I cannot figure out why an ifelse
assignment does not return the entire object I'm trying to pass.
Below, I look to see if the state of Texas is in a vector of state names (in my actual code, I'm looking up unique state names in a shapefile). Then, if Texas is in this list of state names, assign to a new object (states_abb) the abbreviations for Texas and New Mexico. Otherwise, assign to states_abb the abbreviations for California and Nevada.
I know from this post and ?ifelse
that...
ifelse returns a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE or FALSE.
So in my first example below, I can understand that only CA is returned.
states <- c("California", "Nevada")
(states_abb <- ifelse("Texas" %in% states, c("NM", "TX"), c("CA", "NV")))
# [1] "CA"
But in this second example below, I've pre-defined the object canv_abb. Why doesn't that whole object get passed? Even if it's a character vector, it's its own object, right? Shouldn't that whole "package" get passed?
txnm_abb <- c("NM", "TX")
canv_abb <- c("CA", "NV")
(states_abb <- ifelse("Texas" %in% states, txnm_abb, canv_abb))
# [1] "CA"
I appreciate any insights as to why this is happening. And can someone offer a solution so that I can assign BOTH abbreviations?