问题描述
我有一个包含多列的数据框.对于数据框中的每一行,我想在该行上调用一个函数,并且该函数的输入使用该行中的多列.例如,假设我有这个数据和这个接受两个参数的 testFunc:
I have a dataframe with multiple columns. For each row in the dataframe, I want to call a function on the row, and the input of the function is using multiple columns from that row. For example, let's say I have this data and this testFunc which accepts two args:
> df <- data.frame(x=c(1,2), y=c(3,4), z=c(5,6))
> df
x y z
1 1 3 5
2 2 4 6
> testFunc <- function(a, b) a + b
假设我想将此 testFunc 应用于 x 和 z 列.因此,对于第 1 行,我想要 1+5,对于第 2 行,我想要 2 + 6.有没有办法在不编写 for 循环的情况下做到这一点,也许使用 apply 函数系列?
Let's say I want to apply this testFunc to columns x and z. So, for row 1 I want 1+5, and for row 2 I want 2 + 6. Is there a way to do this without writing a for loop, maybe with the apply function family?
我试过这个:
> df[,c('x','z')]
x z
1 1 5
2 2 6
> lapply(df[,c('x','z')], testFunc)
Error in a + b : 'b' is missing
但是出错了,有什么想法吗?
But got error, any ideas?
我要调用的实际函数不是简单的总和,而是 power.t.test.我使用 a+b 只是为了举例.最终目标是能够做这样的事情(用伪代码编写):
the actual function I want to call is not a simple sum, but it is power.t.test. I used a+b just for example purposes. The end goal is to be able to do something like this (written in pseudocode):
df = data.frame(
delta=c(delta_values),
power=c(power_values),
sig.level=c(sig.level_values)
)
lapply(df, power.t.test(delta_from_each_row_of_df,
power_from_each_row_of_df,
sig.level_from_each_row_of_df
))
其中结果是每行 df 的 power.t.test 输出向量.
where the result is a vector of outputs for power.t.test for each row of df.
推荐答案
您可以将 apply
应用于原始数据的子集.
You can apply apply
to a subset of the original data.
dat <- data.frame(x=c(1,2), y=c(3,4), z=c(5,6))
apply(dat[,c('x','z')], 1, function(x) sum(x) )
或者如果您的函数只是求和,请使用矢量化版本:
or if your function is just sum use the vectorized version:
rowSums(dat[,c('x','z')])
[1] 6 8
如果你想使用 testFunc
testFunc <- function(a, b) a + b
apply(dat[,c('x','z')], 1, function(x) testFunc(x[1],x[2]))
EDIT 要按名称而不是索引访问列,您可以执行以下操作:
EDIT To access columns by name and not index you can do something like this:
testFunc <- function(a, b) a + b
apply(dat[,c('x','z')], 1, function(y) testFunc(y['z'],y['x']))
这篇关于使用来自每一行的多个参数在数据帧的每一行上调用类似应用的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!