r - 过滤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%
我如何过滤and的最大值和最小值month_pct
,year_pct
然后显示对应的id
andprice
对于这些值,我怎么能在 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
和为每个列名选择max
和min
行将因子值转换为数字。
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
推荐阅读
- jenkins - Jenkins Active Choice Reactive Reference Parameter Formatted HTML in MultiBranch pipeline job 获取脚本中的当前分支名称
- android - 如何从通过单击按钮调用的函数内部返回 OnClickListener?
- reactjs - 跨组件更新 React 配置
- flyway - Flyway:重用sql文件
- android - React Native Firebase - 您试图通过调用 firebase.app() 来使用未安装在您的 Android 项目上的 firebase 模块
- vb.net - 使用正则表达式获取字符串下划线 2 和下划线 3 之间的子字符串,vb.net
- javascript - 使用 setState 获得太多的重新渲染
- javascript - 如何使用 socket.io 发送长的复杂数组?
- vba - 如何使用 VBA for powerpoint 使幻灯片中的形状出现在另一张幻灯片中?
- javascript - 表单提交后添加 POST 参数