Problem: I want to add_row using dplyr/tibble. I want to group the data by A in my example, and then add_row that contains the group name A and then a value for B.
The problem I am facing is trying to add the Group_by variable A in the column under A. No matter what I try it always comes back with an error or NA as the value in that column.
Reproducible example:
example <- data.frame(A = sample(letters[1:3],10,replace = TRUE), B = sample(letters[24:26],10,replace = TRUE), C = sample(1:3,10,replace = TRUE))
Output of example data:
A B C 1 c y 2 2 b x 3 3 c y 1 4 b y 1 5 c z 1 6 a x 1 7 b x 1 8 c z 2 9 a y 3 10 c y 1
Code I want to run.
answer <- example %>% mutate(A = as.character(A), B = as.character(B)) %>% group_by(A) %>% do(add_row(., B = "ADDED", C = "ADDED"))
Output of the data:
A B C 1 a x 1 2 a y 3 3 <NA> ADDED ADDED 4 b x 3 5 b y 1 6 b x 1 7 <NA> ADDED ADDED 8 c y 2 9 c y 1 10 c z 1 11 c z 2 12 c y 1 13 <NA> ADDED ADDED
So in the output of the data, where there is NA I would like it to say the group name (a,b, or c)
I have tried putting just the group variable name there, it does not work throws an error.
Thanks!