简体   繁体   English

使用不纯的 function 遍历数据帧的行的最优雅的方法是什么?

[英]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.

相关问题 将 function 应用于 data.table 或 data.frame 中的多对列的最优雅方法是什么? - What is the most elegant way to apply a function to multiple pairs of columns in a data.table or data.frame? 遍历数据帧中每个观察结果的最有效方法 - Most efficient way to loop through each observation in a data frame 查找具有所有唯一值的data.frame第一列的最优雅方法是什么? - what is the most elegant way to find the first column of a data.frame that has all unique values? 将几个空行添加到 R 中的数据框中的最优雅方法? - Most elegant ways to add a few empty rows into a data frame in R? 分割数据和生成季节性箱形图的最优雅方法是什么? - What is the most elegant way to split data and produce seasonal boxplots? 检查 R 中缺失数据模式的最优雅方法是什么? - what is the most elegant way to check for patterns of missing data in R? 将 R 中的 data.frame 与 while 循环生成的数据放在一起的最有效方法是什么? - What is the most efficient way to put together a data.frame in R with data generated by a while loop? 将大型数据帧的行作为 arguments 传递到 R 中的 function 的最快和最有效的方法 - Fastest and most efficient way to pass rows of a large data.frame as arguments to a function in R R:在将所有元素粘贴到单个字符串之前,最优雅的方法来清理数据框 - R: Most elegant way to sanitize data frame before pasting all elements to single string 通过sapply或lapply函数而不是R中的for循环向下复制多个数据帧行 - Copy multiple data frame rows down through sapply or lapply function instead of for loop in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM