简体   繁体   English

R - 如何基于来自同一行的另一列的值从列获取值

[英]R - How to get value from a column based on value from another column of same row

I have a data frame (df) with 8 columns and 1200 rows. 我有一个8列和1200行的数据框(df)。 Among those 8 columns I want to find the minimum value of column 7 and find the corresponding value of column 2 in that particular row where the minimum value of column 7 was found. 在这8列中,我想找到第7列的最小值,并在找到第7列最小值的特定行中找到第2列的相应值。 Also column 2 holds characters so I want a character vector giving me its value. 第2列还包含字符,所以我想要一个字符向量给我它的值。

I found the minimum of column 7 using 我找到了第7列的最小值

min_val <- min(as.numeric(df[, 7]), na.rm = TRUE)

Now how do I get the value from column 2 (variable name of column being 'column.2') corresponding to the row in which column 7 contains value of 'min_val' as calculated above? 现在,如何从第2列(列的变量名为'column.2')获取对应于第7列包含上述计算值'min_val'的行的值?

This might be a trivial question but I am new to R so any help will be much appreciated. 这可能是一个微不足道的问题,但我是R的新手,所以任何帮助将不胜感激。

Use which.min to get the minimum value index. 使用which.min获取最小值索引。 Something like : 就像是 :

df[which.min(df[,7]),2]

Note that which.min only returns the first index of the minimum, so if you've got several rows with the same minimal value, you will only get the first one. 请注意,which.min仅返回最小值的第一个索引,因此如果您有多个具有相同最小值的行,则只能获得第一个。

If you want to get all the minimum rows, you can use : 如果要获得所有最小行,可以使用:

df[which(df[,7]==min(df[,7])), 2]

The same answer from juba, but using data.table package (his answer uses just the R base, without the need of loading any libraries). 来自juba的相同答案,但使用data.table包(他的答案仅使用R基,无需加载任何库)。

# Load data.table
library(data.table)

# Get 2nd column's value correspondent to the first minimum value in 7th column 
df[which.min(V7), V2]

# Get all respective values in 2nd column correspondent to the minimum value in 7th column 
df[V2 == min(V7), V2]

For handling data.frame-like objects, data.table is quite handly and helpful, just like the dplyr package. 对于处理类似数据帧的对象, data.table非常方便有用,就像dplyr包一样。 It's worth to look at them. 值得一看的是它们。

Here I've assumed your colnames were named as V1..V8. 在这里,我假设你的名字命名为V1..V8。 Otherwise, just replace the V7/V2 with the respective column names in 7th and 2nd position of your data, respectively. 否则,只需将V7 / V2分别替换为数据第7位和第2位的相应列名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据同一行另一列的最高值从 1 列中获取值? - How do I get the value from 1 column based on highest value of another column of the same row? 根据R中同一行的另一列中的单元格的值对一列求和 - Sum a column based on the value of a cell in another column of the same row in R R 在某些条件下用同一行中另一列的值替换或保留一列中的值 - R Replace value or keep value in one column with value from another column in the same row with certain conditions 如何从另一行获取值取决于 R 中另一列中的值 - How to get the value from another row depends on value in another column in R 如何根据 R 中另一列的值从一列中减去一个值? - How to subtract a value from one column based on the value of another in R? 如何使用R中另一列的键从dataframe的一列中获取值? - How to get the value from a column of a dataframe with the key of another column in R? 如何计算基于同一列(在 R 中)中的前一个值的值行? - How to calculate a value row that is based on the previous value in the same column (in R)? 对于 R 中的多列,如何按组从另一行值中减去另一行值并将值分配给同一列中的不同行 - How to subtract one row value from another and assign the value to a different row all in the same column, by group, for multiple columns in R 从基于 R 中另一列中的条件的列中获取值? - Obtain value from a column based off condition in another column in R? 如何 select 一行中具有相同值的多行在 R 的列中保留没有 null 值的行? - how to select a row from multiple rows with the same value in a column keep rows without null value in a column in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM