简体   繁体   English

R:相当于“rank”和“partition”

[英]R: Equivalent of "rank" and "partition"

I am working with the R programming language.我正在使用 R 编程语言。 I have the following SQL query:我有以下 SQL 查询:

select b.var1 as var1, b.var2 from
(select *, rank() over( partition by var1 order by var3) as rank1
from my_table)b; 

The goal of this SQL code is to:此 SQL 代码的目标是:

  • Find groups of records containing duplicate values of the same "var1"查找包含相同“var1”重复值的记录组
  • For each of these groups, sort these records based on their values of "var3"对于这些组中的每一个,根据它们的“var3”值对这些记录进行排序
  • For each of these groups of sorted duplicate records, only keep the the record with the largest value of "var3"对于每组排序的重复记录,只保留“var3”值最大的记录
  • Note: Records containing non-duplicate values of "var1" are left untouched注意:包含“var1”的非重复值的记录保持不变

My Question: Does anyone know if it is possible to run this same code in R?我的问题:有谁知道是否可以在 R 中运行相同的代码? For example:例如:

library(RODBC)
library(dbi)
library(odbc)
library(sqldf)

dbWriteTable(my_db_connection, SQL("   select b.var1 as var1, b.var2 from
    (select *, rank() over( partition by var1 order by var3) as rank1
    from my_table)b " ), results_of_this_query)

Does anyone know if it is possible to do this in R?有谁知道是否可以在 R 中执行此操作? Does R recognize SQL commands like "rank()", "over" and "partition"? R 是否识别 SQL 命令,如“rank()”、“over”和“partition”?

Thanks!谢谢!

Consider ave with rank after you sort by partition and order columns:按分区和排序列排序后,考虑averank

df <- with(df, df[order(var1, var3),])

df$rank1 <- with(df, ave(1:nrow(df), var1, FUN=rank))

Add lambda for other arguments:为其他 arguments 添加 lambda:

df$rank1 <- with(df, ave(1:nrow(df), var2, FUN=function(x) rank(x, ties.method="first", na.last="keep")))

The Tidyverse collection of packages specially the dplyr can easy do this task for you on a grammar likely SQL. Tidyverse 软件包集合特别是dplyr可以轻松地为您完成此任务,语法可能为 SQL。

The final code should be something like最终代码应该类似于

df %>%
group_by(var1) %>%
mutate(rank = dense_rank(var3))

I may not understand you SQL-string and made the correctly translated because SQL language is not my strong skill.我可能无法理解您的 SQL 字符串并正确翻译,因为 SQL 语言不是我的强项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM