Skip to content
Prev 257750 / 398502 Next

Can R replicate this data manipulation in SAS?

On Fri, Apr 22, 2011 at 11:27 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
There was a bug in f.  The last part above should have been:

f <- function(x) with(x, data.frame(
	id = head(id, -1),
	start_date = head(start_date, -1),
	stop_date = start_date[-1]-1,
	head(cbind(regimen, NRTI, NNRTI, PI, all = NRTI + NNRTI + PI,
		  HAART = as.numeric((NRTI >= 3 & NNRTI==0 & PI==0) |
			(NRTI >= 2 & (NNRTI >= 1 | PI >= 1)) |
            (NRTI == 1 & NNRTI >= 1 & PI >= 1))), -1)))
result <- do.call(rbind, by(d2, id, f))
rownames(result) <- NULL

result
id start_date  stop_date regimen NRTI NNRTI PI all HAART
1  1004 1995-07-24 1995-11-19       1    1     0  0   1     0
2  1004 1995-11-20 1995-12-09       2    2     0  0   2     0
3  1004 1995-12-10 1996-01-09       3    1     0  0   1     0
4  1004 1996-01-10 1996-05-08       4    2     0  0   2     0
5  1004 1996-05-09 1996-05-31       5    2     0  1   3     1
6  1004 1996-06-01 1996-06-30       6    3     0  1   4     1
7  1004 1996-07-01 1997-01-31       7    4     0  1   5     1
8  1004 1997-02-01 1997-02-28       8    3     0  1   4     1
9  1004 1997-03-01 1997-11-15       9    2     0  1   3     1
10 1004 1997-11-16 1999-01-04      10    2     0  0   2     0
11 9999 2003-01-02 2006-04-04       1    0     0  1   1     0
12 9999 2006-04-05 2009-07-07       2    0     1  1   2     0
13 9999 2009-07-08       <NA>       3    0     0  1   1     0