Άγνωστος μέσα στο πλήθος
2010-Nov-24 19:27 UTC
[R] Change the class of columns in a data frame
Hi. First of all, excuse me if I do any mistakes, but English is not a language I use very often. I have a data frame with numbers. A small part of the data frame is this: nominal ordinal 2 2 2 1 2 1 2 2 So, I want to use the gower distance function on these numbers. Here ( http://rgm2.lab.nig.ac.jp/RGM2/R_man-2.9.0/library/StatMatch/man/gower.dist.html) says that in order to use gower.dist, all nominal variables must be of class "factor" and all ordinal variables of class "ordered". By default, all the columns are of class "integer" and mode "numeric". In order to change the class of the columns, i use these commands:> DF=read.table("clipboard",header=TRUE,sep="\t") *# I select all thecells 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 tempmatrix*> 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]])} * * The first for/if loop changes the class from integer to factor, which is what I want, but the second changes the class of ordinal variables to: "ordered" "factor". I need to change all the columns with the header "ordinal" to "ordered", as the gower.dist function says. Thanks in advance, B.T. [[alternative HTML version deleted]]
you can do it like this: # example data DF <- data.frame(nominal = rep(2, 5), ordinal = c(1,2,2,1,2)) DF$nominal <- factor(DF$nominal) DF$ordinal <- ordered(DF$ordinal) DF str(DF) I hope it helps. Best, Dimitris On 11/24/2010 8:27 PM, ???????? ???? ??? ?????? wrote:> Hi. > > First of all, excuse me if I do any mistakes, but English is not a language > I use very often. > > I have a data frame with numbers. A small part of the data frame is this: > > > nominal ordinal > 2 2 > 2 1 > 2 1 > 2 2 > > > So, I want to use the gower distance function on these numbers. > > Here ( > http://rgm2.lab.nig.ac.jp/RGM2/R_man-2.9.0/library/StatMatch/man/gower.dist.html) > says that in order to use gower.dist, all nominal variables must be of > class "factor" and all ordinal variables of class "ordered". > > By default, all the columns are of class "integer" and mode "numeric". In > order to change the class of the columns, i use these 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]])} * * > > > The first for/if loop changes the class from integer to factor, which is > what I want, but the second changes the class of ordinal variables to: > "ordered" "factor". > > I need to change all the columns with the header "ordinal" to "ordered", as > the gower.dist function says. > > Thanks in advance, > B.T. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
Άγνωστος μέσα στο πλήθος
2010-Nov-24 20:04 UTC
[R] Change the class of columns in a data frame
Hi. First of all, excuse me if I do any mistakes, but English is not a language I use very often. I have a data frame with numbers. A small part of the data frame is this: nominal ordinal 2 2 2 1 2 1 2 2 So, I want to use the gower distance function on these numbers. Here ( http://rgm2.lab.nig.ac.jp/RGM2/R_man-2.9.0/library/StatMatch/man/gower.dist.html) says that in order to use gower.dist, all nominal variables must be of class "factor" and all ordinal variables of class "ordered". By default, all the columns are of class "integer" and mode "numeric". In order to change the class of the columns, i use these commands:> DF=read.table("clipboard",header=TRUE,sep="\t") *# I select all thecells 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 tempmatrix*> 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]])} * * The first for/if loop changes the class from integer to factor, which is what I want, but the second changes the class of ordinal variables to: "ordered" "factor". I need to change all the columns with the header "ordinal" to "ordered", as the gower.dist function says. Thanks in advance, B.T. [[alternative HTML version deleted]]