【问题标题】:Alternative to "lead" function in dplyr package?替代 dplyr 包中的“领导”功能?
【发布时间】:2021-09-26 05:31:19
【问题描述】:

我需要在我的数据框中创建一个列,其中新列 (next.1) 从列 current 的第 i + 1 行开始。我用dplyr 尝试了这段代码,它在虚拟数据集上完成了这项工作。但是,它只是在我的原始数据框中不起作用。我试图分离 dplyr 包,重新启动 R 等,但没有运气。我想知道是否有任何其他方法可以在不使用dplyr 的情况下完成相同的工作?

month <- c(1:12)
current <- c(20:31)
df <- data.frame(month, current)
df$month <- as.factor(as.character(df$month))

library(dplyr)
df <- df %>% 
  dplyr::mutate(next.1 = lead(current, default = first(current)))

【问题讨论】:

  • 你能解释一下出了什么问题吗?这段代码对我来说很好
  • 你可以试试data.table::shift()

标签: r for-loop dplyr iteration


【解决方案1】:

这可行:

library(dplyr)
df %>% 
  mutate(
         next.1 = lead(current),
         # in case you do not want the last value to be `NA`:
         next.1 = ifelse(is.na(next.1), current + 1, next.1)
         )
   month current next.1
1      1      20     21
2      2      21     22
3      3      22     23
4      4      23     24
5      5      24     25
6      6      25     26
7      7      26     27
8      8      27     28
9      9      28     29
10    10      29     30
11    11      30     31
12    12      31     32

【讨论】:

    【解决方案2】:

    这里有几个选项。

    基础R

    您可以创建一个新列,在其中删除 current 列的第一个条目,然后从中减去 1,然后将第一个条目添加为最后一个条目(或者您可以做 NA,但这只是基于您的dplyr 输出)。

    df$next.1 <- c(df$current[-1], df$current[1])
    

    输出

       month current next.1
    1      1      20     21
    2      2      21     22
    3      3      22     23
    4      4      23     24
    5      5      24     25
    6      6      25     26
    7      7      26     27
    8      8      27     28
    9      9      28     29
    10    10      29     30
    11    11      30     31
    12    12      31     20
    

    transform 来自data.table

    library(data.table)
    
    data.table::transform(df, next.1 = c(df$current[-1], df$current[1]))
    

    shift 来自data.table 以及dplyr

    library(dplyr)
    library(data.table)
    
        df %>% 
            dplyr::mutate(next.1 = data.table::shift(current, -1, df$current[1]))
    

    如果您不希望最后一行的“20”值,那么您可以在所有 3 个选项中将 df$current[1] 替换为 NA(或任何其他值)。

    【讨论】:

      猜你喜欢
      • 2017-04-30
      • 2014-07-15
      • 2016-08-05
      • 1970-01-01
      • 2016-03-21
      • 2012-05-07
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多