简体   繁体   English

根据 R 中另一列的最小值获取一列的对应值

[英]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.

相关问题 R:从数据框中的一列返回一个值,该值对应于另一列中的最小值 - R: return a value from one column in a data frame corresponding to the minimum value in another column 从基于 R 中另一列中的条件的列中获取值? - Obtain value from a column based off condition in another column in R? R data.table如何基于另一列的值从多个列之一(按列NAME)获取VALUE - R data.table How to obtain the VALUE from one of many columns (by column NAME), based on the value of another column 根据r中的另一列更改一列的值 - Changing the value of one column based on another in r 根据另一个 R 检查一列的值 - Check value of one column based on another R R-根据另一列中的NA值更改一列中的值 - R - Change value in one column based on NA value in another column 一列的值基于R中另一列的值 - value of one column based on value of another column in R 如何根据 R 中另一列的值从一列中减去一个值? - How to subtract a value from one column based on the value of another in R? 如何根据另一列中的相应值将值添加到新列中? - How to add value into new column based on corresponding value in another column? R Dataframe:如何根据另一列中的相应值乘以一列中的过滤值? - R Dataframe: How to multiply filtered values in a column based on the corresponding value in another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM