R中具有数据帧的每一行的最小值 [英] min for each row with dataframe in R

查看:293
本文介绍了R中具有数据帧的每一行的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试对数据帧中的多个列进行简单的最小值计算,但是min函数会自动返回整个列的最小值,而不是分别为每一行返回最小值.我确定我在这里遗漏了一些非常简单的东西?任何想法都很感激.

I'm try to do a simple minimum across multiple columns in a data frame but the min function automatically returns the min across the whole of each column rather than for each row separately. I'm sure I'm missing something really simple here? Any ideas much appreciated.

x<-c(1,2,7)
y<-c(1,5,4)
minIwant <- c(1,2,4)
df <- data.frame(x,y,minIwant)
df$minIget <- min(df$x,df$y)
df
  x y minIwant minIget
1 1 1        1       1
2 2 5        2       1
3 7 4        4       1

推荐答案

您可以使用apply浏览每一行

apply(df, 1, FUN=min)

如果1表示将FUN应用于df的每一行,则2表示将FUN应用于列.

Where 1 means to apply FUN to each row of df, 2 would mean to apply FUN to columns.

这篇关于R中具有数据帧的每一行的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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