[英]How to recode a continuous variable into Ranges
I need to recode a continuous variable into categories, usually i use the "cut" Function, but in the cut function i need to specify the breaks. 我需要将连续变量重新编码为类别,通常我使用“剪切”函数,但在剪切函数中我需要指定中断。 i am looking for a way to have a different set of breaks depending on other categorical variables in my data frame. 我正在寻找一种方法,根据我的数据框中的其他分类变量,有一组不同的休息时间。
the variable in my example is Cost and the "breaks" are in the second table "cost.range", i have a different set of Breaks for each "Region" and each "Category" 我的例子中的变量是Cost,“break”在第二个表“cost.range”中,我为每个“Region”和每个“Category”设置了一组不同的Breaks
Example : 示例:
Region Product Category Cost
Country A Product 1 CAT A 731
Country B Product 1 CAT A 659
Country C Product 1 CAT A 385
Country D Product 1 CAT A 763
Country A Product 2 CAT A 701
Country B Product 2 CAT A 759
Country C Product 2 CAT A 580
Country D Product 2 CAT A 147
Country A Product 3 CAT B 645
Country B Product 3 CAT B 657
Country C Product 3 CAT B 424
Region Category Cost.Range Range
Country A CAT A 10 R1
Country A CAT A 50 R2
Country A CAT A 200 R3
Country A CAT A 1000 R4
Country A CAT B 20 R1
Country A CAT B 100 R2
Country A CAT B 400 R3
Country A CAT B 1500 R4
code to generate the example : 用于生成示例的代码:
Region <- c("Country A","Country B","Country C","Country D","Country A","Country B","Country C","Country D","Country A","Country B","Country C","Country D","Country A","Country B","Country C","Country D")
Product <- c("Product 1","Product 1","Product 1","Product 1","Product 2","Product 2","Product 2","Product 2","Product 3","Product 3","Product 3","Product 3","Product 4","Product 4","Product 4","Product 4")
Category <- c("CAT A","CAT A","CAT A","CAT A","CAT A","CAT A","CAT A","CAT A","CAT B","CAT B","CAT B","CAT B","CAT B","CAT B","CAT B","CAT B")
Cost <- c(731,659,385,763,701,759,580,147,645,657,424,34,850,463,160,550)
Table1 <- data.frame(Region, Product, Category, Cost)
Region <- c("Country A","Country A","Country A","Country A","Country A","Country A","Country A","Country A")
Category <- c("CAT A","CAT A","CAT A","CAT A","CAT B","CAT B","CAT B","CAT B")
Cost.range <- c(10,50,200,1000,20,100,400,1500)
Range <- c("R1","R1","R3","R4","R1","R2","R3","R4")
Table2 <- data.frame(Region, Category, Cost.range, Range)
This is not the most elegant solution (and I'd be interested to see a better method) but it should achieve the result you're looking for. 这不是最优雅的解决方案(我有兴趣看到更好的方法),但它应该达到您正在寻找的结果。
The select()
and distinct()
functions from the dplyr
package find the possible combinations of Region
and Category
. dplyr
包中的select()
和distinct()
函数可以找到Region
和Category
的可能组合。 These combinations are used to subset the two tables and apply the cut()
function to each subset. 这些组合用于对两个表进行子集化,并将cut()
函数应用于每个子集。
library('dplyr')
library('data.table')
dt1 <- data.table(Table1)
dt2 <- data.table(Table2)
t2d <- Table2 %>% select(Region, Category) %>% distinct
for(i in 1:nrow(t2d)){
dt2_range_subset <- dt2[Region == as.character(t2d$Region[i])
& Category == t2d$Category[i], Cost.range]
dt1[Region == as.character(t2d$Region[i]) & Category == t2d$Category[i],
Cost_factor := cut(as.matrix(Cost), dt2_range_subset)]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.