[R] how to define ordinal, nominal and scale type variables?

Duncan Murdoch murdoch.duncan at gmail.com
Fri Sep 6 19:36:51 CEST 2013


On 06/09/2013 5:29 AM, Markus Gschwind wrote:
> Hello,
>
> I am a beginner of R but knowing SPSS and matlab.
>
> I need to analyse my data with a mixed type cluster analysis, that's why I
> am looking into R.
>
> I have a datasheet with 45 subjects (rows) and 30 variables of each subjet
> (columns) in Excel (datasheet.csv) or SPSS (datasheet.sav).
>
> In SPSS I can define if the variable is ordinal (e.g.  low - intermediate -
> high), nominal (e.g. type of hobby) or scale (e.g. age).
> The variables are all numerical (as SPSS needs them in numbers coding the
> ordinal or nominal qualities).
>
> However, when using data importing like
>
> mydata<-read.spss("datasheet.sav")
> or
> mydata<-read.table("datasheet.csv", sep=",",na="",stringsAsFactors=F)
>
> this produces a 'list' and not a 'data frame' with factors.
>
> My question: How can I import the "measure" (i.e. nominal, ordinal, scale)
> form SPSS?
> Or how can I define variables in mydata as nominal or ordinal, even if they
> consist of numbers?

Actually, the read.table() function will produce a dataframe, but you 
asked for no factors, so you'll get no factors.

To convert a variable x to a nominal factor, use

f <- factor(x, levels = c( ..... ))

where the levels are chosen to list all possible values that x could 
take.  If x is a column of a dataframe d, use

d$f <- factor(d$x, levels = c( .... ))

to create a new column named f in the dataframe.

For an ordinal factor, do the same, but use ordered() instead of factor().

(You can do some of this automatically using read.table, e.g. by 
specifying colClasses, but you have more flexibility if you do it as 
shown above.)

Duncan Murdoch



More information about the R-help mailing list