# In R: How to subtract/divide rows in same data frame

I have a data frame, let's say it's called my.data

1 Item A B C

2 AAE 7 1 37

3 CCC 7 0 38

4 PPI 10 1 35

I want to create a row below "AAE" called "AAA" which is "AAE" - "CCC" - "PPI", so for example, for column A we have 7-7-10 = -10, for column B we have 1-0-1 = 0, etc. To clarify, my data set is considerably bigger, and "CCC" and "PPI" are not directly under "AAE"

How can I do this?

Another option is to firstly transpose the data.frame, do normal mutation and then transpose it back.

library(dplyr)
`row.names<-`(df[,-1], df[,1]) %>% t %>%
transform(AAA = AAE - CCC - PPI) %>%
t %>% data.frame

A B   C
AAE   7 1  37
CCC   7 0  38
PPI  10 1  35
AAA -10 0 -36

I can get most of the way there using Reduce to recursively apply -

newrow <- c(Item="AAA", lapply(dat[dat\$Item %in% c("AAE","CCC","PPI"),-1], Reduce, f=`-`))
rbind(dat[1,], newrow, dat[-1,])

#   Item   A B   C
#2   AAE   7 1  37
#21  AAA -10 0 -36
#3   CCC   7 0  38
#4   PPI  10 1  35
apply(my.data[,c("A","B","C")],2,function(x)Reduce("-",x))
A   B   C
-10   0 -36