在整个行上逐行使用purrr而不是apply()

Hum*_*hen 5 r apply purrr

我想apply()用一个purrr函数替换(及其派生)。

我有一个像这样的data.frame:

> df
  V1 V2 V3
1 NA  2  3
2  2 NA  3
3  3  3 NA
Run Code Online (Sandbox Code Playgroud)

我想逐行应用两个函数:min(x, na.rm = T)和,which.min(x)然后将结果作为数据帧返回。

如果我知道有多少列,我可以这样做:

pmap_dfr(df, function(V1, V2, V3) {data.frame(min = pmin(V1, V2, V3, na.rm = T),
                                              where = which.min(c(V1, V2, V3)))})

  min where
1   2     2
2   2     1
3   3     1
Run Code Online (Sandbox Code Playgroud)

我该如何使pmap()或其他任何purrr函数像将整行一样作为参数apply()

func <- function(x) {data.frame(min = min(x, na.rm = T), where = which.min(x))}

> Reduce(rbind, apply(df,1, func))
    min where
V2    2     2
V1    2     1
V11   3     1


Run Code Online (Sandbox Code Playgroud)

我可能只是错过了功能或技巧。谢谢你的帮助。

H 1*_*H 1 3

如果您使用省略号,您的解决方案将适用于所有列。

pmap_dfr(df, ~data.frame(min = min(..., na.rm = TRUE), where = which.min(c(...)))) 

  min where
1   2     2
2   2     1
3   3     1
Run Code Online (Sandbox Code Playgroud)