1.数据处理

df<-function(file_path){
  dir_way <- read.csv(file_path, header=F)
  table = dir_way[!apply(dir_way == "", 1, all),]
  table_1<-table[-1:-3,][,-1]
  table_1$V2[table_1$V3==1]<-"女性"
  table_1$V2[table_1$V2==1]<-"男性"
  table_1$V5[table_1$V4==1]<-"0-18岁"
  table_1$V5[table_1$V5==1]<-"18-45岁"
  table_1$V5[table_1$V6==1]<-"45+岁"
  table_1$V8[table_1$V7==1]<-"0"
  table_1$V8[table_1$V8==1]<-"1"
  table_1$V8[table_1$V9==1]<-"2"
  table_1$V8[table_1$V10==1]<-"3"
  table_1$V8[table_1$V11==1]<-"4"
  dw<-table_1[c(1,4,7)]
  dw[dw=='']<-NA
  dw=na.omit(dw)
  names(dw)[1]<-"性别"
  names(dw)[2]<-"年龄"
  names(dw)[3]<-"背包等级"
  return (dw)
}
library(readr)
ws <- df("~/Desktop/调查表.csv")
se <-head(df("~/Desktop/南站_进站.csv"),-1)
ss <-head(df("~/Desktop/南站_地铁.csv"),-1)
head(ws)
##   性别    年龄 背包等级
## 4 女性   45+岁        1
## 5 女性   45+岁        3
## 6 女性 18-45岁        1
## 7 女性 18-45岁        1
## 8 男性 18-45岁        2
## 9 男性 18-45岁        1
head(se)
##   性别    年龄 背包等级
## 4 女性 18-45岁        1
## 5 男性 18-45岁        3
## 6 男性 18-45岁        1
## 7 女性 18-45岁        3
## 8 女性 18-45岁        3
## 9 男性 18-45岁        0
head(ss)
##   性别    年龄 背包等级
## 4 女性 18-45岁        4
## 5 女性 18-45岁        3
## 6 男性 18-45岁        3
## 7 女性 18-45岁        1
## 8 男性 18-45岁        3
## 9 女性   45+岁        1

备注:背包等级
level1:无行李
level2:随身小包
level3:大挎包或双肩背包
level4:拉杆箱
level5:多于2件行李

2.数值统计

2.1单变量数值统计

西站地铁站单变量统计

attach(ws)
table_ws<-table(ws)
table_se<-table(se)
table_ss<-table(ss)
#df_ws<-data.frame(ws)

ws_values_1<-data.frame(margin.table(table_ws,1))
ws_values_1
##   性别 Freq
## 1 女性  116
## 2 男性  177
ws_values_2<-data.frame(margin.table(table_ws,2))
ws_values_2
##      年龄 Freq
## 1  0-18岁   31
## 2 18-45岁  217
## 3   45+岁   45
ws_values_3<-data.frame(margin.table(table_ws,3))
ws_values_3 
##   背包等级 Freq
## 1        0   74
## 2        1   88
## 3        2   61
## 4        3   57
## 5        4   13

南站进站口单变量统计

se_values_1<-data.frame(margin.table(table_se,1))
se_values_1
##   性别 Freq
## 1 女性 1025
## 2 男性  908
se_values_2<-data.frame(margin.table(table_se,2))
se_values_2
##      年龄 Freq
## 1 18-45岁 1856
## 2   45+岁   77
se_values_3<-data.frame(margin.table(table_se,3))
se_values_3 
##   背包等级 Freq
## 1        0  147
## 2        1  133
## 3        2  613
## 4        3  892
## 5        4  148

南站地铁站单变量统计

