How to return an average of last n rows from diffe

2019-08-23 09:12发布

问题:

I've got a data like below:

data <- structure(list(seq = c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
7L, 7L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L), new_seq = c(2, 2, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
2, 2, 2, 2, NA, NA, NA, NA, NA, 4, 4, 4, 4, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 6, 6, 6, 6, 6, NA, NA, 8, 8, 8, NA, NA, NA), value = c(2L, 
0L, 0L, 1L, 0L, 5L, 5L, 3L, 0L, 3L, 2L, 3L, 2L, 3L, 4L, 1L, 0L, 
0L, 0L, 1L, 1L, 0L, 2L, 5L, 3L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 3L, 
5L, 3L, 1L, 1L, 1L, 0L, 1L, 0L, 4L, 3L, 0L, 3L, 1L, 3L, 0L, 0L, 
1L, 0L, 0L, 3L, 4L, 5L, 3L, 5L, 3L, 5L, 0L, 1L, 1L, 3L, 2L, 1L, 
0L, 0L, 0L, 0L, 5L, 1L, 1L, 0L, 4L, 1L, 5L, 0L, 3L, 1L, 2L, 1L, 
0L, 3L, 0L, 1L, 1L, 3L, 0L, 1L, 1L, 2L, 2L, 1L, 0L, 4L, 0L, 0L, 
3L, 0L, 0L)), row.names = c(NA, -100L), class = c("tbl_df", "tbl", 
"data.frame"))

Column new_seq refers to the value of seq. For each value in new_seq which is not NA I would like to calculate a average of last 2 rows of value from respective seq. So, for example rows 1:2 of a new column should have a value of 0.5 (average of rows 49:50), rows 51:54 also should have a value of 0.5 (average of rows 49:50 as well), but rows 60:63 should have value of 4 (average of rows 58:59). How can I do that with tidyverse?

回答1:

Something like this?

# calculate the mean value based on the last two rows of each seq
lookup <- data %>%
  group_by(seq) %>%
  mutate(rank = seq(n(), 1)) %>% 
  filter(rank <= 2) %>%
  summarise(new_column = mean(value)) %>%
  ungroup()

# match back to original dataset (only non-NA values of new_seq can be matched)
left_join(data, lookup, by = c("new_seq" = "seq"))

Results in:

# A tibble: 100 x 4
     seq new_seq value new.column
   <int>   <dbl> <int>      <dbl>
 1     1       2     2        0.5
 2     1       2     0        0.5
 3     2      NA     0       NA  
 4     2      NA     1       NA  
...


回答2:

Well, it's only half tidyverse and I'm sure someone can do better, but here's an attempt.

group_by and mutate make it easy to calculate the average of the last 2 rows of the group, but I couldn't figure out how to get the connection between seq and new_seq so I did that in base R.

dat2 <- dat %>%
    group_by(seq) %>%
    mutate(end_val = (nth(value, -1L) + nth(value, -2L))/2)

dat3$result <- apply(dat2, 1, function(x) {
    dat2[dat2$seq == x['new_seq'], 'end_val'][[1]][1]
})

Here's the result. I subsetted the relevant rows (since otherwise it'd be too long to see on the screen at once) but added the original row numbers as the rowid column:

dat3 %>% tibble::rowid_to_column() %>% .[c(1:3,50:55,59:64),] 

# A tibble: 15 x 6
# Groups:   seq [6]
   rowid   seq new_seq value end_val result
   <int> <int>   <dbl> <int>   <dbl>  <dbl>
 1     1     1       2     2     1      0.5
 2     2     1       2     0     1      0.5
 3     3     2      NA     0     0.5   NA  
 4    50     2      NA     1     0.5   NA  
 5    51     3       2     0     3.5    0.5
 6    52     3       2     0     3.5    0.5
 7    53     3       2     3     3.5    0.5
 8    54     3       2     4     3.5    0.5
 9    55     4      NA     5     4     NA  
10    59     4      NA     5     4     NA  
11    60     5       4     0     2      4  
12    61     5       4     1     2      4  
13    62     5       4     1     2      4  
14    63     5       4     3     2      4  
15    64     6      NA     2     2     NA