[R] Fwd: Re: Character coerced to factor and I cannot get it back
John Kane
jrkrideau at yahoo.ca
Fri Apr 20 16:48:22 CEST 2007
--- John Kane <jrkrideau at yahoo.ca> wrote:
> Date: Fri, 20 Apr 2007 10:47:45 -0400 (EDT)
> From: John Kane <jrkrideau at yahoo.ca>
> Subject: Re: [R] Character coerced to factor and I
> cannot get it back
> To: jim holtman <jholtman at gmail.com>
>
> Thanks Jim,
>
> I can live with it as a factor or I will do as you
> suggest. What is bothering me is that I cannot seem
> to reliably convert it to a character and have it
> stay one when I incorporate it into a data.frame.
>
> Now that I know what is happening I can work around
> it
> but what happens the next time when I do a similar
> cbind?
>
> I cannot see why I get a factor when I deliberately
> so as as.character(x) command.
>
> Have I stumbled over a bug or is this how R is
> supposed to work ? Here is the problem in it's
> simplest form.
> aa <- c('a','b','c','d', 'e')
> bb <- 11:15
> dd <- data.frame(bb, aa)
> class(dd[,1])
> class(dd[,2])
>
>
> ddd <- data.frame(bb, as.character(aa))
> class(ddd[,1])
> class(ddd[,2])
>
> If you run these "aa" is converted to a factor in
> each
> case.
>
> > sessionInfo()
> R version 2.4.1 (2006-12-18)
> i386-pc-mingw32
>
> locale:
>
LC_COLLATE=English_Canada.1252;LC_CTYPE=English_Canada.1252;LC_MONETARY=English_Canada.1252;LC_NUMERIC=C;LC_TIME=English_Canada.1252
>
> attached base packages:
> [1] "stats" "graphics" "grDevices" "utils"
> "datasets" "methods"
> [7] "base"
>
>
>
> --- jim holtman <jholtman at gmail.com> wrote:
>
> > Then convert it to a character and leave it there,
> > or add another
> > column with the value in characters. If you leave
> > it as a factor, you
> > just have to remember to use the 'as.character' at
> > the appropriate
> > times.
> >
> > On 4/19/07, John Kane <jrkrideau at yahoo.ca> wrote:
> > >
> > > --- jim holtman <jholtman at gmail.com> wrote:
> > >
> > > > To convert the 'factor' to 'numeric':
> > > >
> > > > as.numeric(as.character(dd1$st))
> > >
> > > But I don't want it converted to numeric. It is
> a
> > > site id which I would actually prefer as a
> > character
> > > variable. In this case I really don't mind it
> > being a
> > > factor. It is more I am just annoyed that I
> cannot
> > > coerce it to character and keep it there for I
> may
> > > need to coerce some other factors to character
> and
> > I
> > > wondered what I was doing wrong.
> > >
> > > Thanks
> > >
> > > >
> > > >
> > > > On 4/19/07, John Kane <jrkrideau at yahoo.ca>
> > wrote:
> > > > > Something probably obivous but I don't see
> it.
> > I
> > > > > needed to find the first 1 or two digits of
> > some 5
> > > > and
> > > > > 6 digit numbers since they identified
> research
> > > > sites
> > > > > while the rest of the number was the plot
> id.
> > > > >
> > > > > I converted the numbers to characters, got
> the
> > > > first 1
> > > > > or 2 characters as appropriate and went to
> > add
> > > > the
> > > > > new vector to the data.frame. For some
> reason
> > R
> > > > is
> > > > > insisting on turning the character variables
> > into
> > > > > factors. This is alright since they really
> > are
> > > > > factors anyway but I took me a while to
> > realise
> > > > what
> > > > > was happening.
> > > > >
> > > > > For convenience, because of the way I had
> > defined
> > > > some
> > > > > other variables it tried to coerce the
> > variable
> > > > back
> > > > > into character and I cannot.
> > > > >
> > > > > Can anyone explain what I am doing wrong or
> > where
> > > > I am
> > > > > misunderstanding what R is doing and why?
> > > > >
> > > > > Thanks
> > > > >
> > > > > EXAMPLE
> > > > >
> > > > > mylist <- list(dd <- data.frame(aa <- 1:4,
> > bb <-
> > > > > letters[1:4],
> > > > > cc <- c(12345, 123456, 45678,
> > 456789)),
> > > > > vec <- letters[1:10] )
> > > > >
> > > > > #Get data frame from list
> > > > > dd <- mylist[[1]]
> > > > > attach(dd)
> > > > >
> > > > > # Convert numeric id to character id
> > > > > id <- as.character(dd[,3]) ; id
> > > > > # get site designators (first one or two
> > > > characters of
> > > > > 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.
> > > > >
> > > >
> > > >
> > > > --
> > > > Jim Holtman
> > > > Cincinnati, OH
> > > > +1 513 646 9390
> > > >
> > > > What is the problem you are trying to solve?
> > > >
> > >
> > >
> > >
> > > Ask a question on any topic and get answers
> > from real people. Go to Yahoo! Answers and share
> > >
> >
> >
> > --
> > Jim Holtman
> > Cincinnati, OH
> > +1 513 646 9390
> >
> > What is the problem you are trying to solve?
> >
>
>
>
> Be smarter than spam. See how smart SpamGuard
> is at giving junk email the boot with the All-new
> http://mrd.mail.yahoo.com/try_beta?.intl=ca
>
>
More information about the R-help
mailing list