ss_values_1<-data.frame(margin.table(table_ss,1))
ss_values_1
##   性别 Freq
## 1 女性  142
## 2 男性  135
ss_values_2<-data.frame(margin.table(table_ss,2))
ss_values_2
##      年龄 Freq
## 1  0-18岁   15
## 2 18-45岁  245
## 3   45+岁   17
ss_values_3<-data.frame(margin.table(table_ss,3))
ss_values_3 
##   背包等级 Freq
## 1        0   27
## 2        1   54
## 3        2   74
## 4        3  103
## 5        4   19

性别统计

value_1<-ws_values_1+se_values_1+ss_values_1
value_1['性别']=c('女性','男性')
value_1
##   性别 Freq
## 1 女性 1283
## 2 男性 1220

背包等级统计

value_3<-ws_values_3+se_values_3+ss_values_3
value_3['背包等级']=c(0,1,2,3,4)
value_3
##   背包等级 Freq
## 1        0  248
## 2        1  275
## 3        2  748
## 4        3 1052
## 5        4  180

2.2 交叉变量统计

西站地铁站交叉变量统计

new_ws_1<-xtabs(~性别+背包等级, data = ws)
ftable(new_ws_1)
##      背包等级  0  1  2  3  4
## 性别                        
## 女性          21 51 16 20  8
## 男性          53 37 45 37  5
new_ws_2<-xtabs(~年龄+背包等级, data = ws)
ftable(new_ws_2)
##         背包等级  0  1  2  3  4
## 年龄                           
## 0-18岁           10  6  9  6  0
## 18-45岁          59 66 42 43  7
## 45+岁             5 16 10  8  6
new_ws_3<-xtabs(~性别+年龄+背包等级, data = ws)
ftable(new_ws_3)
##              背包等级  0  1  2  3  4
## 性别 年龄                           
## 女性 0-18岁            3  5  4  2  0
##      18-45岁          16 34  9 14  5
##      45+岁             2 12  3  4  3
## 男性 0-18岁            7  1  5  4  0
##      18-45岁          43 32 33 29  2
##      45+岁             3  4  7  4  3

南站进站口交叉变量统计

new_se_1<-xtabs(~性别+背包等级, data = se)
ftable(new_se_1)
##      背包等级   0   1   2   3   4
## 性别                             
## 女性           82  87 282 503  71
## 男性           65  46 331 389  77
new_se_2<-xtabs(~年龄+背包等级, data = se)
ftable(new_se_2)
##         背包等级   0   1   2   3   4
## 年龄                                
## 18-45岁          135 133 601 860 127
## 45+岁             12   0  12  32  21
new_se_3<-xtabs(~性别+年龄+背包等级, data = se)
ftable(new_se_3)
##              背包等级   0   1   2   3   4
## 性别 年龄                                
## 女性 18-45岁           82  87 270 491  59
##      45+岁              0   0  12  12  12
## 男性 18-45岁           53  46 331 369  68
##      45+岁             12   0   0  20   9

南站地铁站交叉变量统计

new_ss_1<-xtabs(~性别+背包等级, data = ss)
ftable(new_ss_1)
##      背包等级  0  1  2  3  4
## 性别                        
## 女性          14 42 30 50  6
## 男性          13 12 44 53 13
new_ss_2<-xtabs(~年龄+背包等级, data = ss)
ftable(new_ss_2)
##         背包等级  0  1  2  3  4
## 年龄                           
## 0-18岁            2  7  2  4  0
## 18-45岁          24 43 70 95 13
## 45+岁             1  4  2  4  6
new_ss_3<-xtabs(~性别+年龄+背包等级, data = ss)
ftable(new_ss_3)
##              背包等级  0  1  2  3  4
## 性别 年龄                           
## 女性 0-18岁            1  3  1  2  0
##      18-45岁          13 35 27 44  3
##      45+岁             0  4  2  4  3
## 男性 0-18岁            1  4  1  2  0
##      18-45岁          11  8 43 51 10
##      45+岁             1  0  0  0  3
library(scales)
library(ggplot2)
plot_<-function(files, name){
  ggplot(files,aes(x = as.numeric(files$背包等级)))+
  geom_histogram(color="white",fill="cornflowerblue")+
  facet_grid(files$性别~files$年龄)+
  labs(title="背包等级的年龄和性别分布", x = "背包等级", subtitle = name)
}

