本文介绍了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))

有什么想法吗?

推荐答案

可以使用 pmax

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

也许我们可以在库中使用 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改变列范围的行最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 10:08