如何在R中使用Group By和order函数

时间:2015-07-08 06:11:04

标签: r sorting group-by data.table

我有一个包含120000条记录和19个变量的数据框,其中2个是州和月收入。

我必须创建一个新的数据集,其中包含来自每个州的前10名(月收入)客户。

我尝试了很多选项,包括data.table和dplyr以及base,但总有一些缺失。

data.table:

 x <- customer_any_360[,order(-dense_rank(MonthlyIncome))[1:10], by = state]

---我试过的例子

有人可以请求帮助,我还是R的新手并且真的在努力解决这个问题。在此先感谢!!

3 个答案:

答案 0 :(得分:8)

如果您想使用SYSTEMTIME个功能,rank中的一个选项为frank,您可以在data.table中指定选项。

ties.method

甚至只是library(data.table)#v1.9.5+ setDT(customer_any_360)[, .SD[frank(-MonthlyIncome, ties.method='dense') %in% 1:10], by = state] 就足够了

order

使用setDT(customer_any_360)[order(-MonthlyIncome), .SD[1:10], by = state] ,有多个选项,dplyrdense_rankmin_rank取决于您想要的内容。此外,对于过滤,可以使用top_nslice

filter

或使用library(dplyr) customer_any_360 %>% group_by(state) %>% slice(dense_rank(-MonthlyIncome)[1:10])

sqldf

或使用 library(sqldf) sqldf('select * from customer_any_360 i where rowid in (select rowid from customer_any_360 where state = i.state order by MonthlyIncome desc limit 10) order by i.state, i.MonthlyIncome desc')

中的ave
base R

编辑:indx <- with(customer_any_360, ave(-MonthlyIncome, state, FUN=function(x) rank(x, ties.method='first')) %in% 1:10) customer_any_360[indx,] 选项是根据@Arun

的建议编辑的

数据

frank

答案 1 :(得分:3)

使用plyr包中的ddply:

data(iris)
ddply(iris, "Species", function(x) head(x[order(x$Sepal.Length, decreasing = TRUE) , ], 2))
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1          5.8         4.0          1.2         0.2     setosa
2          5.7         4.4          1.5         0.4     setosa
3          7.0         3.2          4.7         1.4 versicolor
4          6.9         3.1          4.9         1.5 versicolor
5          7.9         3.8          6.4         2.0  virginica
6          7.7         3.8          6.7         2.2  virginica

答案 2 :(得分:2)

通过在令人惊叹的data.table套餐中寻找答案,您已走上正轨。在这里,我只为1至50州编制了一些数据,并从正态分布N(50000,20000 ^ 2)中提取收入。

根据来自@Arun的条评论进行编辑,并请求OP中的所有列(使用.SD隐藏变量):

require(data.table)
set.seed(123)
mydata <- data.table(state = 1:50, 
                     monthlyIncome = round(rnorm(120000, 50000, 20000)),
                     anothervar = 1:120000)
selecteddata <- mydata[order(-monthlyIncome), head(.SD, 10), by = state]

# just to verify
selecteddata <- selecteddata[order(state, -monthlyIncome)]
mydata <- mydata[order(-monthlyIncome)]
identical(selecteddata[1:10], mydata[state==1][1:10])  # state 1
identical(selecteddata[11:20], mydata[state==2][1:10]) # state 2
相关问题