I have this sample data:
cvar <- c(\"2015-11-01\",\"2015-11-02\",\"All\")
nvar1 <- c(12,10,5)
nvar2 <- c(7,5,6)
data <- cbind.data.frame(cvar,nvar1,n
Something like this then maybe:
data %>%
rbind(c("add",sum(nvar1),sum(nvar2)))
# cvar nvar1 nvar2
#1 2015-11-01 12 7
#2 2015-11-02 10 5
#3 All 5 6
#4 add 27 18
Edit:
According to your comment, this will work:
data %>%
mutate(nvar3 = nvar1) %>%
rbind(c("add",sum(nvar1),sum(nvar2),sum(.$nvar3)))
Using the .
will allow rbind
to find nvar3
Edit2:
Provide the new row as a list and it will maintain the column classes:
> str(
+ data %>%
+ mutate(nvar3 = nvar1) %>%
+ rbind(list("add",sum(nvar1),sum(nvar2),sum(.$nvar3)))
+ )
'data.frame': 4 obs. of 4 variables:
$ cvar : chr "2015-11-01" "2015-11-02" "All" "add"
$ nvar1: num 12 10 5 27
$ nvar2: num 7 5 6 18
$ nvar3: num 12 10 5 27