[R] Simple (?) subset problem

David Scott d.scott at auckland.ac.nz
Fri Aug 15 01:37:11 CEST 2008


On Thu, 14 Aug 2008, Farley, Robert wrote:

> BINGO!
>
>
>> str(SurveyData$direction_)
> Factor w/ 2 levels "EASTBOUND
> ",..: 1 1 1 1 2 2 1 1 2 1 ...
>> levels(SurveyData$direction_)
> [1] "EASTBOUND                                         " "WESTBOUND
> "
>>
>
>
> Was my mistake in how I read the data?
>
> SurveyData <- read.spss("C:/Data/R/orange_delivery.sav",
> use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE)
>
>
>
> That brings up 2 more questions:
>
> How do I "trim" the factor names? {or read them correctly}
>
> How do I write to a list the names of factors? {I have another factor
> with ~15 "levels" and I'm a REALLY poor typist}
>
>

Good guess on the spaces! Comes of having played with a lot of data over 
the years and seeing that sort of thing before.

You need sub. There is an example of trimming trailing spaces in the gsub 
help page.

So something like:

temp <- sub(' +$', '', SurveyData$direction_)
SurveyData$direction_ <- temp


should do it. You could just replace the direction_ variable without using 
temp but it is safer this way and you should check the values of temp 
before replacing your original data.

David Scott

_________________________________________________________________
David Scott	Department of Statistics, Tamaki Campus
 		The University of Auckland, PB 92019
 		Auckland 1142,    NEW ZEALAND
Phone: +64 9 373 7599 ext 86830		Fax: +64 9 373 7000
Email:	d.scott at auckland.ac.nz

Graduate Officer, Department of Statistics
Director of Consulting, Department of Statistics



More information about the R-help mailing list