-2

we got a data frame with 2 columns looking like this: It's a list of months with a value and their quarter assigned.

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
...

The number for the quarter is repeading 3 times and then gets +1. Aim is it to set for each value of the second and thrid month of a quarter the same value as of the first one. So it will look like this:

quarter_number base_quarter 
1            63.44767
1            63.44767
1            63.44767
2            56.85544
2            56.85544
...

Thanks for your hints!

Fabian Stolz
  • 1,935
  • 7
  • 27
  • 30
  • 6
    Rather than asking us how to do it from scratch, we typically prefer that you demonstrate that you've tried _something_ (and show your code) and explain how it didn't work. Otherwise, we begin to feel suspiciously like free labor. – joran Jun 24 '12 at 19:53
  • Also, please at least post reproducible data, as explained here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Andrie Jun 24 '12 at 20:12
  • `my.obj[my.obj$quarteR_number == 1] <- 63.44767` should set all `1` to a defined number. – Roman Luštrik Jun 24 '12 at 20:15

3 Answers3

3

Use 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
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
1

I second Joran and Andrie it's better to post reproducible data with your code. Here's my approach, to this problem

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

Hope this help

dickoa
  • 18,217
  • 3
  • 36
  • 50
0

There are a couple of nice answers here. This is another way:

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'))
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113