0

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)        
James Z
  • 12,209
  • 10
  • 24
  • 44
Диана
  • 103
  • 7

1 Answers1

0

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.

diomedesdata
  • 995
  • 1
  • 6
  • 15
  • Hi, thank for the reply, but when I try the code it returns me an error: mydata4[, lapply(.SD, function(x) {x + x[1]/13}), .SDcols = -1:2] Error in `[.data.table`(mydata4, , lapply(.SD, function(x) { : .SDcols is numeric but has both +ve and -ve indices > – Диана Sep 19 '22 at 08:39
  • @Диана I think I realise what was going wrong. Try the code now? – diomedesdata Sep 19 '22 at 08:40
  • In new version in returns me an error : Error in names(mydata4)[-1:2] : only 0's may be mixed with negative subscripts > – Диана Sep 19 '22 at 08:50
  • I will create a sample set ) – Диана Sep 19 '22 at 08:51
  • Whoops, forgot about that detail. Try now? With brackets, should work. – diomedesdata Sep 19 '22 at 08:52
  • Sorry to bother again... now I have transposed the table, as rows i have dates and as columns I have names. And now I have to do the same task of getting rid of "department" and additing the time from this table to other tables... How could I modify the code? – Диана Sep 19 '22 at 11:05
  • @Диана I would suggest maybe a new question, since rows and columns are not super interchangable in the code above. – diomedesdata Sep 19 '22 at 12:44