Skip to content

Using "rep", but don't know what to put after each =

2 messages · joe1985, jim holtman

#
Hello

I have one DF (detheleny1periode), with some variables that mathes, in some
way, variables in another DF (y2).

The DF named detheleny1periode look like this (i have not included alle
variables):

CHR_NR      diffdatoperiode
11377                29
11377                59  
11377                78 

with many different CHR_NR's.

And the other DF named y2 look like this (i have not included alle
variables):

CHR_NR  variab
11377       1
11377       0
11377       1
11377       0
11377       0
11377       0
11377       1
11377       0
11377       0
11377       0
11377       0
11377       0
11377       0

again with many different CHR_NR's.

So what want is to make it look like this:

CHR_NR  variab             x
11377       1               29
11377       0               29
11377       1               59
11377       0               59
11377       0               59
11377       0               59
11377       1               78 
11377       0               78  
11377       0               78
11377       0               78
11377       0               78
11377       0               78
11377       0               78

So i thougth i could use y2$x <- rep(c(detheleny1periode$diffdatoperiode,
each= ), but my problem is that i don't what to put after  "each=" because
the number of rows differs a lot between each "CHR_NR", but what i do know
is that want the detheleny1periode$diffdatoperiode repeated the times theres
is between each "1" in "variab", as I illustrated above. 

Hope you can help me
#
Here is one way of doing it.  It does not check for errors in the case
that there are not enough values in the first dataframe:
+ 11377                29
+ 11377                59
+ 11377                78"), header=TRUE)
+ 11377       1
+ 11377       0
+ 11377       1
+ 11377       0
+ 11377       0
+ 11377       0
+ 11377       1
+ 11377       0
+ 11377       0
+ 11377       0
+ 11377       0
+ 11377       0
+ 11377       0"), header=TRUE)
+     # determine the index to use by using cumsum
+     change <- cumsum(x2.p[[.name]]$variab)
+     # add new column
+     cbind(x2.p[[.name]], x=x1.p[[.name]]$diffdatoperiode[change])
+ })
CHR_NR variab  x
1   11377      1 29
2   11377      0 29
3   11377      1 59
4   11377      0 59
5   11377      0 59
6   11377      0 59
7   11377      1 78
8   11377      0 78
9   11377      0 78
10  11377      0 78
11  11377      0 78
12  11377      0 78
13  11377      0 78

        
On Fri, Feb 20, 2009 at 6:49 AM, joe1985 <johannes at dsr.life.ku.dk> wrote: