I have a tibble with info about users (ID) that use different workstations (WS) to create documents in a certain domain (DM), documents have a certain error rate (ER), and then there is more info (as a made up example PP):
df <- tibble("ID" = c("A","B","A","B","A","B","A", "C", "B","A","B","C"),
"WS" = c("a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b"),
"DM" = c("r", "r", "p", "p", "r", "r", "p", "p", "r", "r", "p", "p"),
"ER" = c(92.91307, 84.93493, 86.46488, 90.83528, 94.30230, 92.85309,
81.14628, 81.33754, 93.22315, 96.83421, 85.76104, 93.71726 ),
"PP" = c(6, 4, 6, 0, 0, 0, 2, 5, 7, 2, 7, 6))
I need to know how many docs were created by each user on each workstation (numDocs_ID_WS), and what the error rate (avg) was:
df_mutA <- df %>%
group_by(ID, WS) %>%
mutate(numDocs_ID_WS = n(),
avg = mean(ER))
df_mutA
Now where I struggle is to get the number of different workstations (numWS) each user was using. I clearly get the correct number when using summarize():
df_sumB <- df %>%
group_by(ID, WS) %>%
summarize(numDocs_ID_WS = n(),
avg = mean(ER)) %>%
mutate(numWS = length(ID))
df_sumB
But with that, I lose additional columns like PP. Using mutate() on the other hand instead of summarize() gives the wrong result; when running
df_mutB <- df %>%
group_by(ID, WS) %>%
mutate(numDocs_ID_WS = n(),
avg = mean(ER)) %>%
mutate(numWS = length(ID))
df_mutB
the results for columns numDocs_ID_WS and numWS show identical results.
I understand that one way to solve this item would be to create a summary table and use join to get the correct final result; this seems not elegant to me, especially in the light of having to deal with big tables (thousands of rows x hundreds of columns). Did I miss anything? Is there a way to create this column in just one step (I would have hoped grouping gets me closer, but I couldn't find the right way)?
Thank you very much for your ideas and help, it is very much appreciated! Best regards, Saschik