Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从与dataframe中其他列的最大值相对应的列中检索值_R_Dataframe - Fatal编程技术网

如何从与dataframe中其他列的最大值相对应的列中检索值

如何从与dataframe中其他列的最大值相对应的列中检索值,r,dataframe,R,Dataframe,我有这样一个数据帧: [,1] [,2] [,3] [,4] [,5] [,6] a 1 5 7 9 4 b 2 8 2 3 5 c 3 9 2 4 8 d 4 2 6 1 3 [1] "d" "c" "a" "a" "c" 我想根据其他列的最大值获得列[,1]的值。结果应该是这样的: [,1] [,2] [,3] [,4] [,5] [,6] a

我有这样一个数据帧:

[,1] [,2] [,3] [,4] [,5] [,6]
 a    1    5     7    9    4
 b    2    8     2    3    5
 c    3    9     2    4    8
 d    4    2     6    1    3
[1] "d" "c" "a" "a" "c"
我想根据其他列的最大值获得列[,1]的值。结果应该是这样的:

[,1] [,2] [,3] [,4] [,5] [,6]
 a    1    5     7    9    4
 b    2    8     2    3    5
 c    3    9     2    4    8
 d    4    2     6    1    3
[1] "d" "c" "a" "a" "c"

提前感谢您。

如果您有一个数据帧,您可以使用
which.max
获取每列中的索引最大值,并使用该索引对第一列中的值进行子集

df$V1[sapply(df[-1], which.max)]
#[1] "d" "c" "a" "a" "c"

如果您有矩阵(看起来更像),那么由于第一列的原因,所有值都将变成字符,在这种情况下,您可以执行以下操作:

mat[apply(mat[, -1], 2, function(x) which.max(as.numeric(x))), 1]
#[1] "d" "c" "a" "a" "c"
数据

df <- structure(list(V1 = c("a", "b", "c", "d"), V2 = 1:4, V3 = c(5L, 
8L, 9L, 2L), V4 = c(7L, 2L, 2L, 6L), V5 = c(9L, 3L, 4L, 1L), 
V6 = c(4L, 5L, 8L, 3L)), row.names = c(NA, -4L), class = "data.frame")

mat <- structure(c("a", "b", "c", "d", "1", "2", "3", "4", "5", "8", 
"9", "2", "7", "2", "2", "6", "9", "3", "4", "1", "4", "5", "8", 
"3"), .Dim = c(4L, 6L))
df <- structure(list(V1 = c("a", "b", "c", "d"), V2 = 1:4, V3 = c(5L, 
8L, 9L, 2L), V4 = c(7L, 2L, 2L, 6L), V5 = c(9L, 3L, 4L, 1L), 
    V6 = c(4L, 5L, 8L, 3L)), class = "data.frame", row.names = c(NA, 
-4L))

m <- structure(c("a", "b", "c", "d", "1", "2", "3", "4", "5", "8", 
"9", "2", "7", "2", "2", "6", "9", "3", "4", "1", "4", "5", "8", 
"3"), .Dim = c(4L, 6L))

df您可以使用
max.col

  • 如果是数据帧
    df
  • 如果是矩阵
    m
数据

df <- structure(list(V1 = c("a", "b", "c", "d"), V2 = 1:4, V3 = c(5L, 
8L, 9L, 2L), V4 = c(7L, 2L, 2L, 6L), V5 = c(9L, 3L, 4L, 1L), 
V6 = c(4L, 5L, 8L, 3L)), row.names = c(NA, -4L), class = "data.frame")

mat <- structure(c("a", "b", "c", "d", "1", "2", "3", "4", "5", "8", 
"9", "2", "7", "2", "2", "6", "9", "3", "4", "1", "4", "5", "8", 
"3"), .Dim = c(4L, 6L))
df <- structure(list(V1 = c("a", "b", "c", "d"), V2 = 1:4, V3 = c(5L, 
8L, 9L, 2L), V4 = c(7L, 2L, 2L, 6L), V5 = c(9L, 3L, 4L, 1L), 
    V6 = c(4L, 5L, 8L, 3L)), class = "data.frame", row.names = c(NA, 
-4L))

m <- structure(c("a", "b", "c", "d", "1", "2", "3", "4", "5", "8", 
"9", "2", "7", "2", "2", "6", "9", "3", "4", "1", "4", "5", "8", 
"3"), .Dim = c(4L, 6L))

df我们可以重塑为“长”格式,然后
slice
获取第一列的值(假设它是
data.frame

library(dplyr)
library(tidyr)
df %>%
    pivot_longer(cols = -V1) %>%
    group_by(name) %>% 
    slice(which.max(value)) %>% 
    pull(V1)
#[1] "d" "c" "a" "a" "c"
如果是
矩阵
,则使用
as.data.frame
转换为
data.frame

library(dplyr)
library(tidyr)
df %>%
    pivot_longer(cols = -V1) %>%
    group_by(name) %>% 
    slice(which.max(value)) %>% 
    pull(V1)
#[1] "d" "c" "a" "a" "c"
数据
df