根据列重复数据集中的行,但增加行 [英] repeat rows in a dataset based on a column, but increment the rows

查看:79
本文介绍了根据列重复数据集中的行,但增加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中包含项目名称,开始年份和合同期限。我需要将此数据集开发为时间序列。例如,我的数据集中的一行是:项目A,从2003年开始,合同期限为5。我想根据合同期限重复每一行。我的数据集如下所示:

I have a dataset which has project name, start year and contract term. I need to develop this dataset into time series. For example, one row in my dataset is: Project A, start year 2003 and contract term 5. I would like to repeat each row based on contract term. My dataset looks like this:

Project Name    Start Year    Contract Term
A                 2003            5
B                 2013            3
C                 2000            2

我想要的结果应如下所示:

My desired result should look like this:

Project Name    Start Year    Contract Term
A                 2003            5
A                 2004            5
A                 2005            5
A                 2006            5
A                 2007            5

B                 2013            3
B                 2014            3
B                 2014            3

C                 2000            2
C                 2001            2

我尝试过:

rpsData <- rpsInput[rep(rownames(rpsInput), rpsInput$Contract.Term), ]

但这只会重复一次按合同条款中的数字进行项目。我不能让它增加年份。

But this only repeats each project by the number in contract term. I can not make it to increment the years.

谢谢!

推荐答案

这里分两个步骤:

步骤1,您知道:

rpsData <- rpsInput[rep(rownames(rpsInput), rpsInput$Contract.Term), ]
rpsData
#     Project.Name Start.Year Contract.Term
# 1              A       2003             5
# 1.1            A       2003             5
# 1.2            A       2003             5
# 1.3            A       2003             5
# 1.4            A       2003             5
# 2              B       2013             3
# 2.1            B       2013             3
# 2.2            B       2013             3
# 3              C       2000             2
# 3.1            C       2000             2

第2步使用顺序和基本加法:

Step 2 makes use of sequence and basic addition:

sequence(rpsInput$Contract.Term) ## This will be helpful...
#  [1] 1 2 3 4 5 1 2 3 1 2

rpsData$Start.Year <- rpsData$Start.Year + sequence(rpsInput$Contract.Term)
rpsData
#     Project.Name Start.Year Contract.Term
# 1              A       2004             5
# 1.1            A       2005             5
# 1.2            A       2006             5
# 1.3            A       2007             5
# 1.4            A       2008             5
# 2              B       2014             3
# 2.1            B       2015             3
# 2.2            B       2016             3
# 3              C       2001             2
# 3.1            C       2002             2

这篇关于根据列重复数据集中的行,但增加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