Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does R's ifelse work with character data?

Tags:

r

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?

like image 659
dfrankow Avatar asked May 25 '12 21:05

dfrankow


People also ask

How does Ifelse work in R?

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.

What does Ifelse return in R?

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 .

What is the difference between if and Ifelse in R?

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.

Can Ifelse be nested in R?

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 Answers

+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'
like image 123
Justin Avatar answered Sep 22 '22 06:09

Justin