I am importing a dataset in R where some of the variable are numerical and some of them are character...but the problem is that R is treating numerical variables as character (I am using "is.character" to judge the type). Now the question is how can I convert these character variables into numeric and also let me know about the other conversion like "numeric to categorical","numeric to continuous" etc.Thank you. Arup -- View this message in context: http://www.nabble.com/How-to-convert-Charagter-variables-into-numeric-tp21867485p21867485.html Sent from the R help mailing list archive at Nabble.com.
You might want to try as.numeric(X), if you want to convert a factor then you could use as.numeric(as.character(X)), where X is a "variable". Karina On Thu, 5 Feb 2009 22:50:38, <arup.pramanik27 at gmail.com> wrote:> I am importing a dataset in R where some of the variable are numericaland> some of them are character...but the problem is that R is treating > numerical variables as character (I am using "is.character" to judge the > type). Now the question is how can I convert these character variablesinto> numeric and also let me know about the other conversion like "numeric to > categorical","numeric to continuous" etc.Thank you. > > Arup > >--
Wacek Kusnierczyk
2009-Feb-06 13:43 UTC
[R] How to convert Charagter variables into numeric
if you use read.table for the import, reading about the colClasses parameter in ?read.table may help. vQ> On Thu, 5 Feb 2009 22:50:38, <arup.pramanik27 at gmail.com> wrote: > > >> I am importing a dataset in R where some of the variable are numerical >> > and > >> some of them are character...but the problem is that R is treating >> numerical variables as character (I am using "is.character" to judge the >> type). Now the question is how can I convert these character variables >> > into > >> numeric and also let me know about the other conversion like "numeric to >> categorical","numeric to continuous" etc.Thank you. >> >> Arup >>
Dear Arup, It's very difficult to know what's going on without more information. What is the form of the data that you're importing and how are you reading the data? I ask because the most common way of getting data into R is probably via read.table(), which by default converts character data to factors but reads numbers as numeric. If a "column" of input data has one or more non-numeric entries (other than the default missing-data indicator NA), it will be read as a factor, even if most entries in the column are numbers. To answer your more general question, R has a number of "coercion" functions prefixed by "as." for changing data from one form to another: For example, if by "numeric to categorical" you mean numeric to a factor, then as.factor() will do the trick. There is also the more general coercion function as() (see ?as). I hope this helps, John ------------------------------ John Fox, Professor Department of Sociology McMaster University Hamilton, Ontario, Canada web: socserv.mcmaster.ca/jfox> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]On> Behalf Of Arup > Sent: February-06-09 1:51 AM > To: r-help at r-project.org > Subject: [R] How to convert Charagter variables into numeric > > > I am importing a dataset in R where some of the variable are numerical and > some of them are character...but the problem is that R is treating > numerical variables as character (I am using "is.character" to judge the > type). Now the question is how can I convert these character variablesinto> numeric and also let me know about the other conversion like "numeric to > categorical","numeric to continuous" etc.Thank you. > > Arup > -- > View this message in context:http://www.nabble.com/How-to-convert-Charagter-> variables-into-numeric-tp21867485p21867485.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.