Possible Duplicate:
How to convert a factor to an integer\numeric without a loss of information
I want to count (and later on plot the count of) the unique values of an array:
data = c(1,2,3,4,5,2.1,1,2,1,2,1,4,5,7,8,9,6,5,4,3,2,2,1)
uniCount = as.data.frame(table(data))
uniCount$cumsum = cumsum(uniCount$Freq)
str(uniCount)
plot (uniCount$data, uniCount$Freq)
plot (uniCount$data, uniCount$cumsum)
But, the values of the column data is not ‘numeric’ but ‘Factor’. For me it seems that the datatype Factor is an associative array of Strings. When I use as.numeric(uniCount$data) the result gives “1 2 3 4 5 6 7 8 9 10”
How can I convert the datatype “Factor” to the datatype “numeric”?
Or how can I prevent that R converts my numeric values to a Facotr?
this may work for you
or
if you dont want factors try
EDIT:
thanks @Carl Witthoft, but
?factorsays thatas.numeric(levels(uniCount$data))[uniCount$data]is recommended and slightly more efficient thanas.numeric(as.character(uniCount$data))“