[英]Error when using mutate and case_when with Error in charToDate(x)
[英]Weird error message when using mutate()/case_when()
我正在嘗試使用 mutate/case_when 在 R 中創建一個新變量,但遇到了一個奇怪的錯誤。 如果我運行此代碼:
library(tidyverse)
df <- data.frame(dist = c(rep(seq(-50,50,1), each = 50)))
temp <- df %>%
mutate(bins = case_when(dist>=-50 & dist<-40 ~ -45,
dist>=-40 & dist<-30 ~ -35,
dist>=-30 & dist<-20 ~ -25,
dist>=-20 & dist<-10 ~ -15,
dist>=-10 & dist<0 ~ -5,
dist>=0 & dist<10 ~ 5,
dist>=10 & dist<20 ~ 15,
dist>=20 & dist<30 ~ 25,
dist>=30 & dist<40 ~ 35,
dist>=40 ~ 45))
我收到此錯誤消息:
Error: Problem with `mutate()` input `bins`.
x could not find function "&<-"
ℹ Input `bins` is `case_when(...)`.
Run `rlang::last_error()` to see where the error occurred.
有誰知道為什么會這樣? 我經常使用 mutate/case_when 並且從未見過這樣的事情。
為什么不簡單地這樣做(僅使用一側條件,case_when 將評估它,優先考慮以前的條件)
temp <- df %>%
mutate(bins = case_when(dist < -40 ~ -45,
dist < -30 ~ -35,
dist < -20 ~ -25,
dist < -10 ~ -15,
dist < 0 ~ -5,
dist < 10 ~ 5,
dist < 20 ~ 15,
dist < 30 ~ 25,
dist < 40 ~ 35,
dist >= 40 ~ 45))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.