I'm trying to solve something that's proven surprisingly tricky.
I'm trying to use if_else but apply the condition to the group, not the row.
I thought that something like %in% or any() both of which return a single TRUE/FALSE would work, but... they don't.
Let me give the example:
bob <- tibble("Classification" = c("Probable", "Confirmed", "Confirmed", "Confirmed", "Probable", "Eaten"),
"ID" = c(1,1,2,2,3,3))
betterbob <- bob %>%
group_by(ID) %>%
mutate(Classification.new = if_else("Confirmed" %in% Classification,
"Confirmed",
Classification)) %>%
ungroup()
What I'm trying to accomplish is: if any of the rows in the group have "Confirmed" in the Classification column, then put "Confirmed" in the Classification.new variable.
However, the above gives an error "x false
must be length 1 (length of condition
), not 2."
I'm really not sure why, because the condition returns [1] TRUE or FALSE. (If I run it without the group_by, the error states that the false condition has a length of 6.)
(If I use ifelse() it doesn't generate an error, but simply returns whatever is in Condition of Row 1, not what is in the row being evaluated.)
So question one: what is an elegant way of doing this?
My plan b is:
inelegantbob <- bob %>%
group_by(ID) %>%
mutate(temp.Classification = if_else(Classification == "Confirmed",
1,
0)) %>%
mutate(temp.maxClassification = max(temp.Classification)) %>%
mutate(Classification.new = if_else(temp.maxClassification == 1,
"Confirmed",
Classification)) %>%
ungroup() %>%
select(-temp.Classification, -temp.maxClassification)
But it seems inelegant and there must be a better way (I think).
Question two is just to help me understand these functions better.
Why does this:
errorbob <- bob %>%
mutate(Classification.new = if_else(1 == 1,
"One does equal one",
Classification))
get an error, instead of "One does equal one" in each row of errorbob$Classification.new?
I'd expect this... imagining R's thought process:
row 1: 1 == 1 is true, so I put "One does equal one" in Classification.new.
row 2: 1 == 1 is true, so I put "One does equal one" in Classification.new.
This returns the error "x false
must be length 1 (length of condition
), not 6.".
But 1 == 1 returns:
[1] TRUE
not
[1] TRUE
[2}TRUE
and so on...
This just makes me realize I don't understand this function all that well.
Thanks so much for any help!!!
Luke
(guy who has a lot to learn)