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 ..... Thanks for any help Elahe
changing factor to numbers without getting NAs
4 messages · Elahe chalabi, Duncan Murdoch, Marc Schwartz
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
On May 13, 2016, at 6:56 AM, ch.elahe via R-help <r-help at r-project.org> 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 ..... Thanks for any help Elahe
Hi,
First, how did you get the data into R?
I am going to guess that you used ?read.table or ?read.csv, which by default, will convert character values into factors (see the 'as.is' argument).
Second, by default, the decimal character in R is a period ('.') and you appear to be importing European values where the decimal character is a comma (','). Thus, take note of the 'dec' argument in read.table/read.csv and modify that to dec = "," in your function call.
The NA values are the result of converting character values that cannot be coerced to numeric due to the commas:
as.numeric("100,2")
[1] NA Warning message: NAs introduced by coercion
as.numeric("100.2")
[1] 100.2 Regards, Marc Schwartz
Thanks Duncan, This type.convert works fine for me and gives me TSTMean with decimal, but I want to add this result as a new column to my df as int or num, how can I do this? Thanks, Elahe
On Friday, May 13, 2016 2:15 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
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