Skip to content
Back to formatted view

Raw Message

Message-ID: <5735C3F6.7060900@gmail.com>
Date: 2016-05-13T12:09:26Z
From: Duncan Murdoch
Subject: changing factor to numbers without getting NAs
In-Reply-To: <9850584.2788372.1463140568884.JavaMail.yahoo@mail.yahoo.com>

On 13/05/2016 7:56 AM, ch.elahe via R-help wrote:
> Hi all,
> I have a df which a part of this is:
>
>     TSTMax     :int 213 228 227 281
>     TSTMin     :int 149 167 158 176
>     TSTMean    :Factor w/94 levels "100,2" , "104,3" , ...
> I want to change the TSTMean into numeric but by using as.numeric(as.character(df$TSTMean)) I get too many NAs.
> Is there a way to change TSTMean into numeric without those NAs?
> I want TSTMean to be at the end like:
>
>     TSTMean  :int 100.2 104.3 .....

You appear to have a comma as the decimal marker, so you can use 
type.convert(as.character(df$TSTMean), dec = ",", as.is = TRUE) instead 
of as.numeric().

A simpler approach might be to avoid getting the factor in the first 
place; if you read this data using read.table, there is the dec option 
to recognize a comma as the decimal separator.

Duncan Murdoch