【问题标题】:Create columns based on multiple other columns in R基于 R 中的多个其他列创建列
【发布时间】:2021-05-24 17:11:53
【问题描述】:

我正在尝试根据我的数据中的三列(CompanyNameYear)创建多个其他列。

我想创建多个列,以提供下表中显示的输出。以下是我要创建的每一列的条件。

Number_Years: 一个人在公司工作的年数。 7years_Span: 一个人在过去 7 年(从当年开始)在公司工作的年数。 Continuous_Years: 一个人在公司工作的连续年数。 Years_Gap:Person 上次在公司工作和再次加入公司之间的年数。

基于列 CompanyNameYear:我想创建上述定义的其他列

Company Name Year Number_Years 7years_Span Continuous_Years Years_Gap
ABC John 2002 7 1 3 0
ABC John 2003 7 2 3 0
ABC John 2004 7 3 3 0
ABC Dave 2005 2 1 1 0
ABC John 2006 7 4 1 1
ABC Dave 2007 2 2 1 1
ABC John 2008 7 5 2 1
ABC John 2009 7 5 2 0
BBC Jim 2010 1 1 1 0
ABC Jim 2010 2 1 2 0
BBC Dave 2011 1 1 1 0
BBB John 2011 1 1 1 0
ABC Jim 2011 2 2 2 0
ABC John 2012 7 4 1 2

我已经尝试创建这些列,但到目前为止我还没有完成,我在 StackOverflow 和其他平台上进行了搜索,但没有成功。如果有人可以提供帮助,将不胜感激。我希望我提供了一个包含足够信息的简单示例。

示例数据输入:

structure(list(Company = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 3L, 1L, 3L, 2L, 1L, 1L), .Label = c("ABC", "BBB", "BBC"
), class = "factor"), Name = structure(c(3L, 3L, 3L, 1L, 3L, 
1L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 3L), .Label = c("Dave", "Jim", 
"John"), class = "factor"), Year = c(2002L, 2003L, 2004L, 2005L, 
2006L, 2007L, 2008L, 2009L, 2010L, 2010L, 2011L, 2011L, 2011L, 
2012L), Number_Years = c(7L, 7L, 7L, 2L, 7L, 2L, 7L, 7L, 1L, 
2L, 1L, 1L, 2L, 7L), `7years_span` = c(1L, 2L, 3L, 1L, 4L, 2L, 
5L, 5L, 1L, 1L, 1L, 1L, 2L, 4L), Continuous_Years = c(3L, 3L, 
3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L), Years_Gap = c(0L, 
0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 2L)), class = "data.frame", row.names = c(NA, 
-14L))

【问题讨论】:

  • 请为您的数据框提供代码。你可以使用dput(dataframe)
  • “当年”是什么意思?去年? 2021 年?
  • “Years_Gap:Person 上次在公司工作的年数。”。该人最后一次在公司工作和什么时间之间?
  • @GuedesBF 感谢您的考虑。我所说的“当前年份”是指如果从 2004 年开始计算,那么考虑从 2004 年开始向后的前 7 年,而不是从 2003 年开始向后计算。对于第二个澄清,Person 上次与公司合作到再次加入公司与他们合作的年数。所以基本上是上次为公司工作然后再次加入以来的差距。
  • 为什么 2008 年和 2009 年的 ABC + John continuous years1?不应该是2吗?

标签: r dplyr


【解决方案1】:

已编辑

df <- structure(list(Company = c("ABC", "ABC", "ABC", "ABC", "ABC", 
                                 "ABC", "ABC", "ABC", "BBC", "ABC", "BBC", "BBB", "ABC", "ABC"
), Name = c("John", "John", "John", "Dave", "John", "Dave", "John", 
            "John", "Jim", "Jim", "Dave", "John", "Jim", "John"), Year = c(2002L, 
                                                                           2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2010L, 
                                                                           2011L, 2011L, 2011L, 2012L)), row.names = c(NA, -14L), class = "data.frame")
df
#>    Company Name Year
#> 1      ABC John 2002
#> 2      ABC John 2003
#> 3      ABC John 2004
#> 4      ABC Dave 2005
#> 5      ABC John 2006
#> 6      ABC Dave 2007
#> 7      ABC John 2008
#> 8      ABC John 2009
#> 9      BBC  Jim 2010
#> 10     ABC  Jim 2010
#> 11     BBC Dave 2011
#> 12     BBB John 2011
#> 13     ABC  Jim 2011
#> 14     ABC John 2012
library(runner)
library(dplyr)

df %>%
  group_by(Company, Name) %>%
  mutate(Number_years = n(),
         X7Years_span = runner(x = Year,
                               k = 7,
                               idx = Year,
                               f = function(x) length(x)),
         Continous_years = cumsum(c(0, diff(Year)) != 1),
         Years_gap = Year - lag(Year, default = first(Year) -1) -1) %>%
  group_by(Continous_years, .add = T) %>%
  mutate(Continous_years = n()) %>%
  ungroup()
#> # A tibble: 14 x 7
#>    Company Name   Year Number_years X7Years_span Continous_years Years_gap
#>    <chr>   <chr> <int>        <int>        <int>           <int>     <dbl>
#>  1 ABC     John   2002            7            1               3         0
#>  2 ABC     John   2003            7            2               3         0
#>  3 ABC     John   2004            7            3               3         0
#>  4 ABC     Dave   2005            2            1               1         0
#>  5 ABC     John   2006            7            4               1         1
#>  6 ABC     Dave   2007            2            2               1         1
#>  7 ABC     John   2008            7            5               2         1
#>  8 ABC     John   2009            7            5               2         0
#>  9 BBC     Jim    2010            1            1               1         0
#> 10 ABC     Jim    2010            2            1               2         0
#> 11 BBC     Dave   2011            1            1               1         0
#> 12 BBB     John   2011            1            1               1         0
#> 13 ABC     Jim    2011            2            2               2         0
#> 14 ABC     John   2012            7            4               1         2

reprex package (v2.0.0) 于 2021-05-25 创建

【讨论】:

  • 比我的回答好多了。干得好。
  • continuous year计算不清楚
  • 是的,它仍然需要来自 OP 的一些澄清。我认为您可能需要max() 之类的东西,不太确定如何按所有“条纹”进行分组。 .....Continous_years = max(streak_run(Continous_years))....
  • 谢谢@AnilGoyal,这很有帮助! @GuedesBF,我将在continuous year 上尝试最大方法。祝你有美好的一天!
  • @FurqanShah,请参阅编辑后的答案。它现在与预期的输出完全匹配
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-26
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多