Roberts, J. Kyle wrote:
I have a dataset in which I need to dummy code (sort of) two separate
variables.
Variable 1
I have 5 years of data on students in teachers classrooms. Each teacher
entered the intervention at different times (years 1-6). I need to code
a date variable for each student as zero for each student in the
classroom before the intervention, and as a "1" when the intervention
started.
For example, in this case, the variable "new.var" would be created from
"year" and "int.year".
teach stud year int.year new.var
1 1 1 3 0
1 2 1 3 0
1 3 2 3 0
1 4 2 3 0
1 5 3 3 1
1 6 3 3 1
1 7 4 3 1
1 8 4 3 1
Variable 2
This is like the above data, but I need the new.var.2 variable to begin
counting up from the intervention year. For example:
teach stud year int.year new.var.2
1 1 1 3 0
1 2 1 3 0
1 3 2 3 0
1 4 2 3 0
1 5 3 3 1
1 6 3 3 1
1 7 4 3 2
1 8 4 3 2
Thanks,
Kyle
***************************************
J. Kyle Roberts, Ph.D.
Baylor College of Medicine
Center for Educational Outreach
One Baylor Plaza, MS: BCM411
Houston, TX 77030-3411
713-798-6672 - 713-798-8201 Fax
jkrobert@bcm.edu
***************************************
How about (assuming `x' is your data.frame):
x$new.var <- ifelse(x$int.year >= x$year, 1, 0)
x$new.var.2 <- ifelse(x$int.year >= x$year, x$year - x$int.year + 1, 0)
HTH,
--sundar
|