I am trying to sum values from the same column across multiple rows in R, but each row falls into 1 or 2 of the desired output rows, so I have struggled to use ddply
or tapply
successfully.
I have triangular transect data, where sample points were taken at each vertex (points 1, 3 and 5) and half-way along each edge (points 2, 4 and 6). I am trying to summarise the data collected along each side of the triangle: i.e. leg A is the sum of points 1 + 2 + 3; leg B is the sum of points 3 + 4 + 5; leg C is the sum of points 5 + 6 + 1.
My data is in the form:
Transect <- c(rep("T001",6),rep("T002",6),rep("T003",6))
Point <- rep(seq(1,6,1),3)
Area <- c(rep(3000, 8), 2500, 2000, rep(3000,4), 1000, rep(3000,3))
df <- data.frame(Transect, Point, Area)
The desired output would be:
Transect2 <- c(rep("T001",3),rep("T002",3),rep("T003",3))
Leg <- rep(c("A", "B", "C"),3)
Total.Area <- c(rep(9000,3), 8500, 7500, 9000, 7000, 7000, 9000)
df.out <- data.frame(Transect2, Leg, Total.Area)
Thanks in advance for your help, and apologies if the question title is poorly worded, I'm not sure how to accurately describe this problem!