Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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
R 在TIBLE中查找行最小值和列索引_R_Purrr_Tibble - Fatal编程技术网

R 在TIBLE中查找行最小值和列索引

R 在TIBLE中查找行最小值和列索引,r,purrr,tibble,R,Purrr,Tibble,我有以下几点: > df <- tibble( ID = LETTERS[1:4], a = c(1,5,9,8), b = c(5,9,8,2), c = c(5,4,5,5) ) > df # A tibble: 4 x 4 ID a b c <chr> <dbl> <dbl> <dbl> 1 A 1 5 5

我有以下几点:

> df <- tibble(
     ID = LETTERS[1:4],
     a  = c(1,5,9,8),
     b  = c(5,9,8,2),
     c  = c(5,4,5,5)
)

> df
# A tibble: 4 x 4
  ID        a     b     c
  <chr> <dbl> <dbl> <dbl>
1 A         1     5     5
2 B         5     9     4
3 C         9     8     5
4 D         8     2     5
> 
我不想使用rowwise

谢谢大家!

您可以将pmin与do.call一起使用来获取行最小值,并对max.col一起使用的值求反以获取最小值的列索引

library(dplyr)
library(purrr)

df %>%
  mutate(Min = do.call(pmin, select(., a:c)), 
         Col_Index = max.col(-select(., a:c)))

#  ID        a     b     c   Min Col_Index
#  <chr> <dbl> <dbl> <dbl> <dbl>     <int>
#1 A         1     5     5     1         1
#2 B         5     9     4     4         3
#3 C         9     8     5     5         3
#4 D         8     2     5     2         2
您可以将pmin与do.call一起使用来获取行最小值,并对max.col一起使用的值求反以获取最小值的列索引

library(dplyr)
library(purrr)

df %>%
  mutate(Min = do.call(pmin, select(., a:c)), 
         Col_Index = max.col(-select(., a:c)))

#  ID        a     b     c   Min Col_Index
#  <chr> <dbl> <dbl> <dbl> <dbl>     <int>
#1 A         1     5     5     1         1
#2 B         5     9     4     4         3
#3 C         9     8     5     5         3
#4 D         8     2     5     2         2
一种选择是:

df %>%
 rowwise() %>%
 mutate(min = min(c_across(a:c)),
        min_index = which.min(c_across(a:c)))

  ID        a     b     c   min min_index
  <chr> <dbl> <dbl> <dbl> <dbl>     <int>
1 A         1     5     5     1         1
2 B         5     9     4     4         3
3 C         9     8     5     5         3
4 D         8     2     5     2         2
一种选择是:

df %>%
 rowwise() %>%
 mutate(min = min(c_across(a:c)),
        min_index = which.min(c_across(a:c)))

  ID        a     b     c   min min_index
  <chr> <dbl> <dbl> <dbl> <dbl>     <int>
1 A         1     5     5     1         1
2 B         5     9     4     4         3
3 C         9     8     5     5         3
4 D         8     2     5     2         2
基本R解决方案:

setNames(cbind(df, t(apply(df[, vapply(df, is.numeric, logical(1))], 1, function(row) {
  cbind(min(row), which.min(row))}))), c(names(df), "min", "col_index"))
基本R解决方案:

setNames(cbind(df, t(apply(df[, vapply(df, is.numeric, logical(1))], 1, function(row) {
  cbind(min(row), which.min(row))}))), c(names(df), "min", "col_index"))

是的,您可以使用pmap_dbl。请参阅更新的答案。非常感谢Ronak Shah…这是我一直在寻找的。@Christian很高兴能得到帮助!通过单击左侧的复选标记,您可以自由选择。每个帖子只能接受一个答案。是的,你可以使用pmap_dbl。请参阅更新的答案。非常感谢Ronak Shah…这是我一直在寻找的。@Christian很高兴能得到帮助!通过单击左侧的复选标记,您可以自由选择。每个帖子只能接受一个答案。