[R] spss.read factor reversal

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Wed Jul 27 02:30:50 CEST 2005


I think it is doing what is supposed to do but I never used read.spss,
so take this with a pinch of salt.

In R when you use as.integer on a factor, the one with the lowest level
gets a value of 1 and so on. The lowest level of the factor can
determined from levels() function.

   f <- factor( c("Green", "Green", "Red", "Blue"), 
                levels=c("Red", "Blue", "Green") )
   levels(f)
   [1] "Red"   "Blue"  "Green"

   as.integer(f)
   [1] 3 3 1 2

But the levels of a factor can be changed 

   as.integer( factor( f, levels=c("Green", "Blue", "Red" ) ) )
   [1] 1 1 3 2

You can also try setting use.value.labels=FALSE in read.spss function
and then creating a factor out of it.

Regards, Adai



On Tue, 2005-07-26 at 17:04 -0700, Joel Bremson wrote:
> Hi,
> 
> I'm having a problem with spss.read reversing my factor input.
> 
> Here is the input copied from the spss data editor:
> 
> color cost
> 1 2.30
> 2 2.40
> 3 3.00
> 1 2.10
> 1 1.00
> 1 2.00
> 2 4.00
> 2 3.20
> 2 2.33
> 3 2.44
> 3 2.55
> 
> For color, red=1, blue=2, and green = 3. It's type is 'String' and
> 
> >out=read.spss(file)
> >out
> 
> $COLOR
> [1] green blue red green green green blue blue blue red red 
> Levels: red blue green
> 
> $COST
> [1] 2.30 2.40 3.00 2.10 1.00 2.00 4.00 3.20 2.33 2.44 2.55
> 
> attr(,"label.table")
> attr(,"label.table")$COLOR
> green blue red 
> 3 2 1 
> 
> attr(,"label.table")$COST
> NULL
> 
> attr(,"variable.labels")
> COLOR COST 
> "color" "cost" 
> 
> =====EOF===================
> 
> Notice that the $COLOR factor data are inverted, looking at the integer 
> output
> we see:
> 
> > as.integer(out$COLOR)
> [1] 3 2 1 3 3 3 2 2 2 1 1
> 
> The spss original data looks like this:
> 1 2 3 1 1 1 2 2 2 3 3
> 
> I can easily invert the output mathematically with:
> q = sapply(m,function(x){ x + 2*(median(unique(m))-x)})
> 
> (m is composed of sequential integers starting at one)
> 
> ,but it seems as though something wrong is happening with read.spss.
> 
> Any ideas?
> 
> Joel Bremson
> Graduate Student
> UC Davis
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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
>




More information about the R-help mailing list