【发布时间】:2021-09-13 14:16:17
【问题描述】:
我正在尝试通过运行以下命令来创建一个新列“age_years”:
linelist <- linelist %>%
mutate(age_years = case_when(
age_unit == "years" ~ age, # if age is given in years
age_unit == "months" ~ age/12, # if age is given in months
is.na(age_unit) ~ age, # if age unit is missing, assume years
TRUE ~ NA_real_)) # any other circumstance, assign missing
但是,当我这样做时,我会收到以下错误消息:
Error: Problem with `mutate()` column `age_years`.
i `age_years = case_when(...)`.
x non-numeric argument to binary operator
Run `rlang::last_error()` to see where the error occurred.
当我运行 rlang::last_error() 以了解更多信息时,我得到了这个:
<error/dplyr:::mutate_error>
Problem with `mutate()` column `age_years`.
i `age_years = case_when(...)`.
x non-numeric argument to binary operator
Backtrace:
1. `%>%`(...)
9. base::.handleSimpleError(...)
10. dplyr:::h(simpleError(msg, call))
当我运行 rlang::last_trace() 时,我得到了这个:
<error/dplyr:::mutate_error>
Problem with `mutate()` column `age_years`.
i `age_years = case_when(...)`.
x non-numeric argument to binary operator
Backtrace:
x
1. +-`%>%`(...)
2. +-dplyr::mutate(...)
3. +-dplyr:::mutate.data.frame(...)
4. | \-dplyr:::mutate_cols(.data, ..., caller_env = caller_env())
5. | +-base::withCallingHandlers(...)
6. | \-mask$eval_all_mutate(quo)
7. +-dplyr::case_when(...)
8. | \-rlang::eval_tidy(pair$rhs, env = default_env)
9. \-base::.handleSimpleError(...)
10. \-dplyr:::h(simpleError(msg, call))
<error/simpleError>
non-numeric argument to binary operator
感谢您的帮助!
【问题讨论】:
-
您可以添加
dput(linelist)的输出,让我们讨论您的数据吗? -
您的列
age应该是非数字列。顺便说一句,我们无法重现您的错误,因为您必须提供数据样本。 -
你能提供
class(linelist); class(linelist$age); class(linelist$age_unit)的输出吗?