某购物平台根据用户在平台上的的月均消费额度对用户进行分类,分类标准为:月均消费少于500元的为初级客户,大于等于500元而小于1000元的为中级客户,大于等于1000元的为高级客户。请编写R语言按上述标准为至少20名客户完成分类,并进行分组输出
时间: 2024-05-12 09:18:42 浏览: 16
假设我们有以下20名客户的月均消费额度数据:
```R
customer <- data.frame(
id = 1:20,
monthly_spending = c(300, 800, 1200, 450, 900, 600, 1500, 2000, 350, 550,
800, 400, 1000, 1200, 150, 650, 900, 700, 950, 1100)
)
```
我们可以使用 `ifelse()` 函数进行分类:
```R
customer$level <- ifelse(customer$monthly_spending < 500, "初级客户",
ifelse(customer$monthly_spending < 1000, "中级客户", "高级客户"))
```
输出分类结果:
```R
customer
```
```
id monthly_spending level
1 1 300 初级客户
2 2 800 中级客户
3 3 1200 高级客户
4 4 450 初级客户
5 5 900 中级客户
6 6 600 中级客户
7 7 1500 高级客户
8 8 2000 高级客户
9 9 350 初级客户
10 10 550 初级客户
11 11 800 中级客户
12 12 400 初级客户
13 13 1000 中级客户
14 14 1200 高级客户
15 15 150 初级客户
16 16 650 中级客户
17 17 900 中级客户
18 18 700 中级客户
19 19 950 中级客户
20 20 1100 高级客户
```
我们也可以使用 `group_by()` 和 `summarize()` 函数进行分组输出:
```R
library(dplyr)
customer %>%
group_by(level) %>%
summarize(count = n(),
total_spending = sum(monthly_spending),
avg_spending = mean(monthly_spending))
```
```
# A tibble: 3 x 4
level count total_spending avg_spending
<chr> <int> <dbl> <dbl>
1 初级客户 6 2650 442.
2 中级客户 10 7950 795.
3 高级客户 4 5000 1250.
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)