Skip to content
Prev 245532 / 398506 Next

Coding a new variable based on criteria in a dataset

RaoulD <raoul.t.dsouza <at> gmail.com> writes:
It isn't quite convenient to read the data posted below into R
(if it was originally tab-separated, that formatting got lost) but
ddply from the plyr package is good for this: something like (untested)

  d <- with(data,ddply(data,interaction(UniqueID,Reason),
                    function(x) {
                          ## make sure x is sorted by date/time here
                          x$F_R <- c("F",rep("R",nrow(x)-1))
                          x
                     })
[snip]