Below is dataframe df1 of which I want to convert column "V2" from factor format to numeric without changing the current values (0 ; 0 ; 8,5 ; 3).
df1=
V1 V2 V3 X2 X3
4470 2010-03-28 0 A 21.53675 0
4471 2010-03-29 0 A 19.21611 0
4472 2010-03-30 8,5 A 21.54541 0
4473 2010-03-31 3 A NA NA
Since column "V2" is in factor format I first convert it to character format:
df1[,2]=as.character(df1[,2])
Then I try to convert "V2" to numeric format:
df1[,2]=as.numeric(df1[,2])
Leading to this R message:
Warning message: NAs introduced by coercion
And the dataframe below where df[3,2]
has changed into "NA" instead of remaining "8,5"..
V1 V2 V3 X2 X3
4470 2010-03-28 0 A 21.53675 0
4471 2010-03-29 0 A 19.21611 0
4472 2010-03-30 NA A 21.54541 0
4473 2010-03-31 3 A NA NA
It might have to do with the fact that 8,5 is not a whole number. Still I do not know how to solve this problem. Help would be much appreciated!