[R] How to change the class of data?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jun 12 15:38:07 CEST 2008


Try this workaround:

library(methods)
setClass("of")
setAs("character", "of", function(from) as.ordered(from))

Input <- "A B
b 1
c 2
a 3
"
DF <- read.table(textConnection(Input), header = TRUE,
  colClasses = c("of", "numeric"))
str(DF)


On Thu, Jun 12, 2008 at 4:57 AM, Birgitle <birgit.lemcke at systbot.uzh.ch> 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?
>
> 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.
>



More information about the R-help mailing list