FAQ/facnum - CBU statistics Wiki

Revision 3 as of 2007-07-20 12:32:25

Clear message
location: FAQ / facnum

How do I convert a factor variable into a numeric in R?

Assume data is stored in a file of type *.dat. This is done by first reading the .dat file into SPSS specifying that variable names are in the top line and saving as a SPSS file e.g. patients.sav.

library(foreign)
x <- read.spss("patients.sav")
attach(x)
is.numeric(x)

You can then create a data matrix (of n rows and v columns, assuming n cases and v variables have just been read into R) using the data.frame command and specifying the variable names (as capitals inside double quotes) as they appeared in the SPSS file. So for example if you have two variables called FRQ and RT you would type

x1 <- data.frame(x["FRQ"],x["RT"])