[英]What is most elegant way to loop through rows of a data frame with an impure function?
If I have the following piece of code:如果我有以下代码:
my_func <- function(var1, var2, var3, var4) {
... (side effect included)
}
df <- crossing(
nesting(var1=...,var2=....)
nesting(var3=...,var4=....)
)
What is the most elegant way to apply my_func over every single row of df?将 my_func 应用于 df 的每一行的最优雅的方法是什么? Plus my_func is not a pure function, it is designed to carry out some side effects (IO, plot...)
再加上 my_func 不是纯粹的 function,它被设计用来执行一些副作用(IO,plot ...)
Method 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)
Method 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
Method 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))
Is there a better way, which plays with %>%
well, to do this?有没有更好的方法,可以很好地处理
%>%
来做到这一点?
Honestly, I'd use purr's pmap::pmap老实说,我会使用 purr 的pmap::pmap
library(tidyverse)
df = data.frame(
x = rnorm(10),
y = runif(10)
)
df %>%
pmap_dbl(function(x, y) {
min(x,y)
})
I find the tidyverse offers to be still worse than plyr for many of these kind of operations.我发现对于许多此类操作, tidyverse的报价仍然比plyr差。 Example:
例子:
> library(plyr)
> library(tidyverse)
> #dummy function
> your_function = function(...) {do.call(args = list(..., sep = " + "), what = str_c)}
> alply(mpg[1:5, ], .margins = 1, .fun = function(row) {
+ your_function(row$manufacturer, row$cyl, row$trans)
+ }) %>% unlist()
1 2 3 4 5
"audi + 4 + auto(l5)" "audi + 4 + manual(m5)" "audi + 4 + manual(m6)" "audi + 4 + auto(av)" "audi + 6 + auto(l5)"
I don't know what you are trying to collect from the function, but probably you will want alply()
or adply()
.我不知道您要从 function 收集什么,但您可能需要
alply()
或adply()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.