在每行数据帧上调用类似应用程序的函数,每行有多个参数 [英] Call apply-like function on each row of dataframe with multiple arguments from each row

查看:169
本文介绍了在每行数据帧上调用类似应用程序的函数,每行有多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个列的数据框。对于数据帧中的每一行,我想调用该行上的一个函数,函数的输入是使用该行的多个列。例如,假设我有这个数据,这个testFunc接受两个参数:

 > 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< - 函数(a,b)a + b

到x和z列。所以,对于行1,我想要1 + 5,对于行2,我想要2 + 6。有没有办法做这个没有写一个for循环,也许与应用功能的家庭?



我尝试过:

 > df [,c('x','z')] 
x z
1 1 5
2 2 6
> lapply(df [,c('x','z')],testFunc)
+ b中的错误:'b'缺少

但是有错误,任何想法?



编辑:我想要的实际功能电话不是一个简单的总和,但它是power.t.test。我用一个+ b为例。最终目标是能够做这样的事情(用伪代码写成):

  df = data.frame(
delta = c(delta_values),
power = c(power_values),
sig.level = c(sig.level_values)


lapply ,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的输出向量。

解决方案

p>您可以将 apply 应用于原始数据的一部分。

  dat <,data()(x = c(1,2),y = c(3,4),z = c(5,6))
apply ,'z')],1,function(x)sum(x))

函数只是使用向量化版本的总和:

  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]))

编辑要按名称访问列,而不是索引,您可以执行以下操作:

  testFunc<  -  function(a,b)a + b 
apply(dat [,c('x','z ')],1,function(y)testFunc(y ['z'],y ['x']))


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

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?

I tried this:

> 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?

EDIT: 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
))

where the result is a vector of outputs for power.t.test for each row of df.

解决方案

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

If you want to use testFunc

 testFunc <- function(a, b) a + b
 apply(dat[,c('x','z')], 1, function(x) testFunc(x[1],x[2]))

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']))

这篇关于在每行数据帧上调用类似应用程序的函数,每行有多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