0

I am trying to find difference in the first and last value of for a set of values in a given column in R. The example data table is

V1 V2
A  2
B  4
A  3
C  1
D  2
B  4
A  5

The output should take the first and last occurrence and give the result

V1 V2
A  3
B  0
C  1
D  2 

I am confused as to which function to use to get to the result in r.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Kshitij Marwah
  • 1,091
  • 3
  • 14
  • 23

2 Answers2

2

We can use aggregate from base R,

aggregate(V2 ~ V1, df, FUN = function(i) ifelse(length(i) > 1, tail(i, 1) - head(i, 1), i))

which gives,

  V1 V2
1  A  3
2  B  0
3  C  1
4  D  2

DATA

dput(df)
structure(list(V1 = structure(c(1L, 2L, 1L, 3L, 4L, 2L, 1L), .Label = c("A", 
"B", "C", "D"), class = "factor"), V2 = c(2L, 4L, 3L, 1L, 2L, 
4L, 5L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-7L))
Sotos
  • 51,121
  • 6
  • 32
  • 66
1

We could use dplyr

library(dplyr)
df1 %>% 
  group_by(V1) %>%
  summarise(V2 = if(n()>1) last(V2)- first(V2) else V2)
# A tibble: 4 x 2
#     V1    V2
#   <chr> <int>
#1     A     3
#2     B     0
#3     C     1
#4     D     2
akrun
  • 874,273
  • 37
  • 540
  • 662