I'll try to replicate your problem:
set.seed(1)
a <- factor(sample(1:100, 10))
> a
[1] 27 37 57 89 20 86 97 62 58 6
Levels: 6 20 27 37 57 58 62 86 89 97
The alexwhan comment is fine actually:
> as.numeric(as.character(a))
[1] 27 37 57 89 20 86 97 62 58 6
Even if your data needs to be trim()ed
it would work anyway:
> paste( " ", a, " ")
[1] " 27 " " 37 " " 57 " " 89 " " 20 " " 86 " " 97 " " 62 " " 58 " " 6 "
> as.numeric(paste( " ", a, " "))
[1] 27 37 57 89 20 86 97 62 58 6
SO the only explanation is you have some (unexpected) character in all your numbers
> as.numeric(paste(a, "a"))
[1] NA NA NA NA NA NA NA NA NA NA
Warning message:
NAs introduced by coercion
If you can't see any letter the following happened to me:
> paste( intToUtf8(160), a, intToUtf8(160))
[1] " 27 " " 37 " " 57 " " 89 " " 20 " " 86 " " 97 " " 62 " " 58 " " 6 "
> as.numeric(paste( intToUtf8(160), a, intToUtf8(160)))
[1] NA NA NA NA NA NA NA NA NA NA
intToUtf8(32) is the usual white space from the keyboard (like above some lines) but the number 160 is something that looks similar what is another different thing, which as.numeric
(and also trim
from gdata
) doesn't recognise and returns
NA
.