plot_(ws, '西站地铁站内') 

plot_(se, '南站进站口')

3.绘图

3.1 西站地铁口

3.1.1频率分布

df_sex<-data.frame(round(prop.table(new_ws_1),2))
df_age<-data.frame(round(prop.table(new_ws_2),2))
p <- ggplot(data = df_sex, mapping = aes(
  x = 背包等级, y=Freq, group=性别, color = 性别, fill = 性别))
p + geom_line()+geom_text(aes(label=Freq), vjust=-0.2)+
  labs(title = "西站地铁站背包等级的频率分布(性别)", x="背包等级", y="频率")

p <- ggplot(data = df_age, mapping = aes(
  x = 背包等级, y=Freq, group=年龄, color = 年龄, fill = 年龄))
p + geom_line()+geom_text(aes(label=Freq), vjust=-0.2)+
  labs(title = "西站地铁站背包等级的频率分布(年龄)", x="背包等级", y="频率")

3.1.2人数分布

new_ws_1<-xtabs(~性别+背包等级, data = ws)
new_ws_1_df<-data.frame(ftable(new_ws_1))
new_ws_1_df['label_sum'] = c(65,25,65,25,50,25,48,24,8,2)
ggplot(new_ws_1_df,aes(x=背包等级, y=Freq, fill=性别))+geom_bar(stat='identity')+
  geom_text(aes(x = 背包等级,y=label_sum, label =Freq , hjust = 'upper'))+labs(
    title = "西站地铁站背包等级的人数分布(性别)",
    x="背包等级", 
    y="人数"
  )

new_ws_2<-xtabs(~年龄+背包等级, data = ws)
new_ws_2_df<-data.frame(ftable(new_ws_2))
new_ws_2_df['label_sum'] = c(70,30,1, 85,50,5,58,40,4,52,25,4,14,8,1)
ggplot(new_ws_2_df,aes(x=背包等级, y=Freq, fill=年龄))+geom_bar(stat='identity')+
  geom_text(aes(x = 背包等级,y=label_sum, label =Freq , hjust = 'upper'))+labs(
    title = "西站地铁站背包等级的人数分布(年龄)",
    x="背包等级", 
    y="人数"
  )

new_ws_3<-xtabs(~性别+年龄+背包等级, data = ws)
ftable(new_ss_3)
##              背包等级  0  1  2  3  4
## 性别 年龄                           
## 女性 0-18岁            1  3  1  2  0
##      18-45岁          13 35 27 44  3
##      45+岁             0  4  2  4  3
## 男性 0-18岁            1  4  1  2  0
##      18-45岁          11  8 43 51 10
##      45+岁             1  0  0  0  3
plot_(ws,"南站地铁内")

3.2 南站进站口

3.2.1频率分布

df_sex<-data.frame(round(prop.table(new_se_1),2))
df_age<-data.frame(round(prop.table(new_se_2),2))
p <- ggplot(data = df_sex, mapping = aes(
  x = 背包等级, y=Freq, group=性别, color = 性别, fill = 性别))
p + geom_line()+geom_text(aes(label=Freq), vjust=-0.2)+
  labs(title = "南站进站口背包等级的频率分布(性别)", x="背包等级", y="频率")

p <- ggplot(data = df_age, mapping = aes(
  x = 背包等级, y=Freq, group=年龄, color = 年龄, fill = 年龄))
p + geom_line()+geom_text(aes(label=Freq), vjust=-0.2)+
  labs(title = "南站进站口背包等级的频率分布(年龄)", x="背包等级", y="频率")

3.2.2人数分布

