[英]dplyr mutate rowwise max of range of columns
I can use the following to return the maximum of 2 columns我可以使用以下返回最多 2 列
newiris<-iris %>%
rowwise() %>%
mutate(mak=max(Sepal.Width,Petal.Length))
What I want to do is find that maximum across a range of columns so I don't have to name each one like this我想要做的是在一系列列中找到最大值,这样我就不必像这样命名每个列
newiris<-iris %>%
rowwise() %>%
mutate(mak=max(Sepal.Width:Petal.Length))
Any ideas?有任何想法吗?
Instead of rowwise()
, this can be done with pmax
而不是rowwise()
,这可以用pmax
来完成
iris %>%
mutate(mak=pmax(Sepal.Width,Petal.Length, Petal.Width))
May be we can use interp
from library(lazyeval)
if we want to reference the column names stored in a vector
.可能是我们可以使用interp
从library(lazyeval)
如果我们想引用存储在列名vector
。
library(lazyeval)
nm1 <- names(iris)[2:4]
iris %>%
mutate_(mak= interp(~pmax(v1), v1= as.name(nm1)))
With rlang
and quasiquotation we have another dplyr option.使用rlang
和 quasiquotation,我们还有另一个 dplyr 选项。 First, get the row names that we want to compute the parallel max for:首先,获取我们想要计算并行最大值的行名称:
iris_cols <- iris %>% select(Sepal.Length:Petal.Width) %>% names()
Then we can use !!!
然后我们就可以使用了!!!
and rlang::syms
to compute the parallel max for every row of those columns:和rlang::syms
计算这些列的每一行的并行最大值:
iris %>%
mutate(mak=pmax(!!!rlang::syms(iris_cols)))
rlang::syms
takes a string input (the column names), and turns it into a symbol rlang::syms
接受一个字符串输入(列名),并将其转换为一个符号!!!
unquotes and splices its argument, here the column names取消引用并拼接其参数,这里是列名Which gives:这使:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species mak
1 5.1 3.5 1.4 0.2 setosa 5.1
2 4.9 3.0 1.4 0.2 setosa 4.9
3 4.7 3.2 1.3 0.2 setosa 4.7
4 4.6 3.1 1.5 0.2 setosa 4.6
5 5.0 3.6 1.4 0.2 setosa 5.0
h/t: https://stackoverflow.com/a/47773379/1036500 h/t: https://stackoverflow.com/a/47773379/1036500
Currently (dplyr 1.0.2), this works:目前(dplyr 1.0.2),这有效:
newiris<-iris %>%
rowwise() %>%
mutate(mak=max(c_across(Sepal.Width:Petal.Length)))
this also lets you use selection helpers (starts_with etc).这也让您可以使用选择助手(starts_with 等)。
For selecting some columns without typing whole names when using dplyr
I prefer select
parameter from subset
function.为了在使用dplyr
时选择一些列而不输入全名,我更喜欢从subset
函数中select
参数。
You can get desired result like this:您可以像这样获得所需的结果:
iris %>% subset(select = 2:4) %>% mutate(mak = do.call(pmax, (.))) %>%
select(mak) %>% cbind(iris)
One approach is to pipe the data into select then call pmax
using a function that makes pmax
rowwise (this is very similar to @inscaven's answer that uses do.call
, unfortunately there isn't a rowMaxs
function in R so we have to use a function to make pmax
rowwise -- below I used purrr::pmap
)一种方法是将数据通过管道传输到 select 然后使用使pmax
rowwise 的函数调用pmax
(这与@inscaven 使用do.call
的答案非常相似,不幸的是,R 中没有rowMaxs
函数,因此我们必须使用使pmax
函数——下面我使用了purrr::pmap
)
library(dplyr)
library(purrr)
# to get the value of the max
iris$rowwisemax <- iris %>% select(Sepal.Width:Petal.Length) %>% pmap(pmax) %>% as.numeric
# to get the argmax
iris$whichrowwisemax <- iris %>% select(Sepal.Width:Petal.Length) %>% {names(.)[max.col(.)]}
It seems like @akrun's answer only addresses the cases when you can type in the names of all the variables, whether that's using mutate
directly with mutate(pmax_value=pmax(var1, var2))
or when using lazy evaluation with mutate_
and interp
via mutate_(interp(~pmax(v1, v2), v1=as.name(var1), v2=as.name(var2))
.好像@ akrun的答案只解决时,你可以在所有的变量的名称输入的情况下,不管是使用mutate
直接mutate(pmax_value=pmax(var1, var2))
或使用惰性计算时mutate_
和interp
通过mutate_(interp(~pmax(v1, v2), v1=as.name(var1), v2=as.name(var2))
。
I can see two ways to do this if you want to use the colon syntax Sepal.Length:Petal.Width
or if you happen to have a vector with the column names.如果你想使用冒号语法Sepal.Length:Petal.Width
或者你碰巧有一个带有列名的向量,我可以看到两种方法来做到这一点。
The first is more elegant.第一个更优雅。 You tidy the data and take the maximum among the values when grouped:您整理数据并在分组时取值中的最大值:
data(iris)
library(dplyr)
library(tidyr)
iris_id = iris %>% mutate(id=1:nrow(.))
iris_id %>%
gather('attribute', 'value', Sepal.Length:Petal.Width) %>%
group_by(id) %>%
summarize(max_attribute=max(value)) %>%
right_join(iris_id, by='id') %>%
head(3)
## # A tibble: 3 × 7
## id max_attribute Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <int> <dbl> <dbl> <dbl> <dbl> <dbl> <fctr>
## 1 1 5.1 5.1 3.5 1.4 0.2 setosa
## 2 2 4.9 4.9 3.0 1.4 0.2 setosa
## 3 3 4.7 4.7 3.2 1.3 0.2 setosa
The harder way is to use an interpolated formula.更难的方法是使用内插公式。 This is good if you have a character vector with the names of the variables to be max'ed over or if you the table is too tall/wide for it to be tidied.如果您有一个字符向量,其中包含要最大化的变量名称,或者您的表格太高/太宽而无法整理,这很好。
# Make a character vector of the names of the columns we want to take the
# maximum over
target_columns = iris %>% select(-Species) %>% names
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
# Make a vector of dummy variables that will take the place of the real
# column names inside the interpolated formula
dummy_vars = sapply(1:length(target_columns), function(i) sprintf('x%i', i))
## [1] "x1" "x2" "x3" "x4"
# Paste those variables together to make the argument of the pmax in the
# interpolated formula
dummy_vars_string = paste0(dummy_vars, collapse=',')
## [1] "x1,x2,x3,x4"
# Make a named list that maps the dummy variable names (e.g., x1) to the
# real variable names (e.g., Sepal.Length)
dummy_vars_list = lapply(target_columns, as.name) %>% setNames(dummy_vars)
## $x1
## Sepal.Length
##
## $x2
## Sepal.Width
##
## $x3
## Petal.Length
##
## $x4
## Petal.Width
# Make a pmax formula using the dummy variables
max_formula = as.formula(paste0(c('~pmax(', dummy_vars_string, ')'), collapse=''))
## ~pmax(x1, x2, x3, x4)
# Interpolate the formula using the named variables
library(lazyeval)
iris %>%
mutate_(max_attribute=interp(max_formula, .values=dummy_vars_list)) %>%
head(3)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species max_attribute
## 1 5.1 3.5 1.4 0.2 setosa 5.1
## 2 4.9 3.0 1.4 0.2 setosa 4.9
## 3 4.7 3.2 1.3 0.2 setosa 4.7
Here is a base-R solution: A range of column names can be selected with subset()
.这是一个 base-R 解决方案:可以使用subset()
选择一系列列名。 The rowwise maximum values can be added with a combination of transform()
and apply()
.可以使用transform()
和apply()
的组合添加行式最大值。
newiris <- transform(iris, mak = apply(subset(iris, select=Sepal.Width:Petal.Length), 1, max))
If one wants to use selection helpers like contains()
, starts_with()
we may use如果一个人想使用像contains()
、 starts_with()
这样的选择助手,我们可以使用
library(dplyr)
iris |>
mutate(max_value = purrr::pmap_dbl(select(iris, contains("petal")), pmax, na.rm=TRUE))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.