Reputation: 463
There are similar questions out there, but I'm pretty confident that this one is unique.
I have a large dataframe with variables that include numeric vectors and various IDs.
> dataframe
ID ID.2 vectors
1 a c(6, 3, 7, 4, 7, 2, 6)
1 a c(4, 7, 2, 7, 4, 7, 8)
2 a c(5, 8, 2, 7, 8, 9, 9)
2 a c(6, 9, 7, 2, 6, 8, 9)
1 b c(4, 8, 2, 6, 8, 9, 0)
1 b c(5, 8, 2, 7, 8, 9, 9)
I need to iterate through the dataframe and find the difference between the vectors by ID variables. So the output from the fake data above should look like:
> dataframe$difference
c(2, -4, 5, -3, 3, -5, -2)
NA # because difference has been found
c(-1, -1, -4, 5, 2, 1, 0)
NA
c(-1, 0, 0, -1, 0, 0, -9)
NA
This has been (surprisingly) complex to figure out. Thanks in advance.
Upvotes: 0
Views: 183
Reputation: 43334
If it were for individual numbers, you could use diff
, but for a list column (presumably) of vectors, you'd need to fall back on list operations like purrr::accumulate
(for consecutive subtracting) or purrr::map2
(for pairwise). dplyr::lead
will shift up one and insert the NA
s.
library(tidyverse)
df <- data_frame(ID = c(1L, 1L, 2L, 2L, 1L, 1L),
ID.2 = c("a", "a", "a", "a", "b", "b"),
vectors = list(c(6, 3, 7, 4, 7, 2, 6), c(4, 7, 2, 7, 4, 7, 8), c(5, 8, 2, 7, 8, 9, 9), c(6, 9, 7, 2, 6, 8, 9), c(4, 8, 2, 6, 8, 9, 0), c(5, 8, 2, 7, 8, 9, 9)))
df %>%
group_by(ID, ID.2) %>%
mutate(diff = map2(vectors, lead(vectors), `-`)) %>%
as.data.frame() # for printing
#> ID ID.2 vectors diff
#> 1 1 a 6, 3, 7, 4, 7, 2, 6 2, -4, 5, -3, 3, -5, -2
#> 2 1 a 4, 7, 2, 7, 4, 7, 8 NA, NA, NA, NA, NA, NA, NA
#> 3 2 a 5, 8, 2, 7, 8, 9, 9 -1, -1, -5, 5, 2, 1, 0
#> 4 2 a 6, 9, 7, 2, 6, 8, 9 NA, NA, NA, NA, NA, NA, NA
#> 5 1 b 4, 8, 2, 6, 8, 9, 0 -1, 0, 0, -1, 0, 0, -9
#> 6 1 b 5, 8, 2, 7, 8, 9, 9 NA, NA, NA, NA, NA, NA, NA
Upvotes: 2