Re: [R] How to change the class of data?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu, 12 Jun 2008 11:31:07 +0100 (BST)

On Thu, 12 Jun 2008, Birgitle wrote:

>
> I have an additional question concerning to this topic.
>
> I usually use something liek that:
>
> read.table(****, colClasses=c("numeric", "factor", "character",
> "my.funny.class"))
>
> but why can I not implement "ordered.factor" in there?

Because the help page says

colClasses: character. A vector of classes to be assumed for the

           columns.  Recycled as necessary, or if the character vector
           is named, unspecified values are taken to be 'NA'.

           Possible values are 'NA' (when 'type.convert' is used),
           '"NULL"' (when the column is skipped), one of the atomic
           vector classes (logical, integer, numeric, complex,
           character, raw), or '"factor"', '"Date"' or '"POSIXct"'.
           Otherwise there needs to be an 'as' method (from package
           'methods') for conversion from '"character"' to the specified
           formal class.

There is no as.ordered.factor() nor as() method, and nothing in the data can specify the order of the levels unambiguously. So unless one accepts a guess (and in R we try not to do that for you, as in e.g. as.Date with numbers), there is no possibility to support ordered factors.

>
> Birgit
>
>
> Kenn Konstabel wrote:
>>
>> Conversion to factor may happen (and often does) when you read in data
>> with
>> read.table(). So one solution may be reading in the same data again in a
>> slightly different way:
>>
>> read.table(file="mydatafile", as.is=TRUE)
>> # see also ?read.table
>>
>> You can also specify a class to each column of the data you're about to
>> read
>> in:
>> read.table(****, colClasses=c("numeric", "factor", "character",
>> "my.funny.class"))
>>
>>
>> Ad take a look at http://cran.r-project.org/doc/FAQ/R-FAQ.html p.
>> 7.10
>> for the right answer -- in any case, don't use as.numeric(x)!
>>
>> Kenn
>>
>>
>> On Thu, Jun 12, 2008 at 9:24 AM, Qman Fin <finosaur_at_gmail.com> wrote:
>>
>>> Hi all,
>>>
>>> I have some data x, which are actualy consisted of numerical enties. But
>>> the
>>> class of this matrix is set to be "factor" by someone else. I used
>>> "class(x)", it turns out to be "factor". So I can not calculate them.
>>>
>>> How can I turn them into numerical data so that I can apply math
>>> operations
>>> on them? Thanks a lot for your help.
>>>
>>> Selina
>>>
>>> [[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.
>>>
>>
>> [[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.
>>
>>
>
>
> -----
> The art of living is more like wrestling than dancing.
> (Marcus Aurelius)
> --
> View this message in context: http://www.nabble.com/How-to-change-the-class-of-data--tp17793351p17795106.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 guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
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.
Received on Thu 12 Jun 2008 - 12:16:22 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 12 Jun 2008 - 12:30:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive