Change selected data.frame columns using value in first row

Viewed 36

I have created a table that show how much time each person in a team has spend for tasks each month.

Empl_level  team_member 2022/05 2022/06 2022/07 2022/08
0             department  117       69      73      30
1             Diana       108       108     113     184
1             Irina       90        63      56      40
2             Inga        77        56      74      30
3             Elina       23        35      58      79

However there is such "team member" as department. how to to create a new dataset, where time from the sell department will be equally divided by real team members

Empl_level  team_member    2022/05      2022/06 
1             Diana       108+(117/4)   108+(69/4)      
1             Irina       90+(117/4)    63+(69/4)       
2             Inga        77+(117/4)    etc.
3             Elina       23+(117/4)        
1 Answers

Using data.table, something like the following could work:

library(data.table)
setDT(df)

df[,  names(df)[-(1:2)] := lapply(.SD, function(x)  {x + x[1]/4}), .SDcols = !1:2][-1]

The [-1] at the end removes the first "department" row.

Related