jeffgoblue
jeffgoblue

Reputation: 319

In R, calculate a new column based on difference between rows

Consider the following subset of my data field:

  Pack  side row col     v1      v2
1   P1  Left   1   1 0.4094 -3.8700
2   P1 Right   1   1 0.4110 -3.5245
3   P1  Left   1   2 0.4118 -3.4876
4   P1 Right   1   2 0.4108 -3.7268
5   P1  Left   1   3 0.4119 -3.5322
6   P1 Right   1   3 0.4110 -3.6101

I'm interested in the difference between left and right for v1 and v2 respectively, specifically the % difference for v1 and straight difference for v2.

My desired output is a new data field that looks like this:

 Pack   row col dv1              dv2
1 P1    1   1   0.389294404      0.3455
2 P1    1   2   -0.243427459    -0.2392
3 P1    1   3   -0.218978102    -0.0779

where the calculation for dv1 is (Right-Left)/Left*100 for v1, and the calc for dv2 is Right-Left of v2.

Here's df data:

df <- structure(list(Pack = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("P1", 
"P2", "P3", "P4"), class = "factor"), side = structure(c(1L, 
2L, 1L, 2L, 1L, 2L), .Label = c("Left", "Right"), class = "factor"), 
    row = c(1L, 1L, 1L, 1L, 1L, 1L), col = c(1L, 1L, 2L, 2L, 
    3L, 3L), v1 = c(0.4094, 0.411, 0.4118, 0.4108, 0.4119, 0.411
    ), v2 = c(-3.87, -3.5245, -3.4876, -3.7268, -3.5322, -3.6101
    )), .Names = c("Pack", "side", "row", "col", "v1", "v2"), row.names = c(NA, 
6L), class = "data.frame")

Thanks!

Upvotes: 2

Views: 984

Answers (2)

NColl
NColl

Reputation: 757

Another dplyr approach using lead and mutate

library(tidyverse)
df2 <- df %>%
mutate(lead_v1 = lead(v1), lead_v2 = lead(v2), dv1 = (lead_v1-v1)/v1*100, dv2 = lead_v2-v2) %>%
select(c(1,3,4,9,10)) %>%
filter(row_number() %% 2 != 0)

> df2
   Pack row col        dv1     dv2
1   P1   1   1  0.3908158  0.3455
2   P1   1   2 -0.2428363 -0.2392
3   P1   1   3 -0.2184996 -0.0779
> 

EDIT - Changed filter to remove even row numbers

Upvotes: 1

Julius Vainora
Julius Vainora

Reputation: 48191

We may first sort the rows by side and be sure that first we will have Left and then Right. This gives

library(tidyverse)
df %>% arrange(side) %>% group_by(Pack, row, col) %>% 
  summarise(dv1 = (v1[2] - v1[1]) / v1[1] * 100, dv2 = v2[2] - v2[1])
# A tibble: 3 x 5
# Groups:   Pack, row [?]
#   Pack    row   col    dv1     dv2
#   <fct> <int> <int>  <dbl>   <dbl>
# 1 P1        1     1  0.391  0.345 
# 2 P1        1     2 -0.243 -0.239 
# 3 P1        1     3 -0.218 -0.0779

or just

df %>% arrange(side) %>% group_by(Pack, row, col) %>% 
  summarise(dv1 = diff(v1) / v1[1] * 100, dv2 = diff(v2))

Upvotes: 2

Related Questions