Changing the class of columns in a data frame

First of all, I'm sorry if I'm wrong, but English is not the language that I use very often.

I have a data frame with numbers. Small part of the data frame:

rated 2 2 2 2

ordinal 2 1 1 2

So, I want to use the distance function on these numbers.

Here ( http://rgm2.lab.nig.ac.jp/RGM2/R_man-2.9.0/library/StatMatch/man/gower.dist.html ) it says that to use gower.dist all nominal variables must be of class "factor" and all the ordinal variables of the class are "ordered".

By default, all columns have the class "integer" and "numeric". To change the class of columns, I use the following commands:

 DF=read.table("clipboard",header=TRUE,sep="\t")      
 # I select all the cells and I copy them to the clipboard. 
 #Then R, with this command, reads the data from there.

 MyHeader=names(DF)     # I save the headers of the data frame to a temp matrix

 for (i in 1:length(DF))  {
     if (MyHeader[[i]]=="nominal") DF[[i]]=as.factor(DF[[i]])
 }     

 for (i in 1:length(DF))  {
     if (MyHeader[[i]]=="ordinal") DF[[i]]=as.ordered(DF[[i]])
 }        

for/if , , "" "".

" " "", gower.dist.

, : .

+3
2

, , - .

- :

> foo <- as.ordered(1:10)
> foo
 [1] 1  2  3  4  5  6  7  8  9  10
Levels: 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10
> class(foo)
[1] "ordered" "factor" 

, , , :

> is.ordered(as.ordered(1:10))
[1] TRUE
> is.factor(as.ordered(1:10))
[1] TRUE

, foo , . , , , , R . R, "ordered" "factor". , Gower.

+1

DF$nominal <- as.factor(DF$nominal)
DF$ordinal <- as.ordered(DF$ordinal)

. , , [[]], , . Owen R. .

, , gower.dist() . daisy():

DF <- data.frame(
    ordinal= c(1,2,3,1,2,1),
    nominal= c(2,2,2,2,2,2)
)
DF$nominal <- as.factor(DF$nominal)
DF$ordinal <- as.ordered(DF$ordinal)

library(cluster)
daisy(DF,metric="gower")
library(StatMatch)
gower.dist(DF)
+1

Source: https://habr.com/ru/post/1776773/


All Articles