LHAndersen
LHAndersen

Reputation: 121

R reads European format numerical values as factors

In R3.1.2, I have uploaded a dataset. My numerical values, however, reads as Factors. By searching online, I found that this code should change my values from factors to numerical:

as.numeric(as.character(athene))

But when typing in this line, the following message occurs:

'NAs introduced by coercion'

and a lot of NAs occur on my screen. The same happens when I specify the specific factor that I would like to change

(as.numeric(as.character(MAG)).

When I look at my dataframe, it has not changed, and still looks like it did before. Nothing has happened:

str(athene)

'data.frame': 72920 obs. of 17 variables:

$ MAG: int 0 0 0 0 0 0 0 0 1 0 ...

$ PCR: int 0 1 0 0 0 0 0 0 0 0 ...

$ LO : int 1 1 1 1 1 1 1 1 1 1 ...

$ WAT: int 0 0 0 0 0 0 0 0 0 0 ...

$ SVE: int 0 0 0 0 0 0 0 0 0 0 ...

$ ARA: int 0 0 1 0 0 0 0 1 0 0 ...

$ GRA: int 0 0 0 1 0 1 1 0 0 1 ...

$ FOR: int 0 0 0 0 0 0 0 0 0 0 ...

$ B13: Factor w/ 233 levels "100,00","101,00",..: 1 233 13 15 5 10 8 6 7 14 ...

$ B12: Factor w/ 1538 levels "1000,00","1001,00",..: 959 960 1013 1024 977 993 974 966 966 1011 ...

$ MTP: Factor w/ 198 levels "0,00","1,00",..: 137 179 36 133 122 122 110 113 197 110 ...

$ MB6: Factor w/ 264 levels "-1,00","-10,00",..: 238 238 240 236 240 233 234 236 236 251 ...

$ URB: int 1 0 0 0 1 0 0 0 0 0 ...

$ BI1: int 12 12 12 12 12 12 12 12 12 12 ...

$ MHF: int 11 9 7 7 7 8 9 10 0 8 ...

$ BI4: int 3 3 3 3 3 3 3 3 3 3 ...

$ ALT: int 1 1 3 4 1 4 1 1 1 1 ...

If someone has any hits as to what I am doing wrong with regards to changing my factorial values into numerical ones, any help would be appreciated.

Upvotes: 1

Views: 216

Answers (1)

Stephan Kolassa
Stephan Kolassa

Reputation: 8267

R does not understand that we Europeans use "," as a decimal separator. Change all "," to "." using gsub() before coercing to numeric:

> B13 <- factor(rep(c("100,00","101,00","102,00"),1:3))
> B13
[1] 100,00 101,00 101,00 102,00 102,00 102,00
Levels: 100,00 101,00 102,00
> as.numeric(gsub(",",".",as.character(B13)))
[1] 100 101 101 102 102 102

Upvotes: 0

Related Questions