Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中使用Group By with order函数_R_Sorting_Group By_Data.table - Fatal编程技术网

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

如何在R中使用Group By with order函数,r,sorting,group-by,data.table,R,Sorting,Group By,Data.table,我有一个数据框架,有120000条记录和19个变量,其中2个是state和MonthlyIncome 我必须创建一个新的数据集,其中有来自每个州的前10名(按月收入计算)客户 我尝试了很多选项,包括data.table、dplyr和base,但总是缺少一些东西 data.table: x <- customer_any_360[,order(-dense_rank(MonthlyIncome))[1:10], by = state] x通过在惊人的数据表包中寻找答案,您走上了正确的道路

我有一个数据框架,有120000条记录和19个变量,其中2个是state和MonthlyIncome

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

我尝试了很多选项,包括data.table、dplyr和base,但总是缺少一些东西

data.table:

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

x通过在惊人的
数据表
包中寻找答案,您走上了正确的道路。在这里,我刚刚整理了一些州1到50的数据,并从正态分布N(50000,20000^2)中得出了收入

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

require(data.table)
种子集(123)

mydata如果您想使用
rank
函数,一个选项是
frank
from
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]
甚至仅仅是
订单
就足够了

setDT(customer_any_360)[order(-MonthlyIncome), .SD[1:10], by = state]

使用
dplyr
,根据您的需要,有多种选项,
densite\u-rank
minu-rank
top\u-n
。此外,对于过滤,可以使用
slice
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
from
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,]

使用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

请提供一些示例数据。可能是
customer\u any\u 360[densite\u rank(-MonthlyIncome),.SD[1:10],by=state]
替换
.SD
.I
,提取索引会更快。我猜您正在混合
dplyr
data.table
函数。
data.table选项将是
frank(-MonthlyIncome,ties.method='dense')`Try
df%>%groupby(state)%%>%top\n(10,MonthlyIncome)
回答得好!但是如果您将排序移到
i
参数,并且只执行一次,而不是为每个组调用一次,则效率会更高。谢谢@Ken Benoit和Arun。但正如Arun所说,它只返回2列-如何从数据集中获取所有列?感谢@Arun,我按照您的建议将排序移到了
I
参数@Ranjan Pandey使用新的
.SD
语法,您现在应该可以获得所有列。(我加了一个化装的只是为了演示。)很好。但是,您选择的是前10个。我不认为使用
密集等级
顺序
对领带/无领带有什么区别。。?您可能需要将
frank
移动到
j
和subset
.SD[排名%1:10]
我想您需要为有关系的数据做些什么。谢谢!!请回答几个问题-a)data.table中的.SD[]的作用是什么?b) 在使用ave-use%ini%的base R方法中,当我尝试该方法时,我得到的表的值与其他方法得到的值相同,但这些值没有排序。提前谢谢@RanjanPandey
.SD
是数据表的
子集
关于
ave
,该代码按照与数据集中相同的顺序给出逻辑真/假。因此,当我们将其用于子集时,它将删除错误行。若要订购,您可以稍后使用
订购
。它工作正常!!谢谢只有一个问题,请您解释一下函数如何识别要传递的参数是数据-iris.function将x作为参数,其中x可以是任何东西。唯一的限制是x应该有一个x$Sepal.Length向量。ddply的工作方式是,第一个参数(这里是iris)将作为第三个参数传递给任何函数(在完成第二个参数指定的分组之后)。因此,iris将被传递给函数。
set.seed(24)
customer_any_360 <- data.frame(cust=1:120000, state= sample(state.abb,
  120000, replace=TRUE), MonthlyIncome= sample(1000:6500, 120000, 
     replace=TRUE), stringsAsFactors=FALSE)
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