[R] Coding a new variable based on criteria in a dataset
bbolker at gmail.com
Tue Dec 21 14:22:33 CET 2010
RaoulD <raoul.t.dsouza <at> gmail.com> writes:
> I'm a bit stuck and need some help with R code to code a variable F_R based
> on a combination of conditions.
> The first condition would code F_R as "F" and would be based on the
> min(Date) and Min(Time) for each combination of UniqueID & Reason. The
> second condition would code the variable as "R" as it would be the rest of
> the data that dont meet the first condition.
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),
## make sure x is sorted by date/time here
x$F_R <- c("F",rep("R",nrow(x)-1))
> For example: for "UID 1" & "Reason 1" the first record would be coded "F"
> and the 4th record would be coded "R".
> UniqueID Reason Date Time
> 1 UID 1 Reason 1 19/12/2010 15:00
> 2 UID 1 Reason 2 19/12/2010 16:00
> 3 UID 1 Reason 3 19/12/2010 16:30
> 4 UID 1 Reason 1 20/12/2010 08:00
More information about the R-help