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

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

How to create sequence of integers in R?

问题

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

id    year
001   1988
001   1989
001   1990
001   1991
001   1992
002   1972
002   1973
002   1974
002   1975

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

我的数据:

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

I've got dataframe like so:

id    year
001   1988
001   1988
001   1988
001   1988
001   1988
002   1972
002   1972
002   1972
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:

id    year
001   1988
001   1989
001   1990
001   1991
001   1992
002   1972
002   1973
002   1974
002   1975

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

My data:

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;),
                 year = c(1988, 1988, 1988, 1988, 1988, 1972, 1972, 1972, 1972))

答案1

得分: 3

使用 seq 函数:

library(dplyr)
df %>%
  mutate(year = seq(first(year), length.out = n()), .by = id)

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

希望这对您有所帮助。

英文:

With seq:

library(dplyr)
df %&gt;% 
  mutate(year = seq(first(year), length.out = n()), .by = id)

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

答案2

得分: 2

一个“基础”解决方案:

transform(df, year = ave(year, id, FUN = \(x) seq_along(x) + x[1] - 1))

#   id year
# 1  1 1988
# 2  1 1989
# 3  1 1990
# 4  1 1991
# 5  1 1992
# 6  2 1972
# 7  2 1973
# 8  2 1974
# 9  2 1975
英文:

A base solution:

transform(df, year = ave(year, id, FUN = \(x) seq_along(x) + x[1] - 1))

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

答案3

得分: 1

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

library(data.table)

setDT(df)

df[, year := .SD + 1:.N - 1, id]

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

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

For completeness, a data.table approach

library(data.table)

setDT(df)

df[, year := .SD + 1:.N - 1, id]

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

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:

确定