英文:
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 <- 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)
答案1
得分: 0
使用 slice_min
library(dplyr)
df %>%
slice_min(Z) %>%
pull(Y)
[1] 5
英文:
Using slice_min
library(dplyr)
df %>%
slice_min(Z) %>%
pull(Y)
[1] 5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论