英文:
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 <- data.frame(id = c("001", "001", "001", "001", "001", "002", "002", "002", "002"),
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 %>%
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论