如何从不同组最后n行的平均值返回(由可变表示)(How to return an average o

2019-10-28 10:29发布

我有一个数据象下面这样:

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"))

new_seq指的价值seq 。 对于中的每个值new_seq这是不NA我想以计算平均最后的2行的value从相应的seq 。 因此,例如行1:2一个新的列应具有的值0.5 (平均行49:50 ),行51:54也应该具有的值0.5 (平均行49:50为好),但行60:63应该具有的值4 (平均行58:59 )。 我如何能做到这一点与tidyverse

Answer 1:

像这样的事情?

# 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"))

结果是:

# 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  
...


Answer 2:

嗯,这是只有一半的tidyverse ,我敢肯定有人可以做的更好,但这里的一个尝试。

group_bymutate可以很容易地计算出最后2行组的平均水平,但我无法弄清楚如何获得之间的连接seqnew_seq所以我做了,在基地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]
})

这里的结果。 我子集相关行(否则它会是太长,一次在屏幕上看到),但添加的原始行号为rowid列:

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 


文章来源: How to return an average of last n rows from different group (indicated by a variable)