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

John Kane jrkrideau at yahoo.ca
Thu Apr 19 17:39:27 CEST 2007

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?



mylist  <- list(dd  <- data.frame(aa <- 1:4, bb <- 
         cc <- c(12345, 123456, 45678, 456789)),
          vec  <-  letters[1:10]  )

#Get data frame from list
dd <-  mylist[[1]]

# Convert numeric id to character id
id  <-  as.character(dd[,3]) ; id
# get site designators (first one or two characters of

st  <- substring(id, 1,nchar(id)-4 ) ; st
typeof (st)  ; class(st)

dd1  <-  cbind(dd, st)
    names(dd1)  <- c("aa","bb","cc","st")
    typeof(dd1$st); class(dd1$st)
dd2  <-  cbind(dd, as.character(st))
    names(dd2)  <- c("aa","bb","cc","st")
    typeof(dd2$st) ;   class(dd2$st)

More information about the R-help mailing list