我有一个带有字符串和数字的df.我需要按组筛选最大值,不包括年份(日期类型).

我是这么做的:

test
type Process RegionName Time Level a b c d e f    g       h i j k  l m  n o    p q r s t u v  w
 a1   XYZ_1        ABC 2010 fixed 0 0 0 0 0 0     1     957 0 0 0  0 0  0 0    0 0 0 0 0 0 0  0
 a1   XYZ_2        ABC 2010 fixed 0 0 0 0 0 0     0      61 0 0 0  3 0  0 0    0 0 0 0 0 0 0  0
 a1   XYZ_3        ABC 2010 fixed 0 0 0 0 0 0     0     695 0 0 0  0 0  2 0    0 0 0 0 0 0 0  0
 a1   XYZ_4        ABC 2010 fixed 0 0 0 0 0 0     0     525 0 0 0  0 0  0 0    3 0 0 0 0 0 0  0
 b1   XYZ_5        ABC 2010 fixed 0 0 0 0 0 0 10551 1168053 0 0 0  0 0  0 0    0 0 0 0 0 0 0  0
 b1   XYZ_6        ABC 2010 fixed 0 0 0 0 0 0     0    7571 0 0 0 30 0  0 0    0 0 0 0 0 0 0  0
 b1   XYZ_7        ABC 2010 fixed 0 0 0 0 0 0     0   10883 0 0 0  0 0 51 0    0 0 0 0 0 0 0  0
 b1   XYZ_8        ABC 2010 fixed 0 0 0 0 0 0     0   40453 0 0 0  0 0  0 0  196 0 0 0 0 0 0  0
 b1   XYZ_9        ABC 2010 fixed 0 0 0 0 0 0     0   24464 0 0 0  0 0  0 0    0 0 0 0 0 0 0 53
 c1  XYZ_10        ABC 2010 fixed 0 0 0 0 0 0     0       0 0 0 0 16 0  0 0    0 0 0 0 0 0 0  0
 c1  XYZ_11        ABC 2010 fixed 0 0 0 0 0 0     0     129 0 0 0  0 0  0 0    0 0 0 0 0 0 0  0
 d1  XYZ_12        ABC 2010 fixed 0 0 0 0 0 0     2    1616 0 0 0  0 0  0 0    0 0 0 0 0 0 0  0
 d1  XYZ_13        ABC 2010 fixed 0 0 0 0 0 0     0     762 0 0 4  0 0  0 0    0 0 0 0 0 0 0  0
 d1  XYZ_14        ABC 2010 fixed 0 0 0 0 0 0     0    1002 0 0 0 12 0  0 0    0 0 0 0 0 0 0  0
 d1  XYZ_15        ABC 2010 fixed 0 0 0 0 0 0     0     556 0 0 0  0 0  7 0    0 0 0 0 0 0 0  0
 d1  XYZ_16        ABC 2010 fixed 0 0 0 0 0 0     0  961647 0 0 0  0 0  0 0 4661 0 0 0 0 0 0  0
 d1  XYZ_17        ABC 2010 fixed 0 0 0 0 0 0     0    1381 0 0 0  0 0  0 0    0 0 0 0 0 0 0  3

max_test <- test %>% 
  group_by(type) %>% 
  slice(which.max(a:w)) 
max_test
type  Process RegionName  Time Level a  b c d e f     g       h i j k
a1    XYZ_1   ABC         2010 fixed 0  0 0 0 0 0     1     957 0 0 0
b1    XYZ_5   ABC         2010 fixed 0  0 0 0 0 0 10551 1168053 0 0 0
c1    XYZ_10  ABC         2010 fixed 0  0 0 0 0 0     0       0 0 0 0
d1    XYZ_12  ABC         2010 fixed 0  0 0 0 0 0     2    1616 0 0 0

A1和b1是我所期望的.然而,c1和d1并非如此.

对于C1,我预期为XYZ_11,因为129&>16

对于d1,我预计XYZ_16,因为961647&>1616

你知道我做错了什么吗?

注意:我没有在代码中引入避免时间值.我只做max(a:w).那么,A1可能会认为2010年而不是957年是最高值?

推荐答案

你想要的是:

df %>% 
  group_by(type) %>%
    filter(if_any(a:w, ~.x == max(across(a:w)))) %>%
      ungroup()
      
# A tibble: 4 × 28
  type  Process RegionName  Time Level     a     b     c     d     e     f     g
  <chr> <chr>   <chr>      <int> <chr> <int> <int> <int> <int> <int> <int> <int>
1 a1    XYZ_1   ABC         2010 fixed     0     0     0     0     0     0     1
2 b1    XYZ_5   ABC         2010 fixed     0     0     0     0     0     0 10551
3 c1    XYZ_11  ABC         2010 fixed     0     0     0     0     0     0     0
4 d1    XYZ_16  ABC         2010 fixed     0     0     0     0     0     0     0
# ℹ 16 more variables: h <int>, i <int>, j <int>, k <int>, l <int>, m <int>,
#   n <int>, o <int>, p <int>, q <int>, r <int>, s <int>, t <int>, u <int>,
#   v <int>, w <int>

slice,根据文件:

允许您按行的(整型)位置对行进行索引.

不知道你为什么要用它来过滤.如果您不知道,您可以使用Filter功能进行过滤.Have a read of the manual

R相关问答推荐

如何在R中构建一个将数字变量作为列名的函数?

最优收敛

在之前合并的数据.tables中分配新列后.internal.selfref无效

如何使用行政边界形状文件中的人口普查数据调整格栅数据集中的人口数据

如何生成包含可能条目列表而不是计数的表?

在R中列表的结尾添加数字载体

根据R中两个变量的两个条件删除带有dspirr的行

如何使用R中的dhrr函数将李克特量表的因子列从长转换为宽?

如何从当前行上方找到符合特定条件的最接近值?

使用tidy—select创建一个新的带有mutate的摘要变量

在GGPLATE中将突出的点放在前面

如何在ggplot中标记qqplot上的点?

从BRM预测价值

将文件保存到新文件夹时,切换r设置以不必创建目录

矩阵的堆叠条形图,条形图上有数字作为标签

SHINY:使用JS函数应用的CSS样式显示HTML表格

计算数据帧中指定值之前的行数,仅基于每行之后的future 行,单位为r

如何根据数据帧中的值从该数据帧中提取值?

Geom_Hline将不会出现,而它以前出现了

如何删除R中除数字元素以外的所有元素