如果我有以下代码:
my_func <- function(var1, var2, var3, var4) {
... (side effect included)
}
df <- crossing(
nesting(var1=...,var2=....)
nesting(var3=...,var4=....)
)
在每一行df上应用my_func最优雅的方法是什么? 另外my_func不是纯函数,它设计用于执行一些副作用(IO,plot ...)
方法1
my_func_wrapper <- function(row) {
my_func(row['var1'], row['var2'], row['var3'], row['var4'])
}
# Vector coercion is a problem, if variables are not the same type.
apply(df, 1, my_func_wrapper)
方法2
df %>%
rowwise() %>%
do(result=invoke(my_func, .)) %>% #If it ends here, I will be pretty happy.
.$result # Relying auto print feature to plot or trigger some side effect
方法3
#This looks pretty good on its own but it does not play well with the pipe %>%
foreach(row=iter(df, by='row')) %do% invoke(my_func, row)
#Method 3.1 (With Pipe)
df %>%
(function(df) foreach(row=iter(df, by='row')) %do% invoke(my_func, row))
#Method 3.2 this does not work
# df %>%
# foreach(row=iter(., by='row')) %do% invoke(my_func, row)
#Method 3.3 this does not work
#I am trying to get this work with purrr's simplified anonymous function, but it does not work.
# df %>%
# as_function(~ foreach(row=iter(., by='row')) %do% invoke(my_func, row))
有没有更好的方法,与%>%
一起玩,这样做?
答案 0 :(得分:1)
老实说,我会使用purr的pmap::pmap
library(tidyverse)
df = data.frame(
x = rnorm(10),
y = runif(10)
)
df %>%
pmap_dbl(function(x, y) {
min(x,y)
})