FAQ/facnum - CBU statistics Wiki

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment
Flind the wroneg tetters tin eaech wrord

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)

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]).