R如果值落在向量范围之间则分配多个类(简单方法)
R Assigning multiple classes if value fall between ranges in a vector (Simple way)
我正在尝试将 class 分配给某个范围内的值。这是一个例子:
library(dplyr)
library(data.table)
library(magrittr)
fieldArea = tibble(TotalFieldArea= runif(100000) * 1000)
fieldArea %<>%
mutate(Segment2019 = if_else(TotalFieldArea < 99, "Very_Small",
if_else(TotalFieldArea < 199, "Small",
if_else(TotalFieldArea < 499, "Medium",
if_else(TotalFieldArea < 999, "Large", "Key_Account")))))
我只是想知道是否有一种方法可以简化 dplyr 中的代码,例如:
# Designing rules
class_rule = c(0, 99, 199, 499, 999)
classes = c("Very_Small", "Small", "Medium", "Large", "Key_Account")
# Applying rules - (Imaginary Code)
fieldArea %<>% mutate(Segment2019 = classes %in% TotalFieldArea < class_rule)
如果 classes 落在范围内,则应分配它们。
我们可以使用cut
library(dplyr)
fieldArea %>%
mutate(Segment2019 = cut(TotalFieldArea, breaks = c(class_rule, Inf),
labels = classes))
我正在尝试将 class 分配给某个范围内的值。这是一个例子:
library(dplyr)
library(data.table)
library(magrittr)
fieldArea = tibble(TotalFieldArea= runif(100000) * 1000)
fieldArea %<>%
mutate(Segment2019 = if_else(TotalFieldArea < 99, "Very_Small",
if_else(TotalFieldArea < 199, "Small",
if_else(TotalFieldArea < 499, "Medium",
if_else(TotalFieldArea < 999, "Large", "Key_Account")))))
我只是想知道是否有一种方法可以简化 dplyr 中的代码,例如:
# Designing rules
class_rule = c(0, 99, 199, 499, 999)
classes = c("Very_Small", "Small", "Medium", "Large", "Key_Account")
# Applying rules - (Imaginary Code)
fieldArea %<>% mutate(Segment2019 = classes %in% TotalFieldArea < class_rule)
如果 classes 落在范围内,则应分配它们。
我们可以使用cut
library(dplyr)
fieldArea %>%
mutate(Segment2019 = cut(TotalFieldArea, breaks = c(class_rule, Inf),
labels = classes))