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" Thank you! Daniel Chan Meteorologist Georgia Forestry Commission P O Box 819 Macon, GA 31202 Tel: 478-751-3508 Fax: 478-751-3465
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
RSiteSearch("trim") or RSiteSearch("trim space") will get you there. Francisco>From: "Dan Chan" <dchan at GFC.STATE.GA.US> >To: <r-help at stat.math.ethz.ch> >Subject: [R] trimming a factor >Date: Fri, 10 Mar 2006 15:52:03 -0500 > >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" > >Thank you! > > >Daniel Chan >Meteorologist >Georgia Forestry Commission >P O Box 819 >Macon, GA >31202 >Tel: 478-751-3508 >Fax: 478-751-3465 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! >http://www.R-project.org/posting-guide.html