FAQ/facnum - CBU statistics Wiki
Self: 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)

These data are numeric.

You can combine these into 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"])

The two columns denoting frequencies and reaction times may now be referred to as x1[,1] and x1[,2] respectively e.g. glm(x1[,1] ~ x1[,2]).

None: FAQ/facnum (last edited 2013-03-08 10:17:13 by localhost)