I have a dataframe with factor columns and I need to change them to numeric.
head(IBOV)
Date Price Open High Low Vol. Change..
1 Oct 18, 2019 104,784.74 105,011.71 105,464.25 104,524.97 2.84M -0.22%
2 Oct 17, 2019 105,015.77 105,388.63 105,891.19 104,826.61 4.19M -0.39%
3 Oct 16, 2019 105,422.80 104,485.87 105,462.07 103,521.08 4.51M 0.89%
4 Oct 15, 2019 104,489.56 104,298.53 105,047.62 104,052.48 4.09M 0.18%
5 Oct 14, 2019 104,301.58 103,833.59 104,304.85 103,438.47 2.99M 0.45%
6 Oct 11, 2019 103,831.92 101,818.60 104,380.89 101,818.60 4.35M 1.98%
I tried to change column 2 to 5 with this code:
IBOV[ ,2:5] <- as.numeric(gsub(",", "", IBOV[ ,2:5]))
But it returns them all as NA's, and this message:
IBOV[ ,2:5] <- as.numeric(gsub(",", "", IBOV[ ,2:5])) Warning message: NAs introduced by coercion
head(IBOV) Date Price Open High Low Vol. Change.. 1 Oct 18, 2019 NA NA NA NA 2.84M -0.22% 2 Oct 17, 2019 NA NA NA NA 4.19M -0.39% 3 Oct 16, 2019 NA NA NA NA 4.51M 0.89% 4 Oct 15, 2019 NA NA NA NA 4.09M 0.18% 5 Oct 14, 2019 NA NA NA NA 2.99M 0.45% 6 Oct 11, 2019 NA NA NA NA 4.35M 1.98%
What am I doing wrong?