简体   繁体   English

将数据框的行作为参数传递给 R 中的函数,其中列名指定参数

[英]Pass rows of a data frame as arguments to a function in R with column names specifying the arguments

I want to use the rows of a dataframe as arguments for a function.我想使用数据帧的行作为函数的参数。 I know this can be done like this:我知道这可以这样做:

 arg <- expand.grid(n = 100, mean = -1:1, sd = 0:3)

 apply(arg, 1, FUN = function(x) rnorm(x[1], x[2], x[3]))

I would like, however, to have a more general, elegant way to pass the arguments using the column names of a dataframe to specify them (if the colum names is not an option, the order of the columns will do it as well).但是,我希望有一种更通用、更优雅的方式来使用数据框的列名传递参数来指定它们(如果列名不是一个选项,列的顺序也可以)。

I don't want to specify in every function which column refers to each argument, as I have done in the example above.我不想在每个函数中指定哪个列引用每个参数,就像我在上面的例子中所做的那样。 Also, I would like to pass the values to functions with arbitrary number of arguments.另外,我想将值传递给具有任意数量参数的函数。

我们可以使用Map

do.call(Map, c(f= rnorm, arg))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:调用 function,data.frame 列名称为 arguments - R: call function with data.frame column names as arguments 在 R 中使用函数指定参数名称 - Specifying names of arguments with a function in R 将列名作为函数参数传递-R - Pass column names as function arguments - R 将大型数据帧的行作为 arguments 传递到 R 中的 function 的最快和最有效的方法 - Fastest and most efficient way to pass rows of a large data.frame as arguments to a function in R 计算一个 function,其 arguments 存储在 R 中数据帧的行中 - Compute a function whose arguments stored in rows of a data frame in R R使用值作为参数将函数应用于数据框的行 - R apply function to rows of data frame using values as arguments data.frame(..., check.names = FALSE) 中的错误:参数暗示 r 中 plot_model 函数中的行数不同 - Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows in plot_model function in r 在公式中将列名称传递为 function arguments - Pass column names as function arguments in formula 将数据框列作为参数传递给mutate函数 - Pass data frame columns as arguments to mutate function R data.frame(...,check.names = FALSE)中的错误:参数暗示行数不同:5、3 - R Error in data.frame(…, check.names = FALSE) : arguments imply differing number of rows: 5, 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM