[R] factor, as.factor and levels

jim holtman jholtman at gmail.com
Wed Apr 8 15:31:48 CEST 2009


It is just a simple version of 'factor'.  The only speed advantage it
might have is that it checks to see if it is a factor first.  Here is
the definition:

)> as.factor
function (x)
if (is.factor(x)) x else factor(x)
<environment: namespace:base>

You can always list out what the function does to get a better
understanding of how it works.

On Wed, Apr 8, 2009 at 8:16 AM, Heinz Tuechler <tuechler at gmx.at> wrote:
> Thank you, Jim. I see, the fact that in the documentation you find only
> "as.factor(x)" means that it does not accept more arguments.
> Does as.factor have speed advantages over factor, or is there a different
> cause for it's existence?
>
> Heinz
>
>
>
> At 13:50 08.04.2009, jim holtman wrote:
>>
>> as.factor does not accept levels as an argument.  use the first form
>> that you have
>>
>> factor(ch1, levels=ch1)
>>
>>
>>
>> On Wed, Apr 8, 2009 at 7:36 AM, Heinz Tuechler <tuechler at gmx.at> wrote:
>> > Dear All,
>> >
>> > to my surprise as.factor does not accept a levels argument. Maybe I did
>> > not
>> > read the documentation well enough. See the example below. I wanted to
>> > use
>> > ch1 as factor in the newdata argument of survfit, so I assumed that I
>> > could
>> > write as.factor(ch1, levels=ch1), since the order should be kept.
>> >
>> > But as.factor(ch1, levels=ch1) results in the error:
>> >
>> > Error in as.factor(ch1, levels = ch1) :
>> >  unused argument(s) (levels = c("low", "inter", "high"))
>> >
>> > factor(ch1, levels=ch1) works as I expected.
>> > Is it intended that as.factor does not use the levels argument?
>> >
>> > Thanks,
>> >
>> > Heinz
>> >
>> > ch1 <- c('low', 'inter', 'high')
>> > factor(ch1)
>> > factor(ch1, levels=ch1)
>> > as.factor(ch1, levels=ch1)
>> >
>> >> version
>> >               _
>> > platform       i386-pc-mingw32
>> > arch           i386
>> > os             mingw32
>> > system         i386, mingw32
>> > status         Patched
>> > major          2
>> > minor          8.1
>> > year           2009
>> > month          03
>> > day            13
>> > svn rev        48132
>> > language       R
>> > version.string R version 2.8.1 Patched (2009-03-13 r48132)
>> >> sessionInfo()
>> > R version 2.8.1 Patched (2009-03-13 r48132)
>> > i386-pc-mingw32
>> >
>> > locale:
>> >
>> > LC_COLLATE=German_Switzerland.1252;LC_CTYPE=German_Switzerland.1252;LC_MONETARY=German_Switzerland.1252;LC_NUMERIC=C;LC_TIME=German_Switzerland.1252
>> >
>> > attached base packages:
>> > [1] splines   stats     graphics  grDevices utils     datasets  methods
>> > [8] base
>> >
>> > other attached packages:
>> > [1] survival_2.34-1 car_1.2-12      gmodels_2.14.1  gdata_2.4.2
>> > [5] Hmisc_3.5-2
>> >
>> > loaded via a namespace (and not attached):
>> > [1] cluster_1.11.12 grid_2.8.1      gtools_2.5.0-1  lattice_0.17-20
>> > [5] MASS_7.2-46
>> >>
>> >
>> > ______________________________________________
>> > 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.
>> >
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list