[R] A Few Suggestions to help out newbies

Prof Brian D Ripley ripley at stats.ox.ac.uk
Wed Apr 3 09:05:44 CEST 2002

On Tue, 2 Apr 2002 david.whiting at ncl.ac.uk wrote:

> On Tue, Apr 02, 2002 at 03:28:13AM -0800, Zed Shaw wrote:
> ...
> > 2)  A Series of Documents helping people translate from another package
> > to R.  For example, "R for SPSS People", "R for SAS People", etc.
> >
> The way I started to teach myself how to use and program in R was to
> write some functions that either simulated STATA output or at least spat
> out a reminder about how to get the output I needed. Here's one example:
> stata.encode <- function(){
> cat("*** STATA to R ***\nEncode character data as a numbered factor. Take a look at\nas.factor(). Here's a quick example:\n\nx <- as.factor(x)\n\nTo see the codes, use codes(x)\n")
> }
> I created a crude library called stata so I could then type:
> library(stata)
> stata.encode()
> ...which gave me...
> *** STATA to R ***
> Encode character data as a numbered factor. Take a look at
> as.factor(). Here's a quick example:
> x <- as.factor(x)
> To see the codes, use codes(x)

Sorry, but that just shows the dangers.  Where did you get that from?
It's not the right answer, and there is a note on the help page for codes
to that effect.  codes() is never necessary, and it is better to forget it
exists.  I use unclass, but as.numeric and as.integer also work.

Similarly, factor(x) is better than as.factor(x), both in avoiding a step
but more importantly because factor has other arguments you should be
considering using.


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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list