R: 根据另一列对应行打印列中的一个值

huangapple go评论72阅读模式
英文:

R: Print A Value from A Column Based on the Corresponding Row of Another Column

问题

在R中,我想打印(作为标量)列Z的最小值对应的列Y的值。例如,由于列Z中的最小值为1.928718,我将选择列Y中相应的行值,即5。以下是代码示例:

df <- read.table(text = 
  "X Y Z
  a  2 3.258206
  b  6 2.825460
  c  5 1.928718
  d  3 3.656937
  e  3 2.060350",
  header = TRUE)

min_Z_row <- df[which.min(df$Z), "Y"]

min_Z_row 中的值将是5

英文:

I have this data frame df in R in which I want to print (as a scalar) a value of column Y based on the corresponding row that is the minimum value of column Z. For example, since 1.928718 is the minimum in column Z I will pick the corresponding row value in column Y which is 5.

df &lt;- read.table(text = 
      &quot;X Y Z
      a  2 3.258206
      b  6 2.825460
      c  5 1.928718
      d  3 3.656937
      e  3 2.060350&quot;,
      header = TRUE)

答案1

得分: 0

使用 slice_min

library(dplyr)
df %>%
 slice_min(Z) %>%
 pull(Y)
[1] 5
英文:

Using slice_min

library(dplyr)
df %&gt;% 
 slice_min(Z) %&gt;%
 pull(Y)
[1] 5

huangapple
  • 本文由 发表于 2023年2月27日 00:31:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定