dplyr 改变列范围的行列最大值 [英] dplyr mutate rowwise max of range of columns

查看:18
本文介绍了dplyr 改变列范围的行列最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下内容返回最多 2 列

I can use the following to return the maximum of 2 columns

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))

有什么想法吗?

推荐答案

代替 rowwise(),这可以用 pmax

iris %>%
      mutate(mak=pmax(Sepal.Width,Petal.Length, Petal.Width))

如果我们想引用存储在 vector 中的列名,也许我们可以使用 library(lazyeval) 中的 interp.>

May be we can use interp from library(lazyeval) if we want to reference the column names stored in a vector.

library(lazyeval)
nm1 <- names(iris)[2:4]
iris %>% 
     mutate_(mak= interp(~pmax(v1), v1= as.name(nm1)))

这篇关于dplyr 改变列范围的行列最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