按组增加

时间:2017-02-24 11:39:53

标签: r dplyr

我正在尝试为每个组增加一列。所以如果有一个值,那么我们会根据它之前的值递增它,否则我们就离开它。

例如,它会从df转到dfb。

df <- data.frame(group = c("A", "A", "B", "B", "B", "C", "C", "C", "D", "D"), 
                 num = c(1, NA, NA, 8, NA, 5, NA, NA, 10, NA))
dfb <- data.frame(group = c("A", "A", "B", "B", "B", "C", "C", "C", "D", "D"),
                 num = c(1, 2, NA, 8, 9, 5, 6, 7, 10, 11))
> df

   group num
1      A   1
2      A  NA
3      B  NA
4      B   8
5      B  NA
6      C   5
7      C  NA
8      C  NA
9      D  10
10     D  NA

> dfb
   group num
1      A   1
2      A   2
3      B  NA
4      B   8
5      B   9
6      C   5
7      C   6
8      C   7
9      D  10
10     D  11

我最好的尝试就是这个,但它不起作用

dfc <- df %>%
   mutate(num = ifelse(is.na(num),lag(num) + 1, num))

删除了我之前的问题,因为我的问题以前定义得很糟糕。 谢谢你的帮助!

2 个答案:

答案 0 :(得分:6)

我们可以做到

df %>% 
 group_by(grp1= cumsum(!is.na(num)), group) %>%
 mutate(num = if(n() > 1) num[1L] + row_number()-1 else num) %>% 
 ungroup() %>%
 select(-grp1)
# A tibble: 10 × 2
#    group   num
#   <fctr> <dbl>
#1       A     1
#2       A     2
#3       B    NA
#4       B     8
#5       B     9
#6       C     5
#7       C     6
#8       C     7
#9       D    10
#10      D    11

data.table

library(data.table)
setDT(df)[, num := if(.N >1) num[1L] + seq_len(.N)-1
            else num,.(grp1=cumsum(!is.na(num)), group)]

答案 1 :(得分:0)

你需要的只是一个基本的循环:)欢呼!

df <- data.frame(group = c("A", "A", "B", "B", "B", "C", "C", "C", "D", "D"), 
                 num = c(1, NA, NA, 8, NA, 5, NA, NA, 10, NA))
df.new <- df

for(i in 2:dim(df.new)[1]){

  if(!is.na(df[i - 1, 'num'])){

    df.new[i, 'num'] <- df[i - 1, "num"] + 1

  }