[R] Conditional variable assignment

David Winsemius dwinsemius at comcast.net
Tue Mar 16 21:04:50 CET 2010


On Mar 16, 2010, at 3:44 PM, Hosack, Michael wrote:

> Hi everyone,
>
> Once again I am stuck with a problem I can't seem to figure out. I  
> suppose this learning curve levels off eventually, lol. I am  
> learning on my own with absolutely no background in programming, so  
> if I seem to request help a lot it's not because I am seeking others  
> to do the work for me. I need to assign one of two arrival times to  
> the 'ARRIVE' variable of my dataframe, and I want a value assigned  
> only to the first element of each 'MM' by 'DD' variable grouping.  
> The value used (either 0700 or 1430) will be conditional on the  
> value in the 'TOD' variable, such that if 'TOD' == 1,  
> 'ARRIVE'<-0700, and if 'TOD'==2, 'ARRIVE'<-1430. I want the  
> remaining three 'MM' by 'DD' values for each unique grouping to  
> remain "NA" for now, because I intend to calculate them later. I  
> have tried ifelse statements with ddply, for loops, etc. to no  
> avail. Any help would be greatly appreciated.

Logical indexing;

dfrm[!duplicated(paste(dfrm$MM, dfrm$DD)), "ARRIVE"] <- "0700"
dfrm$ARRIVE[dfrm$ARRIVE == "0700" & dfrm$TOD=="2"] <- "1430"

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list