new_se_1<-xtabs(~性别+背包等级, data = se)
new_se_1_df<-data.frame(ftable(new_se_1))
new_se_1_df['label_sum'] = c(100,20,100,20,400,200,500,200,100,40)
ggplot(new_se_1_df,aes(x=背包等级, y=Freq, fill=性别))+geom_bar(stat='identity')+
  geom_text(aes(x = 背包等级,y=label_sum, label =Freq , hjust = 'upper'))+labs(
    title = "南站进站口背包等级的人数分布(性别)",
    x="背包等级", 
    y="人数"
  )

new_se_2<-xtabs(~年龄+背包等级, data = se)
new_se_2_df<-data.frame(ftable(new_se_2))
new_se_2_df['label_sum'] = c(100,3,100, 5,500,70,700,10,70,10)
ggplot(new_se_2_df,aes(x=背包等级, y=Freq, fill=年龄))+geom_bar(stat='identity')+
  geom_text(aes(x = 背包等级,y=label_sum, label =Freq , hjust = 'upper'))+labs(
    title = "南站进站口背包等级的人数分布(年龄)",
    x="背包等级", 
    y="人数"
  )

new_se_3<-xtabs(~性别+年龄+背包等级, data = se)
ftable(new_se_3)
##              背包等级   0   1   2   3   4
## 性别 年龄                                
## 女性 18-45岁           82  87 270 491  59
##      45+岁              0   0  12  12  12
## 男性 18-45岁           53  46 331 369  68
##      45+岁             12   0   0  20   9
plot_(se,"南站地铁内")

3.3 南站地铁内

3.3.1频率分布

df_sex<-data.frame(round(prop.table(new_ss_1),2))
df_age<-data.frame(round(prop.table(new_ss_2),2))
p <- ggplot(data = df_sex, mapping = aes(
  x = 背包等级, y=Freq, group=性别, color = 性别, fill = 性别))
p + geom_line()+geom_text(aes(label=Freq), vjust=-0.2)+
  labs(title = "南站地铁站背包等级的频率分布(性别)", x="背包等级", y="频率")

p <- ggplot(data = df_age, mapping = aes(
  x = 背包等级, y=Freq, group=年龄, color = 年龄, fill = 年龄))
p + geom_line()+geom_text(aes(label=Freq), vjust=-0.2)+
  labs(title = "南站地铁站背包等级的频率分布(年龄)", x="背包等级", y="频率")

3.3.2人数分布

new_ss_1<-xtabs(~性别+背包等级, data = ss)
new_ss_1_df<-data.frame(ftable(new_ss_1))
new_ss_1_df['label_sum'] = c(24,10,39,9,65,41,85,50,15,10)
ggplot(new_ss_1_df,aes(x=背包等级, y=Freq, fill=性别))+geom_bar(stat='identity')+
  geom_text(aes(x = 背包等级,y=label_sum, label =Freq , hjust = 'upper'))+labs(
    title = "南站地铁站背包等级的人数分布(性别)",
    x="背包等级", 
    y="人数"
  )

new_ss_2<-xtabs(~年龄+背包等级, data = ss)
new_ss_2_df<-data.frame(ftable(new_ss_2))
new_ss_2_df['label_sum'] = c(27,18,1, 52,30,2,72,40,2,102,60,2,18,10,2)
ggplot(new_ss_2_df,aes(x=背包等级, y=Freq, fill=年龄))+geom_bar(stat='identity')+
  geom_text(aes(x = 背包等级,y=label_sum, label =Freq , hjust = 'upper'))+labs(
    title = "南站地铁站背包等级的人数分布(年龄)",
    x="背包等级", 
    y="人数"
  )

new_ss_3<-xtabs(~性别+年龄+背包等级, data = ss)
ftable(new_ss_3)
##              背包等级  0  1  2  3  4
## 性别 年龄                           
## 女性 0-18岁            1  3  1  2  0
##      18-45岁          13 35 27 44  3
##      45+岁             0  4  2  4  3
## 男性 0-18岁            1  4  1  2  0
##      18-45岁          11  8 43 51 10
##      45+岁             1  0  0  0  3
plot_(ss,"南站地铁内")

