我需要计算两组之间的“值”变量(“ spe2”变量)之差。 这样新变量“ diff”就是B-A和D-C。我怎样才能做到这一点?下面举一个例子。
date <- c(2010,2011,2012,2013)
value <- c(1,21,32,46)
spe1 <- c("A","B","C","D")
spe2 <- c(1,1,2,2)
df1 <- data.frame(date, value, spe1, spe2)
df1
#expected result
date <- c(2010,2011,2012,2013)
value <- c(1,21,32,46)
spe1 <- c("A","B","C","D")
spe2 <- c(1,1,2,2)
diff <- c(20,20,14,14)
df2 <- data.frame(date, value, spe1, spe2, diff)
df2