[英]obtain corresponding value of one column based on minimum value of another in R
I have several pairs of variables eg X1 and Y1, X2 and Y2...Xn and Yn etc).我有几对变量,例如 X1 和 Y1、X2 和 Y2...Xn 和 Yn 等)。 I know hoe to obtain the minimum value of some of the columns eg (X1, X2...Xn), but I would like to find the Y value that corresponds to the minimum X value.
我知道要获得某些列的最小值,例如(X1,X2...Xn),但我想找到对应于最小 X 值的 Y 值。 I was thinking potentially some sort of key/value pair... would do the trick, but cant quite figure out how to implement it.
我在想可能是某种键/值对......会做到这一点,但不能完全弄清楚如何实现它。 If someone could help with any form of a solution, that would be fantastic.
如果有人可以提供任何形式的解决方案,那就太好了。
# Make some dummy data
X1 = c(1,20,3,40,5)
Y1 = c(20,32,60,82,100)
X2 = c(10,2,30,4,50)
Y2= c(2,30,6,80,10)
df = data.frame(X1,Y1,X2,Y2)
# This is what the dataframe looks like
X1 Y1 X2 Y2
1 1 20 10 2
2 20 32 2 30
3 3 60 30 6
4 40 82 4 80
5 5 100 50 10
# create column of minimum X values
df$minX = c(pmin( df[,1], df[,3]))
I want to create another column with the Y value corresponding to the minimum value of X, but cant quite figure out how to do it.我想创建另一列,其 Y 值对应于 X 的最小值,但不知道该怎么做。 In the example above, the resultant dataframe should look something like the following.
在上面的示例中,生成的 dataframe 应如下所示。 NOTE: The corresponding Y value is not necessarily max or min).
注意:相应的 Y 值不一定是最大值或最小值)。
X1 Y1 X2 Y2 minX correspondingY
1 1 20 10 2 1 20
2 20 32 2 30 2 30
3 3 60 30 6 3 60
4 40 82 4 80 4 80
5 5 100 50 10 5 100
Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance.
提前致谢。
Maybe you can try the code below也许你可以试试下面的代码
X <- df[startsWith(names(df), "X")]
df$minX <- do.call(pmin, X)
df$correspondingY <- df[startsWith(names(df), "Y")][cbind(seq(nrow(X)), max.col(-X))]
which gives这使
> df
X1 Y1 X2 Y2 minX correspondingY
1 1 20 10 2 1 20
2 20 32 2 30 2 30
3 3 60 30 6 3 60
4 40 82 4 80 4 80
5 5 100 50 10 5 100
In tidyverse
you can do:在
tidyverse
,您可以执行以下操作:
library(dplyr)
df1 <- df %>% mutate(row = row_number())
df1 %>%
inner_join(
df1 %>%
tidyr::pivot_longer(cols = -row,
names_to = c('.value'),
names_pattern = '([A-Z])') %>%
group_by(row) %>%
slice(which.min(X)), by = 'row')
# X1 Y1 X2 Y2 row X Y
#1 1 20 10 2 1 1 20
#2 20 32 2 30 2 2 30
#3 3 60 30 6 3 3 60
#4 40 82 4 80 4 4 80
#5 5 100 50 10 5 5 100
You can remove the row
column if it is not needed.如果不需要,您可以删除
row
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.