如何在R中创建整数序列?

huangapple go评论99阅读模式
英文:

How to create sequence of integers in R?

问题

对于每个 id,我想将变量 year 转换为从第一个观察开始,每个连续行增加一的序列。期望的输出:

  1. id year
  2. 001 1988
  3. 001 1989
  4. 001 1990
  5. 001 1991
  6. 001 1992
  7. 002 1972
  8. 002 1973
  9. 002 1974
  10. 002 1975

如何实现这个目标?我更喜欢使用 tidyverse 的解决方案。谢谢。

我的数据:

  1. df <- data.frame(id = c("001", "001", "001", "001", "001", "002", "002", "002", "002"),
  2. year = c(1988, 1988, 1988, 1988, 1988, 1972, 1972, 1972, 1972))
英文:

I've got dataframe like so:

  1. id year
  2. 001 1988
  3. 001 1988
  4. 001 1988
  5. 001 1988
  6. 001 1988
  7. 002 1972
  8. 002 1972
  9. 002 1972
  10. 002 1972

For each id, I would like to convert the variable year into a sequence beginning from the first observation, increasing by one in each consecutive row. The desired output:

  1. id year
  2. 001 1988
  3. 001 1989
  4. 001 1990
  5. 001 1991
  6. 001 1992
  7. 002 1972
  8. 002 1973
  9. 002 1974
  10. 002 1975

How do I do this? A tidyverse solution would be preferred. Thanks.

My data:

  1. df &lt;- data.frame(id = c(&quot;001&quot;, &quot;001&quot;, &quot;001&quot;, &quot;001&quot;, &quot;001&quot;, &quot;002&quot;, &quot;002&quot;, &quot;002&quot;, &quot;002&quot;),
  2. year = c(1988, 1988, 1988, 1988, 1988, 1972, 1972, 1972, 1972))

答案1

得分: 3

使用 seq 函数:

  1. library(dplyr)
  2. df %>%
  3. mutate(year = seq(first(year), length.out = n()), .by = id)
  4. # id year
  5. # 1 1 1988
  6. # 2 1 1989
  7. # 3 1 1990
  8. # 4 1 1991
  9. # 5 1 1992
  10. # 6 2 1972
  11. # 7 2 1973
  12. # 8 2 1974
  13. # 9 2 1975

希望这对您有所帮助。

英文:

With seq:

  1. library(dplyr)
  2. df %&gt;%
  3. mutate(year = seq(first(year), length.out = n()), .by = id)
  4. # id year
  5. # 1 1 1988
  6. # 2 1 1989
  7. # 3 1 1990
  8. # 4 1 1991
  9. # 5 1 1992
  10. # 6 2 1972
  11. # 7 2 1973
  12. # 8 2 1974
  13. # 9 2 1975

答案2

得分: 2

一个“基础”解决方案:

  1. transform(df, year = ave(year, id, FUN = \(x) seq_along(x) + x[1] - 1))
  2. # id year
  3. # 1 1 1988
  4. # 2 1 1989
  5. # 3 1 1990
  6. # 4 1 1991
  7. # 5 1 1992
  8. # 6 2 1972
  9. # 7 2 1973
  10. # 8 2 1974
  11. # 9 2 1975
英文:

A base solution:

  1. transform(df, year = ave(year, id, FUN = \(x) seq_along(x) + x[1] - 1))
  2. # id year
  3. # 1 1 1988
  4. # 2 1 1989
  5. # 3 1 1990
  6. # 4 1 1991
  7. # 5 1 1992
  8. # 6 2 1972
  9. # 7 2 1973
  10. # 8 2 1974
  11. # 9 2 1975

答案3

得分: 1

对于完整性,可以使用data.table方法:

  1. library(data.table)
  2. setDT(df)
  3. df[, year := .SD + 1:.N - 1, id]

或者如果最初每个id的年份不都相同,那么您需要使用first(year)

  1. df[, year := first(year) + 1:.N - 1, id]
英文:

For completeness, a data.table approach

  1. library(data.table)
  2. setDT(df)
  3. df[, year := .SD + 1:.N - 1, id]

or in case not all years are the same initially per id then you need first(year)

  1. df[, year := first(year) + 1:.N - 1, id]

huangapple
  • 本文由 发表于 2023年7月17日 21:43:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定