我们有一个包含2列的数据框,如下所示: 这是一个月份列表,其中包含值和分配的季度。
quarter_number base_quarter
1 63.44767
1 71.12924
1 95.58290
2 56.85544
2 45.89876
2 47.88994
3 65.54654
...
该季度的数字重复3次,然后获得+1。 目的是为四分之一的第二个和第三个月的每个值设置与第一个相同的值。所以它看起来像这样:
quarter_number base_quarter
1 63.44767
1 63.44767
1 63.44767
2 56.85544
2 56.85544
...
感谢您的提示!
答案 0 :(得分:3)
使用ave
:
R> set.seed(1)
R> Data <- data.frame(quarter_number=rep(1:3, each=3), base_quarter=10*rnorm(9))
R> Data
quarter_number base_quarter
1 1 -6.264538
2 1 1.836433
3 1 -8.356286
4 2 15.952808
5 2 3.295078
6 2 -8.204684
7 3 4.874291
8 3 7.383247
9 3 5.757814
R> head1 <- function(x) head(x,1)
R> Data$base_quarter <- ave(Data$base_quarter, Data$quarter_number, FUN=head1)
R> Data
quarter_number base_quarter
1 1 -6.264538
2 1 -6.264538
3 1 -6.264538
4 2 15.952808
5 2 15.952808
6 2 15.952808
7 3 4.874291
8 3 4.874291
9 3 4.874291
答案 1 :(得分:1)
我的第二个Joran和Andrie最好用您的代码发布可重现的数据。 这是我的方法,这个问题
set.seed(1)
Dat <- data.frame(number = rep(1:10, each =3), value = rnorm(30))
head(Dat)
number value
1 1 -0.62645
2 1 0.18364
3 1 -0.83563
4 2 1.59528
5 2 0.32951
6 2 -0.82047
require(plyr) # for join
join(number = unique(Dat$number)), Dat, match = "first")
Joining by: number
number value
1 1 -0.626454
2 2 1.595281
3 3 0.487429
4 4 -0.305388
5 5 -0.621241
6 6 -0.044934
7 7 0.821221
8 8 0.782136
9 9 0.619826
10 10 -1.470752
Dat2 <- join(data.frame(number = unique(Dat$number)), Dat, match = "first")
Result <- Dat2[rep(1:nrow(Dat2), each = 3), ]
head(Result)
number value
1 1 -0.62645
1.1 1 -0.62645
1.2 1 -0.62645
2 2 1.59528
2.1 2 1.59528
2.2 2 1.59528
希望这个帮助
答案 2 :(得分:0)
这里有几个不错的答案。这是另一种方式:
Data <- data.frame(quarter_number=rep(1:3, each=3), base_quarter=10*rnorm(9))
merge(Data, Data[! duplicated(Data$quarter_number), ],
by='quarter_number', suffix=c('.orig', '.first'))