[英]value of one column based on value of another column in R
I have to find value minimum value in one column and based on that value I have to find the value corresponding in another column.我必须在一列中找到值最小值,并根据该值在另一列中找到对应的值。
my data set is K and i have column name as X, Y:我的数据集是 K,我的列名是 X,Y:
> K
X Y
1 2 3
2 4 5
3 6 7
4 8 9
5 10 11
The columns have these values and i find the minimum value of column Y using这些列具有这些值,我使用找到 Y 列的最小值
apply(K[c(2)],2,min) # this gives me 3.
now i have to relate it to column X which i am finding difficult.现在我必须将它与我发现困难的 X 列联系起来。
I am totally new to R and i am still learning.我对 R 完全陌生,我还在学习。 Also i don't know any other libraries other than,
library(readr)
.我也不知道除了
library(readr)
之外的任何其他库。
Assuming that you want to get the entry in X
which is placed in the same row as the minimum of Y
, you can try:假设您想获取
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]
Regarding the second part of your question, Datacamp has a free introductory course to R covering the very basics.关于您问题的第二部分,Datacamp 有一个免费的 R 入门课程,涵盖了非常基础的内容。
No need for apply
.无需
apply
。 If you know there is only one value of Y that is the lowest or you need the first record of the lowest Y, you can use which.min
.如果您知道只有一个 Y 值是最低的,或者您需要最低 Y 的第一条记录,您可以使用
which.min
。 Otherwise use min
as this is more robust.否则使用
min
因为这更健壮。 In your example it doesn't matter, but test both methods on your full data.frame to see if there is a difference.在您的示例中,这并不重要,但是在您的完整 data.frame 上测试这两种方法,看看是否有区别。
K[K$Y == min(K$Y), ]
X Y
1 2 3
K[which.min(K$Y), ]
X Y
1 2 3
Really depends on what you mean by "relate".真的取决于你所说的“相关”是什么意思。 Let's say you want to find all rows where X is less than the minimum of row Y:
假设您要查找 X 小于 Y 行最小值的所有行:
K[K$X<min(K$Y),]
Will result in:将导致:
X Y
1 1 2
The command is a subset command, it asks to give all the columns of any row where X is smaller than the minimum of the entire column of Y.该命令是一个子集命令,它要求给出任何行的所有列,其中 X 小于 Y 的整个列的最小值。
I had the same problem, and there's a "tidier" way to do this with a mutate and filter:我遇到了同样的问题,并且有一种“更整洁”的方法可以使用 mutate 和 filter 来做到这一点:
K %>% mutate(minY=min(Y)) %>% filter(Y=minY).
This can be used with group_by as well if you need the minimum record for each group.如果您需要每个组的最小记录,这也可以与 group_by 一起使用。
If you are willing to explore other libraries (which eventually you will...), data.table
is the one worth exploring.如果您愿意探索其他库(最终您会...),
data.table
是值得探索的。 with data.table
-与
data.table
-
k <- data.table(
X = c(2, 4, 6, 8, 10),
Y = c(3, 5, 7, 9, 11)
)
will give you -会给你 -
Description:data.table [5 × 2]
X Y
<dbl> <dbl>
2 3
4 5
6 7
8 9
10 11
5 rows
Now to find minimum of Y and corresponding X value, you just need this -现在要找到 Y 的最小值和相应的 X 值,您只需要这个 -
k[, .(min_Y = min(Y), corr_X = X[which.min(Y)])]
which will give you -这会给你 -
Description:data.table [1 × 2]
min_Y corr_X
<dbl> <dbl>
3 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.