[英]Extract rows with min or max value from a Data Frame
Im working on a crash course for R at https://bioinformatics-core-shared-training.github.io/r-crash-course/crash-course.nb.html我正在https://bioinformatics-core-shared-training.github.io/r-crash-course/crash-course.nb.html 上R 速成课程
The problem im facing is to extract rows that are min or max for a certain value.我面临的问题是提取某个值的最小值或最大值的行。
For example, when running例如,当运行
df[df$tmp ==min(df$tmp),]
I get the correct row with the expected value.我得到了具有预期值的正确行。
However, when running the following code但是,当运行以下代码时
df[min(df$tmp),]
I get something else completely.我完全得到了别的东西。
Im wondering what is causing this discrepancy?我想知道是什么导致了这种差异?
Assuming df$Tmp
is numeric with no NAs, min(df$Tmp)
should be returning a number.假设
df$Tmp
是没有 NA 的数字, min(df$Tmp)
应该返回一个数字。 Assuming that number is an integer, i, df[min(df$Tmp),]
will return the ith row of your data frame, assuming that your data frame has an ith row.假设该数字是一个整数, i,
df[min(df$Tmp),]
将返回数据帧的第 i 行,假设您的数据帧有第 i 行。
On the other hand, df[df$Tmp ==min(df$tmp),]
will return the row(s) of df where df$Tmp
is equal to the minimum value in that column.另一方面,
df[df$Tmp ==min(df$tmp),]
将返回 df 的行,其中df$Tmp
等于该列中的最小值。
df[df$Tmp ==min(df$tmp),]
is the correct approach to get what you are looking for. df[df$Tmp ==min(df$tmp),]
是获得所需内容的正确方法。
df[min(df$Tmp),]
returns the row in df
that is equal to min(df$Tmp)
. df[min(df$Tmp),]
返回df
中等于min(df$Tmp)
。 It may result in an error in certain cases for eg when min(df$Tmp)
is not an integer, or is negative, or if it is greater than the number of rows in df
etc. Hope this makes sense.在某些情况下它可能会导致错误,例如当
min(df$Tmp)
不是整数,或者是负数,或者它大于df
的行数等时。希望这是有道理的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.