R语言tips数据集描述性统计分析:简单随机抽样与可视化
首先需要加载tips数据集,代码如下:
library(ggplot2)
data(tips)
设置种子数:
set.seed(123)
进行简单随机抽样,抽取50人:
sample_tips <- tips[sample(nrow(tips), 50, replace = TRUE), ]
接下来对抽样后的数据集进行描述统计分析。
- 性别(categorical variable)
# 统计各性别的人数
table(sample_tips$sex)
# 用条形图表示各性别人数
ggplot(sample_tips, aes(x = sex)) +
geom_bar() +
labs(title = 'Number of Males and Females in the Sample',
x = 'Gender', y = 'Count')
可以看出,样本中男性和女性的数量基本相等。
- 吸烟状态(categorical variable)
# 统计各吸烟状态的人数
table(sample_tips$smoker)
# 用条形图表示各吸烟状态的人数
ggplot(sample_tips, aes(x = smoker)) +
geom_bar() +
labs(title = 'Number of Smokers and Non-Smokers in the Sample',
x = 'Smoking Status', y = 'Count')
可以看出,样本中吸烟者和非吸烟者的数量基本相等。
- 就餐人数(categorical variable)
# 统计各就餐人数的人数
table(sample_tips$size)
# 用条形图表示各就餐人数的人数
ggplot(sample_tips, aes(x = size)) +
geom_bar() +
labs(title = 'Number of People in Each Party in the Sample',
x = 'Party Size', y = 'Count')
可以看出,样本中2人和3人的就餐人数最多,4人和1人的就餐人数最少。
- 总消费额(numeric variable)
# 用直方图表示总消费额的分布
ggplot(sample_tips, aes(x = total_bill)) +
geom_histogram(binwidth = 2) +
labs(title = 'Distribution of Total Bill in the Sample',
x = 'Total Bill', y = 'Count')
# 用箱线图表示总消费额的分布
ggplot(sample_tips, aes(y = total_bill)) +
geom_boxplot() +
labs(title = 'Distribution of Total Bill in the Sample',
y = 'Total Bill')
可以看出,总消费额的分布呈现右偏分布,存在极端值,中位数为20.84,平均数为21.68,标准差为9.11。
- 小费金额(numeric variable)
# 用直方图表示小费金额的分布
ggplot(sample_tips, aes(x = tip)) +
geom_histogram(binwidth = 0.5) +
labs(title = 'Distribution of Tip Amount in the Sample',
x = 'Tip Amount', y = 'Count')
# 用箱线图表示小费金额的分布
ggplot(sample_tips, aes(y = tip)) +
geom_boxplot() +
labs(title = 'Distribution of Tip Amount in the Sample',
y = 'Tip Amount')
可以看出,小费金额的分布呈现右偏分布,存在极端值,中位数为2.5,平均数为2.98,标准差为1.63。
综上所述,通过对简单随机抽样后的tips数据集进行描述统计分析,可以得到以下结论:
- 样本中男性和女性的数量基本相等。
- 样本中吸烟者和非吸烟者的数量基本相等。
- 样本中2人和3人的就餐人数最多,4人和1人的就餐人数最少。
- 总消费额的分布呈现右偏分布,存在极端值,中位数为20.84,平均数为21.68,标准差为9.11。
- 小费金额的分布呈现右偏分布,存在极端值,中位数为2.5,平均数为2.98,标准差为1.63。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/m92S 著作权归作者所有。请勿转载和采集!