Nicholas Hayden
Nicholas Hayden

Reputation: 523

Subtracting numbers within a data.frame based on a column - R

I am novice R programmer. I have this dummy data.frame.

a b group
1 4 1
5 5 1
7 8 1
10 9 1
14 0 2
18 4 2
20 5 2
25 7 2
29 10 3
35 15 3
40 22 3
48 44 3
57 34 4
68 65 4
77 8 4
86 55 4

I would like to produce the differences between groups 1 and 2, 2 and 3, 3 and 4 for columns a and b.

I think I am doing it very mechanically.

Thank you for your time

Upvotes: 2

Views: 1098

Answers (2)

user3603486
user3603486

Reputation:

This looks like panel data. You could try the plm package.

mydata$index <- rep(1:4, 3) # or however many unique groups you have
mydata <- pdata.frame(mydata, c('index', 'group'))
diff_a <- diff(mydata$a)

Upvotes: 1

akrun
akrun

Reputation: 887148

Here, the OP mentioned the 'group's have equal number of elements, So, one option is to remove the last 4 elements (head(df2, -4)[-3]) and first 4 elements (tail(df2, -4)[-3]) so that we will get the difference for columns 'a', 'b' between groups 1-2, 2-3, etc. as the 'group's are in order. For classification purpose, cbind with a 'group' vector created by pasteing the unique elements of 'group' (by removing the 1st and last element)

un1 <- unique(df2$group)
cbind(group = rep(paste(un1[-length(un1)], un1[-1], sep="-"), each = 4), 
      head(df2,-4)[-3]- tail(df2,-4)[-3])
#    group   a   b
#1    1-2 -13   4
#2    1-2 -13   1
#3    1-2 -13   3
#4    1-2 -15   2
#5    2-3 -15 -10
#6    2-3 -17 -11
#7    2-3 -20 -17
#8    2-3 -23 -37
#9    3-4 -28 -24
#10   3-4 -33 -50
#11   3-4 -37  14
#12   3-4 -38 -11

Upvotes: 1

Related Questions