Try this in the R database:
aggregate(Diff~Group,
with(df, data.frame(Group=Group, Diff=ifelse(Type=="X", 1, -1)*Income)), sum)
<strong> data
df <- structure(list(Group = structure(c(1L, 1L, 2L, 2L, 3L, 4L), .Label = c("A",
"B", "C", "D"), class = "factor"), Type = structure(c(1L, 2L,
2L, 1L, 1L, 2L), .Label = c("X", "Y"), class = "factor"), Income = c(1000L,
500L, 2000L, 1500L, 700L, 600L)), .Names = c("Group", "Type",
"Income"), class = "data.frame", row.names = c(NA, -6L))