【问题标题】:R error - Non-numeric Argument to Binary OperatorR错误-二进制运算符的非数字参数
【发布时间】: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)的输出吗?

标签: r dplyr


【解决方案1】:

我已经修复了你的代码。现在它可以完美运行了。

library(tidyverse)

n = 100
linelist = tibble(
  age = sample(10:40, n, replace = TRUE),
  age_unit = sample(c("years", "months", NA, "other"), n, replace = TRUE)
)

linelist <- linelist %>% 
  mutate(age_years = case_when(
    age_unit == "years"  ~ as.double(age),       # if age is given in years
    age_unit == "months" ~ as.double(age)/12,    # if age is given in months
    is.na(age_unit)      ~ as.double(age),       # if age unit is missing, assume years
    TRUE                 ~ NA_real_))
    
linelist

输出

# A tibble: 100 x 3
     age age_unit age_years
   <int> <chr>        <dbl>
 1    19 NA          19    
 2    11 months       0.917
 3    29 NA          29    
 4    32 months       2.67 
 5    29 NA          29    
 6    20 months       1.67 
 7    25 months       2.08 
 8    13 years       13    
 9    10 years       10    
10    24 NA          24    
# ... with 90 more rows

即使agechr 类型,这也有效

linelist = tibble(
  age = sample(paste(10:40), n, replace = TRUE),
  age_unit = sample(c("years", "months", NA, "other"), n, replace = TRUE)
)

输出

# A tibble: 100 x 2
   age   age_unit
   <chr> <chr>   
 1 27    years   
 2 26    months  
 3 23    years   
 4 30    years   
 5 37    months  
 6 17    NA      
 7 16    months  
 8 33    other   
 9 36    other   
10 12    months  

接下来

linelist <- linelist %>% 
  mutate(age_years = case_when(
    age_unit == "years"  ~ as.double(age),       # if age is given in years
    age_unit == "months" ~ as.double(age)/12,    # if age is given in months
    is.na(age_unit)      ~ as.double(age),       # if age unit is missing, assume years
    TRUE                 ~ NA_real_))

linelist

输出

# A tibble: 100 x 3
   age   age_unit age_years
   <chr> <chr>        <dbl>
 1 27    years        27   
 2 26    months        2.17
 3 23    years        23   
 4 30    years        30   
 5 37    months        3.08
 6 17    NA           17   
 7 16    months        1.33
 8 33    other        NA   
 9 36    other        NA   
10 12    months        1   
# ... with 90 more rows

【讨论】:

  • as.double 添加到我的代码中修复了它,谢谢!
  • 是的,你是对的。阅读case_when 函数here 的文档。这里的关键是“所有 RHS 必须评估为相同类型的向量”的信息。
猜你喜欢
  • 2020-09-15
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
相关资源
最近更新 更多