gpt4 book ai didi

r - 基于 R 中另一列值的一列值

转载 作者:行者123 更新时间:2023-12-03 23:15:39 24 4
gpt4 key购买 nike

我必须在一列中找到最小值,并根据该值在另一列中找到对应的值。

我的数据集是 K,我的列名是 X、Y:

> K
X Y
1 2 3
2 4 5
3 6 7
4 8 9
5 10 11

这些列具有这些值,我使用以下方法找到 Y 列的最小值
apply(K[c(2)],2,min) # this gives me 3.

现在我必须将它与我发现困难的 X 列联系起来。

我对 R 完全陌生,我仍在学习。另外我不知道除了 library(readr) 之外的任何其他图书馆.

最佳答案

假设您想获取 X 中的条目与 Y 的最小值放在同一行, 你可以试试:

# create example data.frame:
K <- data.frame(X = seq(2, 10, 2), Y = seq(3, 11, 2))

# find index of minimum entry in column Y:
idx <- match(min(K$Y), K$Y) # gives you the first entry of the minimum
idx2 <- which(min(K$Y) %in% K$Y) # gives you all indices of the minimum

# output the corresponding element (or elements) in column X:
K$X[idx]
K$X[idx2]

关于您问题的第二部分,Datacamp 有一个免费的 R 入门类(class),涵盖了非常基础的知识。

关于r - 基于 R 中另一列值的一列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51349178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com