Data type issue with case_when funtion

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(): :information_source: 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)
)

Here's how I 'd do it if I needed to be able to do more than just one number as word from "one" to "one hundred" :

library(dplyr)
library(english)

numbers <- as.character(1:100)
names(numbers) <- as.english(1:100)

df <- data.frame(
  x = 1:10, 
  y = c("1", "2" , "thirty-three",  "4", "5", "6", "seventeen", "8", "9", "10")
  )

df |> mutate(
  y = ifelse(
    y %in% names(numbers),
    unname(numbers[y]),
    y
  ) |> as.numeric()
)
1 Like