'How to export data frame rows based on group data with specific formatting in R

I have a data frame that includes all employee data (1000 employees, nested in 50 groups). For each group, I want R to export each group data participants in a seperate excel file (or docx if possible). The formatting should be as follows: 1- The first rows should have the group informtino (i.e. id_group, country, region, section), then 2- the other rows should have the participants daa (i.e. sequesnce, EmployeeName, id_emplyee)

The results should include 50 different files, each file includes all employees data including their group information (attached is an example of each file).

# Here is an example of the full data frame:

EmployeeName =  randomNames::randomNames(1000, ethnicity = 5 ,which.names = "last") 
id_emplyee =  sample(1:1000, 1000, replace = F)
id_group =  sample(1:50, 1000, replace = T)

cou = c("USA", "UK", "AUS", "ARM", "BEL")
country = sample(cou, 1000, replace = T)
region_ = c("n", "e", "w", "s", "m")
region = sample(region_, 1000, replace = T)

sect = c("1", "2", "3")
section = sample(sect, 1000, replace = T)

df = data.frame(EmployeeName, id_emplyee, id_group, country,
                       region, section)

enter image description here

r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source