R 修改Hmisc包中cut2函数的中断

R 修改Hmisc包中cut2函数的中断,r,dplyr,hmisc,R,Dplyr,Hmisc,这是对这一问题的后续行动: 提供的答案使用了Hmisc::cut2,效果非常好!我想修改中断,这样它就不会中断$1,而是中断$0.50 以下是为答案提供的代码: library(Hmisc) library(dplyr) df$cut_Price <- cut2(df$Price, cuts = 4:13) df %>% group_by(cut_Price, Size, Type) %>% summarise_at(c("Opps", "NumberofSale

这是对这一问题的后续行动:

提供的答案使用了
Hmisc::cut2
,效果非常好!我想修改中断,这样它就不会中断$1,而是中断$0.50

以下是为答案提供的代码:

library(Hmisc)
library(dplyr)

df$cut_Price <- cut2(df$Price, cuts = 4:13)

df %>% group_by(cut_Price, Size, Type) %>%
    summarise_at(c("Opps", "NumberofSales", "Revenue"),"sum") %>%
    arrange(Size, cut_Price) %>% ungroup() %>%
    mutate(cut_Price = gsub("(.*, \\d\\.)00", "\\199", cut_Price))

 # A tibble: 16 × 6
       cut_Price   Size    Type    Opps NumberofSales  Revenue
           <chr> <fctr>  <fctr>   <dbl>         <dbl>    <dbl>
1  [ 5.00, 6.99)  LARGE desktop  477870        342455  2037.67
2  [ 6.00, 7.99)  LARGE desktop  842882        523309  3292.29
3  [ 7.00, 8.99)  LARGE desktop  283107        149878  1189.56
4  [10.00,11.00)  LARGE desktop 5506835       1179544 12674.17
5  [11.00,12.00)  LARGE desktop 3542187       1521347 17342.81
6  [ 3.63, 4.99) MEDIUM desktop 6038044       5129937 18617.94
7  [ 5.00, 6.99) MEDIUM desktop 2558997        478423  2548.95
8  [ 7.00, 8.99) MEDIUM desktop 1071631        352294  2483.10
9  [ 9.00,10.00) MEDIUM desktop 2510873        861183  8428.70
10 [10.00,11.00) MEDIUM desktop  441354        215643  2322.70
11 [11.00,12.00) MEDIUM desktop 5144351       1954720 22138.16
12 [ 3.63, 4.99)  SMALL desktop  801038        587541  2145.76
13 [ 4.00, 5.99)  SMALL desktop  939806        303515  1214.60
14 [ 5.00, 6.99)  SMALL desktop 8303927       2143565 11902.14
15 [10.00,11.00)  SMALL desktop  920975        321515  3284.54
16 [11.00,12.00)  SMALL desktop  181471        236643  2811.50
库(Hmisc)
图书馆(dplyr)
df$cut_Price%分组依据(cut_Price,Size,Type)%>%
总结于(c)(“Opp”、“NumberofSales”、“Revenue”)、“sum”)%>%
排列(大小、降价)%%>%ungroup()%%>%
变异(降价=gsub((.*,\\d\\.)00“,“\\199”,降价))
#一个tibble:16×6
降价规模类型Opps数量销售收入
1[5.00,6.99)大型桌面477870 342455 2037.67
2[6.00,7.99)大型桌面8428825233093292.29
3[7.00,8.99)大型桌面2831071498781189.56
4[10.00,11.00)大型桌面5506835 1179544 12674.17
5[11.00,12.00)大型桌面3542187 1521347 17342.81
6[3.63,4.99)中型桌面6038044 5129937 18617.94
7[5.00,6.99)中型桌面2558997 478423 2548.95
8[7.00,8.99)中型桌面1071631 352294 2483.10
9[9.00,10.00)中型桌面2510873 861183 8428.70
10[10.00,11.00)中型桌面441354 215643 2322.70
11[11.00,12.00)中型桌面5144351 1954720 22138.16
12[3.63,4.99)小型桌面801038 587541 2145.76
13[4.00,5.99)小型桌面939806 303515 1214.60
14[5.00,6.99)小型桌面8303927 2143565 11902.14
15[10.00,11.00)小型桌面920975 321515 3284.54
16[11.00,12.00)小型桌面181471 236643 2811.50

任何帮助都会很好,谢谢!

您需要传递所需的中断向量,您可以使用
seq
创建该向量:

library(tidyverse)

df %>% group_by(Size, 
                cut_Price = Hmisc::cut2(Price, cuts = seq(4, 13, .5)), 
                Type) %>% 
    summarise_at(c("Opps", "NumberofSales", "Revenue"), sum)

## Source: local data frame [18 x 6]
## Groups: Size, cut_Price [?]
## 
##      Size     cut_Price    Type    Opps NumberofSales  Revenue
##    <fctr>        <fctr>  <fctr>   <dbl>         <dbl>    <dbl>
## 1   LARGE [ 5.50, 6.00) desktop  477870        342455  2037.67
## 2   LARGE [ 6.00, 6.50) desktop  842882        523309  3292.29
## 3   LARGE [ 7.50, 8.00) desktop  283107        149878  1189.56
## 4   LARGE [10.00,10.50) desktop  928563        209218  2138.41
## 5   LARGE [10.50,11.00) desktop 4578272        970326 10535.76
## 6   LARGE [11.00,11.50) desktop 3542187       1521347 17342.81
## 7  MEDIUM [ 3.63, 4.00) desktop 6038044       5129937 18617.94
## 8  MEDIUM [ 5.00, 5.50) desktop 2558997        478423  2548.95
## 9  MEDIUM [ 7.00, 7.50) desktop 1071631        352294  2483.10
## 10 MEDIUM [ 9.50,10.00) desktop 2510873        861183  8428.70
## 11 MEDIUM [10.50,11.00) desktop  441354        215643  2322.70
## 12 MEDIUM [11.00,11.50) desktop 5144351       1954720 22138.16
## 13  SMALL [ 3.63, 4.00) desktop  801038        587541  2145.76
## 14  SMALL [ 4.00, 4.50) desktop  939806        303515  1214.60
## 15  SMALL [ 5.00, 5.50) desktop  849537        340580  1837.93
## 16  SMALL [ 5.50, 6.00) desktop 7454390       1802985 10064.21
## 17  SMALL [10.00,10.50) desktop  920975        321515  3284.54
## 18  SMALL [11.50,12.00) desktop  181471        236643  2811.50

您需要传递所需的断点向量,可以使用
seq
创建断点向量:

library(tidyverse)

df %>% group_by(Size, 
                cut_Price = Hmisc::cut2(Price, cuts = seq(4, 13, .5)), 
                Type) %>% 
    summarise_at(c("Opps", "NumberofSales", "Revenue"), sum)

## Source: local data frame [18 x 6]
## Groups: Size, cut_Price [?]
## 
##      Size     cut_Price    Type    Opps NumberofSales  Revenue
##    <fctr>        <fctr>  <fctr>   <dbl>         <dbl>    <dbl>
## 1   LARGE [ 5.50, 6.00) desktop  477870        342455  2037.67
## 2   LARGE [ 6.00, 6.50) desktop  842882        523309  3292.29
## 3   LARGE [ 7.50, 8.00) desktop  283107        149878  1189.56
## 4   LARGE [10.00,10.50) desktop  928563        209218  2138.41
## 5   LARGE [10.50,11.00) desktop 4578272        970326 10535.76
## 6   LARGE [11.00,11.50) desktop 3542187       1521347 17342.81
## 7  MEDIUM [ 3.63, 4.00) desktop 6038044       5129937 18617.94
## 8  MEDIUM [ 5.00, 5.50) desktop 2558997        478423  2548.95
## 9  MEDIUM [ 7.00, 7.50) desktop 1071631        352294  2483.10
## 10 MEDIUM [ 9.50,10.00) desktop 2510873        861183  8428.70
## 11 MEDIUM [10.50,11.00) desktop  441354        215643  2322.70
## 12 MEDIUM [11.00,11.50) desktop 5144351       1954720 22138.16
## 13  SMALL [ 3.63, 4.00) desktop  801038        587541  2145.76
## 14  SMALL [ 4.00, 4.50) desktop  939806        303515  1214.60
## 15  SMALL [ 5.00, 5.50) desktop  849537        340580  1837.93
## 16  SMALL [ 5.50, 6.00) desktop 7454390       1802985 10064.21
## 17  SMALL [10.00,10.50) desktop  920975        321515  3284.54
## 18  SMALL [11.50,12.00) desktop  181471        236643  2811.50

cuts
更改为
seq(4,13,5)
。如果重要的话,你也需要清理
gsub
。我试过了,但范围仅在[0,4]和[4,max]之间。这对你有用吗?@alistaireYep。你真正需要的是
df%>%groupby(Size,cut\u Price=Hmisc::cut2(Price,cuts=seq(4,13,5)),Type)%%>%summary_at(c(“Opps”、“NumberofSales”、“Revenue”),sum)
我收到一个错误,上面写着:
mutate_impl(.data,dots)中的错误:SET_STRING_ELT()的值必须是'CHARSXP'而不是'raw'。此外:有50个或更多警告(使用warnings()查看前50个)<>代码>你能把它作为答案吗?这是一个来自C++底层的错误类型,但是我不能复制它。确保你的包是最新的,以防万一已经被修复的bug。把<代码>剪切< <代码>到<代码> SEQ(4, 13,5)
。如果重要的话,你也需要清理
gsub
。我试过了,但范围仅在[0,4]和[4,max]之间。它对你有用吗?@alistaireYep。你真正需要的是
df%>%分组依据(大小,降价=Hmisc::cut2(价格,降价=seq(4,13,5)),键入)%>%在(c(“Opps”,“NumberSales”,“Revenue”)总结,sum)
我收到一个错误,上面写着:
mutate_impl(.data,dots)中的错误:SET_STRING_ELT()的值必须是'CHARSXP'而不是'raw'。此外:有50个或更多警告(使用warnings()查看前50个)<代码> >你能把它作为答案吗?这是一个奇怪的类型错误,来自底层C++,但是我不能复制它。确保你的包是最新的,以防万一已经被修复的bug。有没有办法做到这一点,它包括每一个中断。例如,这不显示从7到7.50个大。你可以使用<代码> TiDyr::C。通过钉住
%%>%ungroup()%%>%complete完成(大小、价格、类型)
。如果您想要一致的标签,您可能还需要在
cut2
中设置
oneval=FALSE
。您可以将其放在答案中,这样我就可以测试它了吗?谢谢您的帮助!有没有办法做到这一点,以使它包括每次中断。例如,这不会显示7到7.50之间的任何内容。您可以使用
tidyr::complete
通过添加
%%>%ungroup()%%>%complete(大小、价格、类型)
。如果您想要一致的标签,您可能还需要在
cut2
中设置
oneval=FALSE
。请您将其放在答案中,以便我可以测试它。谢谢您的帮助!