[R] trimming a factor

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Fri Mar 10 21:59:04 CET 2006



Dan Chan wrote:
> Hi, 
> 
> I have the following dataframe.  The County Column has many empty spaces
> at the end.
> I want to cut out the empty space and put them back into the dataframe.
> How can this be done?  
> 
> 
>>head(DailyCounty)
> 
>                  Date               County GFCPer GFCEquip Acre nFires
> Date2
> 1 2001-01-04 00:00:00 Appling                   1        1 0.20      1
> 2001-01-04
> 2 2001-01-05 00:00:00 Appling                   2        2 0.13      1
> 2001-01-05
> 3 2001-01-06 00:00:00 Appling                   1        1 3.41      1
> 2001-01-06
> 4 2001-01-07 00:00:00 Appling                   1        1 0.18      1
> 2001-01-07
> 5 2001-01-10 00:00:00 Appling                   1        1 5.10      1
> 2001-01-10
> 6 2001-01-16 00:00:00 Appling                   1        1 0.48      1
> 2001-01-16
> 
> 
>>sub(' +$','',levels(DailyCounty$County))
> 
> "Appling" 
> 
> 
>>sub(' +$','',DailyCounty$County)
> 
> [1] "1"
> 

I think you want:

DailyCounty$County <-
   factor(sub(' +$', '', as.character(DailyCounty$County)))


HTH,

--sundar




More information about the R-help mailing list