Problem with converting a factor column to a numeric

Hi, i'm new in R, but getting to love it.

I want to change certain columns in my dataframe from factor to numeric, but when i do, R do not return my values, but instead other numbers.

My dataframe (dry15.soil_1) looks like this

It has the structure:

> str(dry15.soil_1)
'data.frame':	48 obs. of  12 variables:
 $ Trt            : Factor w/ 8 levels "C","R","S","SR",..: 1 2 7 8 6 3 5 4 2 8 ...
 $ ugNO3.N.g      : Factor w/ 15 levels "0,02","0,03",..: 12 15 3 1 14 1 2 3 1 2 ...
 $ ugNH4.N.g      : Factor w/ 31 levels "0,03","0,07",..: 17 4 6 13 21 6 9 22 3 3 ...
 $ ugTDN.g        : Factor w/ 48 levels "11,04","11,71",..: 37 41 32 7 9 30 35 6 33 29 ...
 $ ugTON.g        : Factor w/ 48 levels "10,81","11,29",..: 36 38 32 7 9 31 35 6 33 30 ...
 $ ugPO4.P.g      : Factor w/ 33 levels "0,09","0,11",..: 2 7 3 7 12 2 5 14 6 5 ...
 $ ugDOC.g        : Factor w/ 47 levels "109,20","114,70",..: 37 45 47 28 23 39 44 11 5 38 ...
 $ N...total.wt.  : Factor w/ 40 levels "0,27","0,33",..: 8 5 13 25 40 3 10 36 13 6 ...
 $ C...total.wt.  : Factor w/ 46 levels "10,00","10,10",..: 46 1 8 37 38 44 4 22 12 3 ...
 $ X.SOM          : Factor w/ 46 levels "13,20","16,30",..: 2 9 6 13 44 4 10 31 14 8 ...
 $ d15N....vs.AIR.: Factor w/ 33 levels "-0,20","-0,30",..: 3 14 12 23 18 5 4 6 8 32 ...
 $ d13C....vs.PDB.: Factor w/ 23 levels "-26,30","-26,40",..: 13 15 14 22 23 18 15 15 12 15 ...

I've tried different solutions:

> dry15.soil_1 [2:12]  <- as.numeric(as.character(dry15.soil_1 [2:12])) 

#Here i try to convert all colums at once (except column 1), but i only get NA values

Warning message:
NAs introduced by coercion 
> head(dry15.soil_1)
  Trt ugNO3.N.g ugNH4.N.g ugTDN.g ugTON.g ugPO4.P.g ugDOC.g N...total.wt. C...total.wt. X.SOM
1   C        NA        NA      NA      NA        NA      NA            NA            NA    NA
2   R        NA        NA      NA      NA        NA      NA            NA            NA    NA
3   W        NA        NA      NA      NA        NA      NA            NA            NA    NA
4  WR        NA        NA      NA      NA        NA      NA            NA            NA    NA
5 SWR        NA        NA      NA      NA        NA      NA            NA            NA    NA
6   S        NA        NA      NA      NA        NA      NA            NA            NA    NA
  d15N....vs.AIR. d13C....vs.PDB.
1              NA              NA
2              NA              NA
3              NA              NA
4              NA              NA
5              NA              NA
6              NA              NA


#Also i tried to convert just a single column (ugNO3.N.g) to see if if worked, but its returns wrong outputs
>dry15.soil_1$ugNO3.N.g <- as.numeric(dry15.soil_1$ugNO3.N.g)   

> head(dry15.soil_1$ugNO3.N.g)
[1] 12 15  3  1 14  1

 #same story for my third try with a single column (ugNO3.N.g)
> dry15.soil_1$ugNO3.N.g <- as.numeric(as.character(dry15.soil_1$ugNO3.N.g)) 

> head(dry15.soil_1$ugNO3.N.g)
[1] 12 15  3  1 14  1

#and same story again for 4th try.

> as.numeric(dry15.soil_1$ugNO3.N.g)
 [1] 12 15  3  1 14  1  2  3  1  2  2  2  1  4  4  3  4 11  1  2  3  4 13  1  2  3  4  6 10  4  7  7
[33]  2  8  6  3  6  8  9  7  5  4  6  4  4  4  4  8
1 Like

How are you reading the data in?

I see that you have entries like "0,02", etc, where I presume that the comma is meant as the decimal separator in many European countries.

You could try using read_csv2() from the readr package:

1 Like

Omg, sorry, yes, thats it. I thought i already converted from , to .

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.