4.数据检验:卡方检验

4.1 西站地铁站

# 检验:性别与背包等级的相关性
summary(new_ws_1)
## Call: xtabs(formula = ~性别 + 背包等级, data = ws)
## Number of cases in table: 293 
## Number of factors: 2 
## Test for independence of all factors:
##  Chisq = 23.953, df = 4, p-value = 8.163e-05
# 检验:年龄与背包等级的相关性
summary(new_ws_2)
## Call: xtabs(formula = ~年龄 + 背包等级, data = ws)
## Number of cases in table: 293 
## Number of factors: 2 
## Test for independence of all factors:
##  Chisq = 17.602, df = 8, p-value = 0.02441
##  Chi-squared approximation may be incorrect
# 检验:背包等级与年龄和性别的相关性
summary(new_ws_3)
## Call: xtabs(formula = ~性别 + 年龄 + 背包等级, data = ws)
## Number of cases in table: 293 
## Number of factors: 3 
## Test for independence of all factors:
##  Chisq = 49.45, df = 22, p-value = 0.0006955
##  Chi-squared approximation may be incorrect

4.2 南站地铁站

# 检验:性别与背包等级的相关性
summary(new_ss_1)
## Call: xtabs(formula = ~性别 + 背包等级, data = ss)
## Number of cases in table: 277 
## Number of factors: 2 
## Test for independence of all factors:
##  Chisq = 21.856, df = 4, p-value = 0.0002141
# 检验:年龄与背包等级的相关性
summary(new_ss_2)
## Call: xtabs(formula = ~年龄 + 背包等级, data = ss)
## Number of cases in table: 277 
## Number of factors: 2 
## Test for independence of all factors:
##  Chisq = 32.9, df = 8, p-value = 6.431e-05
##  Chi-squared approximation may be incorrect
# 检验:背包等级与年龄和性别的相关性
summary(new_ss_3)
## Call: xtabs(formula = ~性别 + 年龄 + 背包等级, data = ss)
## Number of cases in table: 277 
## Number of factors: 3 
## Test for independence of all factors:
##  Chisq = 65.01, df = 22, p-value = 3.94e-06
##  Chi-squared approximation may be incorrect

4.3 南站进站口

# 检验:性别与背包等级的相关性
summary(new_se_1)
## Call: xtabs(formula = ~性别 + 背包等级, data = se)
## Number of cases in table: 1933 
## Number of factors: 2 
## Test for independence of all factors:
##  Chisq = 26.349, df = 4, p-value = 2.69e-05
# 检验:年龄与背包等级的相关性
summary(new_se_2)
## Call: xtabs(formula = ~年龄 + 背包等级, data = se)
## Number of cases in table: 1933 
## Number of factors: 2 
## Test for independence of all factors:
##  Chisq = 59.48, df = 4, p-value = 3.731e-12
# 检验:背包等级与年龄和性别的相关性
summary(new_se_3)
## Call: xtabs(formula = ~性别 + 年龄 + 背包等级, data = se)
## Number of cases in table: 1933 
## Number of factors: 3 
## Test for independence of all factors:
##  Chisq = 131.25, df = 13, p-value = 1.179e-21
##  Chi-squared approximation may be incorrect

4.4 结论

结论:同一性别或/和同一年龄内部背包等级都存在明显差异,但西站地铁站同一性别内部的背包 数量差异最明显,建议优先使用性别分析;南站进站口和南站地铁站建议优先使用年龄分析。

应用:男性主要无行李,携带大挎包或双肩背包;女性主要携带随身小包。 青少年主要无行李或携带双肩包,青年主要携带随身小包,老年人主要携带随身小包。 南站进站口和南站地铁站直接看18-45岁人数分布图即可。