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)
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.
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))
This testDF keeps track of where each of 3 employees was over the course of the year among several locations.
(You can think of “Location” as unique to each Employee…it is eseentially a unique ID for that row.)
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.
So, keeping in mind that I’m really trying to understand how to work in R, I have a few questions:
-
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?
-
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?
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.Also, your example for
EmployeeLocationNumberdoes not match your description.Now, one way to vectorize a function in the manner you are thinking (repeated calls for each value) is to pass it through
Vectorize()which gives
As to your other questions, I would just write it as
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).