Trouble using `dplyr::mutate()` with conditional statements in R for complex data transformations
I'm having difficulty using `dplyr::mutate()` to apply conditional transformations to my data frame. I want to create a new column based on existing columns with specific rules. For instance, I want to categorize values in `score` into 'low', 'medium', and 'high' based on their range. Here's what I've tried: ```r library(dplyr) # Sample data frame data <- data.frame(id = 1:5, score = c(15, 45, 75, NA, 20)) # Attempting to create a new column with conditions data <- data %>% mutate( category = case_when( score < 30 ~ 'low', score >= 30 & score < 70 ~ 'medium', score >= 70 ~ 'high', TRUE ~ 'unknown' ) ) ``` However, I'm getting the following warning message when I run this: ``` Warning message: In case_when(...): The result is a list-column, which is not consistent with other columns. ``` I thought `case_when()` would handle `NA` values automatically, but it seems it's causing issues. Additionally, when I check the structure of `data`, the `category` column is of type `list` instead of a `character` vector. I have also tried using `ifelse()` but faced similar issues. Is there a better way to achieve this transformation, or am I missing something in my implementation? I'm using R version 4.1.0 and the latest version of the `dplyr` package. My team is using R for this REST API. Am I missing something obvious?