r - dplyr::slice_max() 中 order_by 参数的功能
问题描述
在and函数的文档中,它说参数可以是要排序的变量或变量的函数。slice_min()
slice_max()
order_by
变量函数是什么意思,如何在实际意义上应用?例如,它可以用于提供分类值的自定义顺序吗?
我已经尝试过在网上进行详尽的信息搜索,但无济于事,所以我转向你们这些好人。谢谢你。
解决方案
变量函数是什么意思,如何实际应用?变量函数是什么意思,如何实际应用?
我认为“变量函数”的最常见用法是您从数据框中作为输入列提供的任何函数,它返回一个数字结果(或至少具有“最大值”值的东西)。这里有几个例子:
## get the row with the highest product of Sepal.Length and Sepal.Width
iris %>% slice_max(Sepal.Length * Sepal.Width)
## here we use the function `*` and the variables `Sepal.Length` and `Sepal.Width`
iris %>% slice_max(nchar(Species))
## get the rows with the longest species name
## here we use the function `nchar` and the variable `Species`
例如,它可以用于提供分类值的自定义顺序吗?
通常,如果您想要分类变量的自定义顺序,我们使用factor
并指定级别的顺序。是的,您可以在其中使用它slice_max
- 最后一个因子水平被认为是最大值:
iris %>% slice_max(Species)
## defaults to alphabetical order - all virginica rows returned
iris %>% slice_max(factor(Species, levels = c("versicolor", "virginica", "setosa")))
## if we make "setosa" the last/max level than setosa rows will be returned
推荐阅读
- spring-boot - 处理 Reactor 中的平行通量
- java - Spring Data JPA 查询、过滤?搜索引擎 ?JPQL?
- python - 使用python中的函数从系统中注销用户
- javascript - 如何从 base64 编码的字符串重建音频 blob?
- c# - 如何为该 C# 中特定元素的自定义类类型列执行 DataTable.Select()
- java - JavaFX 显示可缩放矢量图形?
- html - 如何使相对于屏幕而不是父级的粘性位置
- php - Collection.php 第 1043 行中的 ErrorException:未定义的偏移量:laravel 中的 0
- html - 如何将图标预览添加到谷歌商店应用程序的 html 列表中?
- pytorch - state_dict中keep_vars是什么意思?