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)
Tyler Smith
2007-Apr-19 19:03 UTC
[R] Character coerced to factor and I cannot get it back
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