尝试使用用户定义的函数在数据框中填充新列。怎么了? [英] Trying to use user-defined function to populate new column in dataframe. What is going wrong?

查看:50
本文介绍了尝试使用用户定义的函数在数据框中填充新列。怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

超短版本:我正在尝试使用用户定义的函数通过以下命令在数据框中填充新列:

Super short version: I'm trying to use a user-defined function to populate a new column in a dataframe with the command:

TestDF$ELN<-EmployeeLocationNumber(TestDF$Location)

但是,当我运行命令,似乎只是将EmployeeLocationNumber应用于第一行的Location值,而不是使用每一行的值来单独确定该行的新列的值。

However, when I run the command, it seems to just apply EmployeeLocationNumber to the first row's value of Location rather than using each row's value to determine the new column's value for that row individually.

请注意:我试图理解R,而不仅仅是执行此特定任务。实际上,我可以使用Apply()函数获得所需的输出,但这无关紧要。我的理解是,以上一行应该逐行工作,但事实并非如此。

Please note: I'm trying to understand R, not just perform this particular task. I was actually able to get the output I was looking for using the Apply() function, but that's irrelevant. My understanding is that the above line should work on a row-by-row basis, but it isn't.

以下是测试的详细信息:

Here are the specifics for testing:

TestDF<-data.frame(Employee=c(1,1,1,1,2,2,3,3,3), 
                   Month=c(1,5,6,11,4,10,1,5,10), 
                   Location=c(1,5,6,7,10,3,4,2,8))

此testDF跟踪3名员工在一年中的每一个位置

This testDF keeps track of where each of 3 employees was over the course of the year among several locations.

(您可以将位置视为每个员工的唯一信息,因此该行实际上是该行的唯一ID。)

(You can think of "Location" as unique to each Employee...it is eseentially a unique ID for that row.)

函数EmployeeLocationNumber获取一个位置并输出一个数字,该数字指示员工访问该位置的顺序。例如 EmployeeLocationNumber(8)= 2 ,因为它是拜访它的员工访问的第二个位置。

The the function EmployeeLocationNumber takes a location and outputs a number indicating the order that employee visited that location. For example EmployeeLocationNumber(8) = 2 because it was the second location visited by the employee who visited it.

EmployeeLocationNumber <- function(Site){
  CurrentEmployee <- subset(TestDF,Location==Site,select=Employee, drop = TRUE)[[1]]
  LocationDate<- subset(TestDF,Location==Site,select=Month, drop = TRUE)[[1]]
  LocationNumber <- length(subset(TestDF,Employee==CurrentEmployee & Month<=LocationDate,select=Month)[[1]])
  return(LocationNumber)
}

我意识到我可能可以将所有这些打包到一个子命令中,但是当您在其他子命令中使用子命令时,我不知道引用是如何工作的。

I realize I probably could have packed all of that into a single subset command, but I didn't know how referencing worked when you used subset commands inside other subset commands.

因此,请记住,我真的想了解如何在R中工作,我有几个问题:

So, keeping in mind that I'm really trying to understand how to work in R, I have a few questions:


  1. 为什么不 TestDF $ ELN< -EmployeeLocationNumber(TestDF $ Location)逐行工作像其他赋值语句一样?

  1. Why won't TestDF$ELN<-EmployeeLocationNumber(TestDF$Location) work row-by-row like other assignment statements do?

是否有更简单的方法可以基于另一个值来引用数据框中的特定值?也许没有返回一个数据帧/列表,然后必须将其展平并从中提取?

Is there an easier way to reference a particular value in a dataframe based on the value of another one? Perhaps one that does not return a dataframe/list that then must be flattened and extracted from?

我确定我使用的函数是可笑的un类似于-R ...我应该做些什么来本质上模拟INNER Join类型查询?

I'm sure the function I'm using is laughably un-R-like...what should I have done to essentially emulate an INNER Join type query?


推荐答案

R的矢量化性质(即逐行)不是通过用参数的每个下一个值重复调用该函数,而是一次传递整个向量并对所有向量进行操作一次。但是在 EmployeeLocationNumber 中,您只返回一个值,因此该值将在整个数据集中重复。

The vectorized nature of R (aka row-by-row) works not by repeatedly calling the function with each next value of the arguments, but by passing the entire vector at once and operating on all of it at one time. But in EmployeeLocationNumber, you only return a single value, so that value gets repeated for the entire data set.

另外,您的 EmployeeLocationNumber 示例与您的描述不匹配。

Also, your example for EmployeeLocationNumber does not match your description.

> EmployeeLocationNumber(8)
[1] 3

现在,一种将函数向量化的方法您的思维方式(重复调用每个值)是通过 Vectorize()

Now, one way to vectorize a function in the manner you are thinking (repeated calls for each value) is to pass it through Vectorize()

TestDF$ELN<-Vectorize(EmployeeLocationNumber)(TestDF$Location)

> TestDF
  Employee Month Location ELN
1        1     1        1   1
2        1     5        5   2
3        1     6        6   3
4        1    11        7   4
5        2     4       10   1
6        2    10        3   2
7        3     1        4   1
8        3     5        2   2
9        3    10        8   3

关于您的其他问题,我将其写为

As to your other questions, I would just write it as

TestDF$ELN<-ave(TestDF$Month, TestDF$Employee, FUN=rank)

逻辑是计算月份,按员工分别查看月份的组,然后给我月份的排名顺序(按顺序排列)。

The logic is take the months, looking at groups of the months by employee separately, and give me the rank order of the months (where they fall in order).

这篇关于尝试使用用户定义的函数在数据框中填充新列。怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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