Skip to content
Prev 74714 / 398502 Next

converting stata's by syntax to R

Chris Wallace <c.wallace at qmul.ac.uk> writes:
How about

unsplit(lapply(split(dat,dat$fam), 
               function(x) seq(length=nrow(x)) == which.max(x$wt)),
        dat$fam)

or 

do.call("rbind", lapply(split(dat,dat$fam),
                        function(x) x[which.max(x$wt),]))

or (same thing, basically)

do.call("rbind", by(dat,dat$fam,function(x) x[which.max(x$wt),]))