Skip to content

Instantly share code, notes, and snippets.

@bryanyang0528
Created May 31, 2014 16:58
Show Gist options
  • Save bryanyang0528/5dca2b7c6073419d092a to your computer and use it in GitHub Desktop.
Save bryanyang0528/5dca2b7c6073419d092a to your computer and use it in GitHub Desktop.
Group data
##讀入資料
eva05 = read.csv(file = "201405_Clothes_data.csv", header=TRUE, sep=",")
##計算各區的百分位數,因為要分成五組,所以每20%分ㄧ組
qx_north <- quantile(eva05$北區_均分, c(0, .2, .4, .6 ,.8, 1))
qx_middle <- quantile(eva05$中區_均分, c(0, .2, .4, .6 ,.8, 1))
qx_south <- quantile(eva05$南區_均分, c(0, .2, .4, .6 ,.8, 1))
##建立新變項來儲存分組
eva05$star_north <- 0
eva05$star_middle <- 0
eva05$star_south <- 0
##分為五組,使用FOR迴圈來指定組別和臨界值
for (i in 1:5){
eva05$star_north[eva05$北區_均分 >= qx_north[[i]]] <- i
#####特別注意這邊使用的是[[]]雙括號,表示只取百分位數(是個LIST)的值
}
for (i in 1:5){
eva05$star_middle[eva05$中區_均分 >= qx_middle[[i]]] <- i
}
for (i in 1:5){
eva05$star_south[eva05$南區_均分 >= qx_south[[i]]] <- i
}
##轉出
write.csv(eva05, file = "evalution_201405.csv" , row.names= F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment