I'm trying to eliminate the unwanted datavalues in my dataset. For example, there is a "five" where there should only be numeric variables and thats what I'm trying to solve currently. From what I've learned I should be able to do it with the code below but I cannot get it to work.
sorghum_clean <- sorghum |>
mutate (
k = case_when(
k == "five" ~ k,
k ~ TRUE)
)
I get the error as follows:
Error in mutate(): In argument: k = case_when(is.na(k) ~ k, k ~ TRUE). Caused by error in case_when(): ! ..2 (left) must be a logical vector, not a double vector. Backtrace: 1. dplyr::mutate(...) 8. dplyr::case_when(is.na(k) ~ k, k ~ TRUE, .default = NULL, .ptype = NULL, .size = NULL)
[image] Show Traceback
Error in mutate(sorghum, k = as.numeric(k), k = case_when(is.na(k) ~ k, : Caused by error in case_when(): ! ..2 (left) must be a logical vector, not a double vector.
In a case_when, the logical test goes on the left side of the ~ symbol. You have it on the right when you write k ~ TRUE. That should be TRUE ~ k.
Also, you will be setting k to the value of k in all cases, so the case_when() will not change any values. Do you mean
mutate (
k = case_when(
k == "five" ~ 5,
TRUE ~ k)
)