Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6691879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:48:11+00:00 2026-05-26T05:48:11+00:00

Super short version: I’m trying to use a user-defined function to populate a new

  • 0

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:

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

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:48:11+00:00Added an answer on May 26, 2026 at 5:48 am

    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 EmployeeLocationNumber does not match your description.

    > EmployeeLocationNumber(8)
    [1] 3
    

    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)
    

    which gives

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version: In trying to deal with a legacy database in Ruby-on-Rails, I've come
Super-short version: I solved this problem when I was nearly finished writing the question.
I have a super pom defined in which I specify a "resources" directory for
I came across PECS (short for Producer extends and Consumer super ) while reading
this and super are keywords aren't they; then how can I use them for
Short version: Is there a simple way to get the value of a primary
Long version... A co-worker asserted today after seeing my use of while (1) in
I am trying to create a simple dialog box to allow my user to
In short, the following code calls an existing selector in the super class, and
To make a long story short, I'm trying to pass a list of dictionaries

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.