[R] Character coerced to factor and I cannot get it back

John Kane jrkrideau at yahoo.ca
Sat Apr 21 02:01:44 CEST 2007


--- Gabor Grothendieck <ggrothendieck at gmail.com>
wrote:

> Just one caveat.  I personally would try to avoid
> using
> global options since it can cause conflicts when
> two different programs assume two different settings
> of the same global option and need to interact.

Thanks, Gabor. A good point.  Given the data set I
have the global option is better for me at the moment
(and the data set is coming from a SAS environment—I
don't think they had ever heard of R),  but I had not
considered that problem in the longer term. Blast,  I
may have to change back again after this project is
done!

However your help and Steven's has saved me a lot of
time and grief.
> 
> On 4/20/07, John Kane <jrkrideau at yahoo.ca> wrote:
> > Thanks Stephen and Gabor.
> >
> > It works like a charm both ways.  It took me a
> minute
> > or two to find the global command but  I
> definately
> > like it :)  Now to remember to change it on the
> USB
> > installation too.
> >
> > Any idea what the rationale was for setting the
> option
> > to TRUE?  I can see advantages either way but
> > personally I like to set my factors myself and for
> a
> > newbie it is disconcerting to see my characters
> refuse
> > to stay that way.
> >
> > --- Stephen Tucker <brown_emu at yahoo.com> wrote:
> >
> > > You can also set this option globally with
> > > options(stringsAsFactors = TRUE)
> > >
> > > I believe this was added in R 2.4.0.
> > >
> >
> > > --- Gabor Grothendieck <ggrothendieck at gmail.com>
> > > wrote:
> > >
> > > > Try this:
> > > >
> > > > DF <- data.frame(let = letters[1:3], num =
> 1:3,
> > > stringsAsFactors = FALSE)
> > > > str(DF)
> > > >
> > > >
> > > > On 4/19/07, John Kane <jrkrideau at yahoo.ca>
> wrote:
> > > > >
> > > > > --- Tyler Smith <tyler.smith at mail.mcgill.ca>
> > > wrote:
> > > > >
> > > > > > I really need to sit down with the manual
> and
> > > sort
> > > > > > factors and classes
> > > > > > properly. In your case, I think the
> problem
> > > has
> > > > > > something to do with
> > > > > > the way a list behaves?  I'm not sure, but
> if
> > > you
> > > > > > convert your list to
> > > > > > a dataframe it seems to work ok:
> > > > > >
> > > > > > > dd3 <- as.data.frame(dd1)
> > > > > > > typeof(dd3$st)
> > > > > > [1] "integer"
> > > > > > > class(dd3$st)
> > > > > > [1] "factor"
> > > > > > > dd3$st <- as.character(dd3$st)
> > > > > > > typeof(dd3$st)
> > > > > > [1] "character"
> > > > > > > class(dd3$st)
> > > > > > [1] "character"
> > > > > >
> > > > > > HTH,
> > > > > >
> > > > > > Tyler
> > > > >
> > > > > Seems to work nicely. I had forgotten about
> > > > > 'as.data.frame.
> > > > >
> > > > > I originally thought that it might be a list
> > > problem
> > > > > too but I don't think so. I set up the
> example
> > > as a
> > > > > list since that is the way my real data is
> being
> > > > > imported from csv. However after my original
> > > posting I
> > > > > went back and tried it with just a dataframe
> and
> > > I'm
> > > > > getting the same results. See below.
> > > > >
> > > > > I even shut down R , reloaded it and
> detached
> > > the two
> > > > > extra packages I usually load. Everything is
> > > working
> > > > > fine but I am doing some things with factors
> > > that I
> > > > > have never done before and this just makes
> me a
> > > bit
> > > > > paranoid.
> > > > >
> > > > > Thanks very much for the help.
> > > > >
> > > > >
> > > > > EXAMPLE
> > > > > dd  <- data.frame(aa <- 1:4, bb <-
> > > letters[1:4],
> > > > >         cc <- c(12345, 123456, 45678,
> 456789))
> > > > >
> > > > > id  <-  as.character(dd[,3]) ; id
> > > > >
> > > > > st  <- substring(id, 1,nchar(id)-4 ) ; st
> > > > > typeof (st)  ; class(st)
> > > > >
> > > > > dd1  <-  cbind(dd, st)
> > > > >    names(dd1)  <- c("aa","bb","cc","st")
> > > > >    dd1
> > > > >    typeof(dd1$st); class(dd1$st)
> > > > >
> > > > > dd2  <-  cbind(dd, as.character(st))
> > > > >    names(dd2)  <- c("aa","bb","cc","st")
> > > > >    dd2
> > > > >    typeof(dd2$st) ;   class(dd2$st)
> > > > >
> > > > >
> ______________________________________________
> > > > > R-help at stat.math.ethz.ch 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.
> > > > >
> > > >
> > > > ______________________________________________
> > > > R-help at stat.math.ethz.ch 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.
> > > >
> > >
> > >
> > >
> __________________________________________________
> > > Do You Yahoo!?
> >
> > > protection around
> > > http://mail.yahoo.com
> > >
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch 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