繁体   English   中英

过滤 R 中多列的最大值和最小值

[英]Filter maximum and minimum values' of multiple columns in R

我有一个玩具数据集如下:

df <- structure(list(id = 1:11, price = c(40.59, 70.42, 1.8, 1.98, 
65.02, 2.23, 54.79, 54.7, 3.32, 1.77, 3.5), month_pct = structure(c(11L, 
10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 1L, 2L), .Label = c("-19.91%", 
"-8.55%", "1.22%", "1.39%", "1.41%", "1.83%", "2.02%", "2.59%", 
"2.86%", "6.58%", "8.53%"), class = "factor"), year_pct = structure(c(4L, 
9L, 5L, 3L, 10L, 1L, 11L, 8L, 6L, 7L, 2L), .Label = c("-10.44%", 
"-19.91%", "-2.46%", "-35.26%", "-4.26%", "-5.95%", "-6.35%", 
"-6.91%", "-7.95%", "1.51%", "1.54%"), class = "factor")), class = "data.frame", row.names = c(NA, 
-11L))

出去:

   id  price month_pct year_pct
0    1  40.59     8.53%  -35.26%
1    2  70.42     6.58%   -7.95%
2    3   1.80     2.86%   -4.26%
3    4   1.98     2.59%   -2.46%
4    5  65.02     2.02%    1.51%
5    6   2.23     1.83%  -10.44%
6    7  54.79     1.41%    1.54%
7    8  54.70     1.39%   -6.91%
8    9   3.32     1.22%   -5.95%
9   10   1.77   -19.91%   -6.35%
10  11   3.50    -8.55%  -19.91%

我如何过滤month_pctyear_pct最大值最小值,然后显示这些值的对应idprice ,我怎么能在 R 中做到这一点?

预期可能是这样的表格或您方便的其他形式:

  max_min       type      pct  id  price
0     max  month_pct    1.13%   7   1.79
1     min  month_pct   -2.63%   1   1.85
2     max   year_pct    0.83%   2   2.42
3     min   year_pct  -16.06%   9   2.30

谢谢。

您可以获取长格式数据,使用parse_number将因子值转换为数字,并为每个列名 select maxmin行。

library(dplyr)

df %>%
  tidyr::pivot_longer(cols = c(month_pct, year_pct)) %>%
  mutate(value = readr::parse_number(as.character(value))) %>%
  group_by(name) %>%
  slice(which.min(value), which.max(value)) %>%
  mutate(max_min = c('min', 'max'), .before = 'id')

#  max_min    id price name       value
#  <chr>   <int> <dbl> <chr>      <dbl>
#1 min        10  1.77 month_pct -19.9 
#2 max         1 40.6  month_pct   8.53
#3 min         1 40.6  year_pct  -35.3 
#4 max         7 54.8  year_pct    1.54

暂无
暂无

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

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