使用Apply或Vectorize将自定义函数应用于数据框 [英] Using Apply or Vectorize to apply custom function to a dataframe

查看:123
本文介绍了使用Apply或Vectorize将自定义函数应用于数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试应用一个自定义函数,该函数调用该数据框的组件进行计算.我在下面做了一个琐碎的例子,因为我的实际问题很难做出可复制的例子.在下面的示例中,我希望将前两列加在一起以创建第三列,即它们的总和.下面是我在网上找到的一个接近我想要的示例:

I am attempting to apply a custom function that calls components of that dataframe to do a calculation. I have made a trivial example below because my actual problem is very hard to make a reproducible example. In the below example I want to have the first two columns be added together to create a third column which is the sum of them. Below is an example I found online that gets close to what I want:

celebrities=data.frame(name=c("Andrew","matt","Dany","Philip","John","bing","Monica"),
                       age=c(28,23,49,29,38,23,29),
                       income=c(25.2,10.5,11,21.9,44,11.5,45))
f=function(x,output){
  name=x[1]
  income=x[3]
  cat(name,income,"\n")
}
apply(celebrities,1,f)

但是当我尝试使用它并应用数学函数时,它不起作用:

But when I try to take it and apply mathematical function it doesn't work:

  f2=function(x,output){
  age=x[2]
  income=x[3]
  sum(age,income)
}
apply(celebrities,1,f2)

本质上,我需要申请一个数据集,使用该行中的值作为函数的输入遍历该数据集的每一行,并向该数据集添加第三列以及函数的结果.请让我知道如何在需要时澄清此问题.我已经提到了以下问题,但它们似乎对我没有帮助.

In essence what I need is for apply to take a dataset, go through every row of that dataset using the values in that row as inputs into the function and add a third column to the dataset with the results of the function. Please let me know how I can clarify this question if needed. I have referred to the questions below, but they don't seem to work for me.

应用功能到矩阵或数据帧的每一行

如何将lapply中的新值分配给列表中数据框的新列

致电在数据框的每一行上应用类似函数,每行具有多个参数

推荐答案

对于所请求的特定任务,可能是

For the particular task requested it could be

celebrities$newcol <- with(celebrities, age + income)

+ 函数本质上是矢量化的.将 apply sum 一起使用是无效的.通过省略第一列可以大大简化 apply 的使用,因为这样可以避免强制转换为由第一列引起的字符矩阵.

The + function is inherently vectorized. Using apply with sum is inefficient. Using apply could have been greatly simplified by omitting the first column because that would avoid the coercion to a character matrix caused by the first column.

 celebrities$newcol <- apply(celebrities[-1], function(x) sum(x) )

这样,您就可以避免将向量强制转换为字符",然后需要将之前的数字列强制转换回 numeric .在apply内部使用 sum 可以避免未对sum进行矢量化的事实,但这是R编码效率低下的一个示例.

That way you would avoid coercing the vectors to "character" and then needing to coerce back the formerly-numeric columns to numeric. Using sum inside apply does get around the fact that sum is not vectorized, but it's an example of inefficient R coding.

如果内部"算法可以完全由矢量化函数构造而成,则将获得自动矢量化:Math和Ops组是通常的组件.请参见?Ops .否则,您可能需要使用 mapply Vectorize .

You get automatic vectorization if the "inner" algorithm can be constructed completely from vectorized functions: the Math and Ops groups being the usual components. See ?Ops. Otherwise, you may need to use mapply or Vectorize.

这篇关于使用Apply或Vectorize将自定义函数应用于数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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