In order to prepare the data for plotting I need to add a new row to the data:
I have this dataframe:
df <- data.frame(
test_id = c(1, 1, 1, 1),
test_nr = c(1, 1, 1, 1),
region = c("A", "B", "C", "D"),
test_value = c(3, 1, 1, 2)
)
test_id test_nr region test_value
1 1 1 A 3
2 1 1 B 1
3 1 1 C 1
4 1 1 D 2
I would like to add a row to this dataframe so the desired output should be:
test_id test_nr region test_value
1 1 1 A 3.00
2 1 1 B 1.00
3 1 1 C 1.00
4 1 1 D 2.00
5 1 1 mean 1.75
As you can see: column 1 and column 2 are the same value, column 3 changed to 'mean' and column 4 is the mean of row1-4.
I have tried to use add_row
from tibble
package which gives an error:
library(dpylr)
library(tibble)
df %>%
mutate(mean1 = mean(test_value)) %>%
add_row(test_id = test_id[1], test_nr=test_nr[1],region="mean", test_value=mean(test_value))
Error in eval_tidy(xs[[j]], mask) : object 'test_id' not found