gpt4 book ai didi

R将具有开始和结束年份的数据帧转换为时间序列

转载 作者:行者123 更新时间:2023-12-05 00:48:14 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Given start date and end date, reshape/expand data for each day between (each day on a row) [duplicate]

(2 个回答)


3年前关闭。




我想将具有起始年和结束年变量的数据框转换为一个完整的时间序列,其中 (1) 包括开始年和结束年之间的所有年份和 (2) 填充中间年份的所有变量。

这是原始数据的样子:

data_original <- data.frame(name = c("peter", "peter", "eric", "denisse"), lastname = c("smith", "smith", "jordan", "williams"), age = c(54, 54, 48, 40), start_year = c(1980,1986, 1990, 2000), end_year = c(1984, 1988, 1993, 2001))

data_original
#> name lastname age start_year end_year
#> 1 peter smith 54 1980 1984
#> 2 peter smith 54 1986 1988
#> 3 eric jordan 48 1990 1993
#> 4 denisse williams 40 2000 2001

这就是我希望数据的样子:
data_final <- data.frame(name = c("peter", "peter", "peter", "peter", "peter", "peter", "peter", "peter", "eric", "eric", "eric", "eric", "denisse", "denisse"), lastname = c("smith", "smith", "smith", "smith", "smith", "smith", "smith", "smith", "jordan", "jordan", "jordan", "jordan", "williams", "williams"), age = c(54, 54, 54, 54, 54, 54, 54, 54, 48, 48, 48, 48, 40, 40), year = c(1980, 1981, 1982, 1983, 1984, 1986, 1987, 1988, 1990, 1991, 1992, 1993, 2000, 2001))

data_final
#> name lastname age year
#> 1 peter smith 54 1980
#> 2 peter smith 54 1981
#> 3 peter smith 54 1982
#> 4 peter smith 54 1983
#> 5 peter smith 54 1984
#> 6 peter smith 54 1986
#> 7 peter smith 54 1987
#> 8 peter smith 54 1988
#> 9 eric jordan 48 1990
#> 10 eric jordan 48 1991
#> 11 eric jordan 48 1992
#> 12 eric jordan 48 1993
#> 13 denisse williams 40 2000
#> 14 denisse williams 40 2001

非常感谢您对此以及您的持续帮助!

最佳答案

这是 tidyverse 的一种选择.通过使用 map2 获取一系列“start_year”、“end_year”来创建“year” , select相关栏目和unnest

library(tidyverse)
data_original %>%
mutate(year = map2(start_year, end_year, `:`)) %>%
select(-start_year, -end_year) %>%
unnest
# name lastname age year
#1 peter smith 54 1980
#2 peter smith 54 1981
#3 peter smith 54 1982
#4 peter smith 54 1983
#5 peter smith 54 1984
#6 peter smith 54 1986
#7 peter smith 54 1987
#8 peter smith 54 1988
#9 eric jordan 48 1990
#10 eric jordan 48 1991
#11 eric jordan 48 1992
#12 eric jordan 48 1993
#13 denisse williams 40 2000
#14 denisse williams 40 2001

或者另一种选择是 data.table
library(data.table)
setDT(data_original)[, .(name, lastname, year = seq(start_year, end_year, by = 1)),
.(grp = 1:nrow(data_original))][, grp := NULL][]

或者我们可以使用 base R以及 Map
lst <- do.call(Map, c(f = `:`, data_original[4:5]))
out <- data_original[1:3][rep(seq_len(nrow(data_original)), lengths(lst)),]
row.names(out) <- NULL

关于R将具有开始和结束年份的数据帧转换为时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50034366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com