简体   繁体   中英

How to loop through data frame columns with lists as a result in R?

I need to be able to extract values from the respective columns of a data frame and insert these into a specific position within a function/loop.

My data frame: it's a df of "new starters" (new employees)

id <- c(115, 115)
f_name <- c("John", "Mary")
l_name <- c("Black", "White")
gender <- c("Male", "Female")
s_date <- c("2021-03-01", "2021-03-01")
ns_df <- data.frame(id, f_name, l_name, gender, s_date)

This gives the following:

   id f_name l_name gender     s_date
1 115   John  Black   Male 2021-03-01
2 115   Mary  White Female 2021-03-01

I then want to take each of the column values and push them into their respective place within a POST request which looks like this:

POST(url = myurl, config = authenticate(user = login,
                                        password = pw,
                                        type = "basic"),
     body = list(APIKey = my_key, # keep the same
                 Action = "CreateNewEmployee", # keep the same
                 EmployeeId = "id", # loop through ids
                 FirstName = "f_name", # loop through first names
                 LastName = "l_name", # look through last names
                 Gender = "gender", # loop through genders
                 StartDate = "s_date" #loop through start dates
       
     ),
     encode = "json")

The desired outcome is the following:

  1. A data frame with new starters is created (can be 0 rows in which case no action follows). If it's more than 0 rows then proceed
  2. Depending on how many rows, a loop is initiated to iterate through each row and extract the attributes so that each goes into the respective part of the list which makes up the body of the POST request
  3. A POST request is sent for each row in the new starters data frame

I know I need to utilise an apply function or a loop that would run through the data frame and prepare the list(s)/body of the POST request but I am not exactly sure how to piece it all together, Any help will be appreciated. thank you all.

You can put the code in a function and apply it for each row using any of the apply command.

lapply(seq(nrow(ns_df)), function(i) {
  
  POST(url = myurl, config = authenticate(user = login,
                                          password = pw,
                                          type = "basic"),
       body = list(APIKey = my_key, # keep the same
                   Action = "CreateNewEmployee", # keep the same
                   EmployeeId = ns_df[i, "id"], # loop through ids
                   FirstName = ns_df[i, "f_name"], # loop through first names
                   LastName = ns_df[i, "l_name"], # look through last names
                   Gender = ns_df[i, "gender"], # loop through genders
                   StartDate = ns_df[i, "s_date"] #loop through start dates
                   
       ), encode = "json")
}) -> result

result

If the output of each POST request is a dataframe you may need do.call(rbind, result) to combine the results in one dataframe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM