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

John Kane jrkrideau at yahoo.ca
Thu Apr 19 21:51:13 CEST 2007


--- John Kane <jrkrideau at yahoo.ca> wrote:

> Date: Thu, 19 Apr 2007 15:50:36 -0400 (EDT)
> From: John Kane <jrkrideau at yahoo.ca>
> Subject: RE: [R] Character coerced to factor and I
> cannot get it back
> To: Jorge Cornejo-Donoso <jorgecornejo at uach.cl>
> 
> 
> --- Jorge Cornejo-Donoso <jorgecornejo at uach.cl>
> wrote:
> 
> > Maybe you could made something like:
> > Id <- floor(number/10000)
> > 
> > On this way you would  get out the last 4 numbers
> of
> > the "number"
> 
> Very nice.  You made me realise that dd$cc %/% 10000
> will do the same thing too and both work without all
> the character handling. 
> 
> I still wish I knew why as.character still gives me
> a
> factor. 
> 
> Thanks
> 
> 
> 
> > 
> > -----Mensaje original-----
> > De: r-help-bounces at stat.math.ethz.ch
> > [mailto:r-help-bounces at stat.math.ethz.ch] En
> nombre
> > de John Kane
> > Enviado el: Jueves, 19 de Abril de 2007 11:39
> > Para: R R-help
> > Asunto: [R] Character coerced to factor and I
> cannot
> > get it back
> > 
> > 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.
> > 
> > __________ Informacisn de NOD32, revisisn 2205
> > (20070419) __________
> > 
> > Este mensaje ha sido analizado con  NOD32
> antivirus
> > system
> > http://www.nod32.com
> > 
> > 
> > 
> 
> 
> 
>       Ask a question on any topic and get answers
> from real people. Go to Yahoo! Answers and share

>



More information about the R-help mailing list