A code fragment:
blarg = data.frame(a=c("aa", "bb", "dd"))
blarg$b = blarg$a
# blarg$b is now c("aa", "bb", "dd")
blarg$b = ifelse(blarg$a!="bb",blarg$a,"ZZZ")
# blarg$b is now c(1, "ZZZ", 3)
# I expected c("aa", "ZZZ", "dd")
# typeof(blarg$b) is "character"
Why is blarg$b c(1, "ZZZ", 3)? Where do the numbers come from?
In R, the ifelse() function is a shorthand vectorized alternative to the standard if...else statement. Most of the functions in R take a vector as input and return a vectorized output. Similarly, the vector equivalent of the traditional if...else block is the ifelse() function.
Description. 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 .
if statement: specifies a block of statements if the condition is true. elif statement: specifies a new condition to test, if the first condition is false. else statement: specifies a block of statements if the condition is false.
The if-else statements can be nested together to form a group of statements and evaluate expressions based on the conditions one by one, beginning from the outer condition to the inner one by one respectively.
+1 for using a data.frame named blarg.
To expand on what Ben said, factors are internally stored as integers so when you do something like this, R doesn't handle it the way you expect.
Take a look at str(blarg)
in the steps of your code above.
You can either use stringsAsFactors=FALSE
as Ben suggested, or make use of the factor:
ifelse(blarg$a!='bb', levels(blarg$a), 'ZZZ')
Or better yet, if you want to replace the levels of blarg$a
that are 'bb'
, you can eliminate the ifelse
statement altogether:
levels(blarg$a)[levels(blarg$a)=='bb'] <- 'ZZZ'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With