Please see sample dataset below.
mydata = data.frame (type =c ("fixed A", "fixed B", "fixed C", "fixed D", "fixed E", "variable A", "variable B", "variable C", "variable D", "variable E"),
cost =c(234,34,53,256,45,345,24,26,78,56))
Im trying to do the following:
For number 1, I have used the following code:
mydata <- mydata %>%
adorn_totals("row") %>%
mutate(across(where(is.numeric),(abs)))
For number 2, I tried to use the following code to filter out the "Variable" strings, create a sum row and then merge it back to the original "mydata" dataset, but keep getting the following error:
x <- mydata %>%
filter(grepl("variable",type))%>%
adorn_totals("row")
error: Error in adorn_totals(., "row") : trying to re-add a totals dimension that is already been added
I would ideally like the following output:
type cost
fixed A 234
fixed B 34
fixed C 53
fixed D 256
fixed E 45
TOTAL 1151
variable A 345
variable B 24
variable C 26
variable D 78
variable E 56
Total Variable 529
Any help would be really appreciated - thank you!
The following seems to work. I don't love the idea of creating multiple different datasets, but this seems to solve the error I was getting (Error in adorn_totals(., "row", name = "Total Variable") : trying to re-add a totals dimension that is already been added)
x <- mydata %>%
adorn_totals("row") %>%
mutate(across(where(is.numeric),(abs))) %>%
filter(grepl("fixed|Total",type))
x1 <- mydata %>%
filter(grepl("variable",type))%>%
adorn_totals("row", name = "Total Variable")
res <- rbind(x,x1)