如何在R中按分组应用多个条件过滤器?

时间:2019-05-29 13:00:56

标签: r dataframe dplyr

我有一个名为“ data”的数据框

enter image description here

我只想选择实际折扣值与折扣值比较并采用较高密封值的那些行。并且如果实际折扣模型 RTD9更高的折扣,则应该获得最高的折扣。

我的最终输出数据帧应如下所示(上面的数据帧中为黄色)

enter image description here

1 个答案:

答案 0 :(得分:2)

我们可以使用> library(data.table) > gc() used (Mb) gc trigger (Mb) max used (Mb) Ncells 1075660 57.5 1899034 101.5 1899034 101.5 Vcells 2609137 20.0 91310117 696.7 99059673 755.8 > DT = data.table(col1 = 1:1e6) > cols = paste0('col', 2:100) > for (col in cols){ DT[, col := 1:1e6, with = F] } There were 50 or more warnings (use warnings() to see the first 50) > > rm_and_release <- function(dt){ + dt <- dt[sample(1e6, 9e5, FALSE)] + print(gc()) + rm(list = "DT", envir = globalenv()) + + print(gc()) + } > > gc() used (Mb) gc trigger (Mb) max used (Mb) Ncells 1076769 57.6 1899034 101.5 1899034 101.5 Vcells 53024698 404.6 91310117 696.7 99059673 755.8 > rm_and_release(DT) used (Mb) gc trigger (Mb) max used (Mb) Ncells 1075902 57.5 1899034 101.5 1899034 101.5 Vcells 97613454 744.8 134081733 1023.0 99059673 755.8 used (Mb) gc trigger (Mb) max used (Mb) Ncells 1075901 57.5 1899034 101.5 1899034 101.5 Vcells 97613454 744.8 160978079 1228.2 99059673 755.8 > exists("DT") [1] FALSE > gc() used (Mb) gc trigger (Mb) max used (Mb) Ncells 1075669 57.5 1899034 101.5 1899034 101.5 Vcells 2613271 20.0 128782463 982.6 99059673 755.8 > ## [1] FALSE 来获取最接近的时间间隔

findInterval