Re: [R] allocating factor levels

From: Don McKenzie <dmck_at_u.washington.edu>
Date: Mon, 07 Mar 2011 21:37:18 -0800

Maybe clumsy but shows the activity. The idea is to use a numeric index to separate cases where Start.action is the same.

(untested)

my.data$action <- rep("set.or.haul ",20)
my.data$recnums <- c(1:20)
my.data$action[my.data$Start.action=="Start.setting" & my.data 
$recnums < 7] <- "set1"
my.data$action[my.data$Start.action=="Start.setting" & my.data $recnums > 12] <- "set2"
my.data$action[my.data$Start.action=="Start.hauling" & my.data $recnums < 13] <- "haul1"
my.data$action[my.data$Start.action=="Start.hauling" & my.data $recnums > 18] <- "haul2"

On 7-Mar-11, at 7:13 PM, Darcy Webber wrote:

> Dear R users,
>
> I am working on allocating the rows within a dataframe into some
> factor levels.Consider the following dataframe:
>
> Start.action Start.time
> 1 Start.setting 2010-12-30 17:58:00
> 2 Start.setting 2010-12-30 18:40:00
> 3 Start.setting 2010-12-31 22:39:00
> 4 Start.setting 2010-12-31 23:24:00
> 5 Start.setting 2011-01-01 00:30:00
> 6 Start.setting 2011-01-01 01:10:00
> 7 Start.hauling 2011-01-01 07:07:00
> 8 Start.hauling 2011-01-01 14:25:00
> 9 Start.hauling 2011-01-01 21:28:00
> 10 Start.hauling 2011-01-02 03:38:00
> 11 Start.hauling 2011-01-02 09:28:00
> 12 Start.hauling 2011-01-02 14:22:00
> 13 Start.setting 2011-01-02 20:51:00
> 14 Start.setting 2011-01-02 21:33:00
> 15 Start.setting 2011-01-02 22:47:00
> 16 Start.setting 2011-01-02 23:27:00
> 17 Start.setting 2011-01-03 00:35:00
> 18 Start.setting 2011-01-03 01:16:00
> 19 Start.hauling 2011-01-03 04:31:00
> 20 Start.hauling 2011-01-03 08:57:00
>
> I am trying to assign a factor level like the one below (named
> "action") according to the sequence of setting and hauling occuring in
> the "Start.action" column. In fact, it wouldnt even need to be a
> factor or character, it could simply be numbered (i.e., the set/haul
> prefix is useless as I could simply split it afterwards).
>
> Start.action Start.time action
> 1 Start.setting 2010-12-30 17:58:00 set1
> 2 Start.setting 2010-12-30 18:40:00 set1
> 3 Start.setting 2010-12-31 22:39:00 set1
> 4 Start.setting 2010-12-31 23:24:00 set1
> 5 Start.setting 2011-01-01 00:30:00 set1
> 6 Start.setting 2011-01-01 01:10:00 set1
> 7 Start.hauling 2011-01-01 07:07:00 haul1
> 8 Start.hauling 2011-01-01 14:25:00 haul1
> 9 Start.hauling 2011-01-01 21:28:00 haul1
> 10 Start.hauling 2011-01-02 03:38:00 haul1
> 11 Start.hauling 2011-01-02 09:28:00 haul1
> 12 Start.hauling 2011-01-02 14:22:00 haul1
> 13 Start.setting 2011-01-02 20:51:00 set2
> 14 Start.setting 2011-01-02 21:33:00 set2
> 15 Start.setting 2011-01-02 22:47:00 set2
> 16 Start.setting 2011-01-02 23:27:00 set2
> 17 Start.setting 2011-01-03 00:35:00 set2
> 18 Start.setting 2011-01-03 01:16:00 set2
> 19 Start.hauling 2011-01-03 04:31:00 haul2
> 20 Start.hauling 2011-01-03 08:57:00 haul2
>
> It seems like such a simple question, yet I just cant think of how to
> implement this. Any hints or ideas on how I might achieve this would
> be much appreciated.
>
> Regards,
> Darcy
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

Why does the universe go to all the bother of existing? -- Stephen Hawking

#define QUESTION ((bb) || !(bb))
-- William Shakespeare

Don McKenzie, Research Ecologist
Pacific WIldland Fire Sciences Lab
US Forest Service

Affiliate Professor
School of Forest Resources, College of the Environment CSES Climate Impacts Group
University of Washington

desk: 206-732-7824
cell: 206-321-5966
dmck_at_uw.edu
donaldmckenzie_at_fs.fed.us



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 08 Mar 2011 - 05:42:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 Mar 2011 - 05:50:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive