Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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数据帧中按行查找最大绝对值_R_Dataframe_Max_Absolute_Rowwise - Fatal编程技术网

在R数据帧中按行查找最大绝对值

在R数据帧中按行查找最大绝对值,r,dataframe,max,absolute,rowwise,R,Dataframe,Max,Absolute,Rowwise,我希望找到一种矢量化方法,从数据帧中的多个列中获取绝对最大值 基本上有一个与pmax函数等价的函数来获得绝对最大值 test_df <- tibble( some_identifier = c("apple", "tunafish", "turkey_sandwich"), val_a = c(-1, 2, 0), val_b = c(-3, 3, NA), val_c = c(2, 3, 1) ) # this is what abs_max column s

我希望找到一种矢量化方法,从数据帧中的多个列中获取绝对最大值

基本上有一个与pmax函数等价的函数来获得绝对最大值

test_df <- tibble(
  some_identifier = c("apple", "tunafish", "turkey_sandwich"), 
  val_a =  c(-1, 2, 0), 
  val_b = c(-3, 3, NA), 
  val_c = c(2, 3, 1)

)

# this is what abs_max column should be 
test_df$abs_max <- c(-3, 3, 1)
test_df

# A tibble: 3 x 5
  some_identifier val_a val_b val_c abs_max
  <chr>           <dbl> <dbl> <dbl>   <dbl>
1 apple              -1    -3     2      -3
2 tunafish            2     3     3       3
3 turkey_sandwich     0    NA     1       1

test\u df这里有一种使用
max.col
的方法-感谢@Gregor

f <- function(data) {
  tmp <- Filter(is.numeric, data)
  if(inherits(data, "tbl_df")) {
    tmp <- as.matrix(tmp)
  }
  tmp[cbind(1:nrow(tmp),
            max.col(replace(x <- abs(tmp), is.na(x), -Inf)))]
}

f(test_df)
# [1] -3  3  1
(在上述函数中调用
tmp

然后

这是一个data.frame,其中
NA
s替换为
-Inf
,所有负值替换为其绝对值

max.col
返回每行最大值的列位置

max.col(replace(x <- abs(Filter(is.numeric, test_df)), is.na(x), -Inf))
# [1] 2 2 3
数据

test_df <- data.frame(
  some_identifier = c("apple", "tunafish", "turkey_sandwich"), 
  val_a =  c(-1, 2, 0), 
  val_b = c(-3, 3, NA), 
  val_c = c(2, 3, 1), stringsAsFactors = FALSE)

test_df谢谢-这是我一直在寻找的实用解决方案类型。我不知道max.col函数。Tibble的行为出乎意料,但很好知道(我通常在tidyverse工作)<代码>>mtcars[cbind(1:3,4:6)][1]110.00 3.90 2.32>dplyr::as_tible(mtcars)[cbind(1:3,4:6)]错误:必须在“[”中使用向量,而不是类矩阵的对象。
#  val_a val_b val_c
#1     1     3     2
#2     2     3     3
#3     0  -Inf     1
max.col(replace(x <- abs(Filter(is.numeric, test_df)), is.na(x), -Inf))
# [1] 2 2 3
cbind(1:nrow(Filter(is.numeric, test_df)),
      max.col(replace(x <- abs(Filter(is.numeric, test_df)), is.na(x), -Inf)))
#     [,1] [,2]
#[1,]    1    2
#[2,]    2    2
#[3,]    3    3
test_df <- data.frame(
  some_identifier = c("apple", "tunafish", "turkey_sandwich"), 
  val_a =  c(-1, 2, 0), 
  val_b = c(-3, 3, NA), 
  val_c = c(2, 3, 1), stringsAsFactors = FALSE)