【问题标题】:Find row of value zero and add a row count before and after it查找值为零的行并在其前后添加行数
【发布时间】:2020-05-16 21:34:15
【问题描述】:

根据以下数据:

library(tidyverse)

limit <- c(7, 7, 7, 7, 7, 7, 7, 7, 7, 4, 4, 4, 4, 4, 4, 4, 4, 4,  5, 5, 5, 5, 5, 5, 5, 5, 5)
group <- c("a", "a", "a", "a", "a", "a", "a", "a", "a","b", "b", "b", "b", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "c", "c", "c")

df <- data.frame(limit, group) 

df

我想创建一个新列(NewCol),如下所示:

如果存在限制 = Id 的行,则在 NewCol 上应该为 0。但是我希望 0 之前的所有行都以相反的顺序返回,直到组的第一行,并且 0 之后的所有行都被计算到组的末尾。

例如,在这种情况下,对于组“a”,它应该看起来像

-6, -5, -4, -3, -2, -1, 0, 1, 2 其中 -6 是该组的第一行,2 是该组的第 9 行。

这是我尝试过的,但仍然没有得到我需要的东西

df %>% group_by(group) %>% mutate(Id = seq(1:length(limit))) %>%
  mutate(NewCol = ifelse(limit == Id, 0, NA)) %>%
  mutate(nn=ifelse(is.na(NewCol),
                       zoo::na.locf(NewCol) + cumsum(is.na(NewCol))*1,
                       NewCol))

谢谢

【问题讨论】:

  • 如果没有limit = Id 所在的行怎么办?每个组的限制都相同吗?
  • 是的,所有组的行数都相同,唯一改变的是限制的位置

标签: r dplyr tidyverse rowcount


【解决方案1】:

这只是row_number()和分组后的'limit'的区别

library(dplyr)
df %>% 
    group_by(group) %>%
    mutate(NewCol = row_number() - limit)

或使用data.table

library(data.table)
setDT(df)[, NewCol := seq_len(.N) - limit]

或者base R

df$NewCol <- with(df, ave(seq_along(limit), group, FUN = seq_along) - limit)

【讨论】:

    【解决方案2】:

    在 Base R 中,我们可以使用 ave

    df$NewCol <- with(df, ave(limit, group, FUN = seq_along) - limit)
    
    #   limit group NewCol
    #1      7     a     -6
    #2      7     a     -5
    #3      7     a     -4
    #4      7     a     -3
    #5      7     a     -2
    #6      7     a     -1
    #7      7     a      0
    #8      7     a      1
    #9      7     a      2
    #10     4     b     -3
    #11     4     b     -2
    #12     4     b     -1
    #13     4     b      0
    #...
    

    或者使用data.table

    library(data.table)
    setDT(df)[, NewCol := seq_along(limit) - limit, group]
    #Or
    #setDT(df)[, NewCol := seq_len(.N) - limit, group]
    

    【讨论】:

      猜你喜欢
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 2017-06-08
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多