[R] Changing factor to numeric

Krzysztof Sakrejda-Leavitt krzysztof.sakrejda at gmail.com
Tue Mar 10 09:38:21 CET 2009


If the real problem is that R reads your data file and converts 
everything it can into factors try including stringsAsFactors=FALSE
in your read.table (or similar) statement.  I run into this often enough 
that I set it as an option (I think it's: 
options(stringsAsFactors=FALSE).  Then you can do the conversion as 
needed.  Using as.numeric(as.character(f)) often changes values (due to 
the whole factor/levels business).

... I also seem to remember read.table will let you pre-specify the data 
type of each column.

Krzysztof

Uwe Ligges wrote:
>  From ?factor:
> 
> The interpretation of a factor depends on both the codes and the 
> "levels" attribute. Be careful only to compare factors with the same set 
> of levels (in the same order). In particular, as.numeric applied to a 
> factor is meaningless, and may happen by implicit coercion. To transform 
> a factor f to its original numeric values, as.numeric(levels(f))[f] is 
> recommended and slightly more efficient than as.numeric(as.character(f)).
> 
> Uwe Ligges
> 
> 
> 
> ojal john owino wrote:
>> Dear Users,
>> I have a variable in my dataset which is of type factor. But it actually
>> contains numeric entries which like 5.735  4.759 ..... This is because 
>> the
>> data was read from a CSV file into R and this variable contained other
>> charaters which were not numeric. I have now dropped the records with the
>> characters which are not numeric for this variable and want to change 
>> it to
>> numeric srotage type.
>>
>> I have tried using as.numeric() function but it changes the values in the
>> variable to what I think are the ranks of the individual values of the
>> varible in the dataset. For example if 5.735 is the current content in 
>> the
>> field, then the new object created by as.numeric will contain a value 
>> like
>> 680 if the 5.735 was the highest value for the varible and the dataset 
>> had
>> 680 records.
>>
>>
>> How can I change the storage type without changing the contents of this
>> variable in this case?
>>
>> Thanks for your consideration.
>>
>>
>>
> 
> ______________________________________________
> R-help at r-project.org 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.
> 

-- 

-----------------------------------------------
Krzysztof Sakrejda-Leavitt

Organismic and Evolutionary Biology
University of Massachusetts, Amherst
319 Morrill Science Center South
611 N. Pleasant Street
Amherst, MA 01003

work #: 413-325-6555
email: sakrejda at nsm.umass.edu




More information about the R-help mailing list