2014-11-14 73 views
0

我已經設法用下面的代碼創建了列「qaurtile」,但是我還想創建一個名爲「quartile_team」的列來顯示每個團隊內的四分位數。我無法弄清楚如何做到這一點。R:如何在組內創建一個四分位數列

幫助表示讚賞,

保羅

# generate dataset 
teams <- c(rep("East", 6), rep("West", 8), rep("North", 7), rep("South", 9)) 
time_spent <- rnorm(30) 
dataset <- as.data.frame(cbind(teams, time_spent)) 
dataset$time_spent <- as.numeric(dataset$time_spent) 

# create quartile column 
dataset <- within(dataset, 
        quartile <- cut(x = time_spent, 
            breaks = quantile(time_spent, probs = seq(0, 1, 0.25)), 
            labels = FALSE, 
            include.lowest = TRUE)) 

回答

2

有更好的方式來做到這一點,但一個快速和骯髒的解決方案可能會使用plyr。我會用你的函數內計算四分位數:

library(plyr) 


ddply(dataset, "teams", function(team){ 

    team_quartile <- cut(x = team$time_spent, breaks = quantile(team$time_spent, probs = seq(0, 1, 0.25)), 
         labels = FALSE, 
         include.lowest = TRUE) 

    data.frame(team, team_quartile) 
}) 

基本上,你想球隊打出數據幀起來,然後在數據幀的每個子集進行計算。您也可以使用tapply。

相關問題