[R] converting a character matrix into numeric....
akshay kulkarni
@k@h@y_e4 @end|ng |rom hotm@||@com
Wed Apr 12 20:57:48 CEST 2023
Dear members,
I have an xts object:
> head(INFYTX)
INFY Historical Data INFY Historical Data.1 INFY Historical Data.2
2003-04-16 "47.26" "44.28" "47.56"
2003-04-17 "46.30" "44.92" "46.53"
2003-04-21 "45.82" "47.27" "47.50"
2003-04-22 "45.62" "46.06" "46.16"
2003-04-23 "45.05" "46.28" "46.50"
2003-04-24 "45.28" "44.80" "46.84"
INFY Historical Data.3 INFY Historical Data.4
2003-04-16 "44.28" "1805267" "5.77%"
2003-04-17 "44.06" "1536300" "-2.03%"
2003-04-21 "45.63" "887774" "-1.04%"
2003-04-22 "44.73" "944036" "-0.44%"
2003-04-23 "44.77" "759898" "-1.25%"
2003-04-24 "44.53" "1185402" "0.51%"
But it is populated with character values and I want to convert them to numeric. THe following code doesn't work:
> head(coredata(INFYTX))
INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 INFY Historical Data.3
[1,] "47.26" "44.28" "47.56" "44.28"
[2,] "46.30" "44.92" "46.53" "44.06"
[3,] "45.82" "47.27" "47.50" "45.63"
[4,] "45.62" "46.06" "46.16" "44.73"
[5,] "45.05" "46.28" "46.50" "44.77"
[6,] "45.28" "44.80" "46.84" "44.53"
INFY Historical Data.4
[1,] "1805267" "5.77%"
[2,] "1536300" "-2.03%"
[3,] "887774" "-1.04%"
[4,] "944036" "-0.44%"
[5,] "759898" "-1.25%"
[6,] "1185402" "0.51%"
> class(coredata(INFYTX))
[1] "matrix" "array"
> class(coredata(INFYTX)) <- "numeric"
Warning message:
In class(coredata(INFYTX)) <- "numeric" : NAs introduced by coercion
> class(coredata(INFYTX))
[1] "matrix" "array"
> head(coredata(INFYTX))
INFY Historical Data INFY Historical Data.1 INFY Historical Data.2 INFY Historical Data.3
[1,] "47.26" "44.28" "47.56" "44.28"
[2,] "46.3" "44.92" "46.53" "44.06"
[3,] "45.82" "47.27" "47.5" "45.63"
[4,] "45.62" "46.06" "46.16" "44.73"
[5,] "45.05" "46.28" "46.5" "44.77"
[6,] "45.28" "44.8" "46.84" "44.53"
INFY Historical Data.4
[1,] "1805267" NA
[2,] "1536300" NA
[3,] "887774" NA
[4,] "944036" NA
[5,] "759898" NA
[6,] "1185402" NA
Why is the coredata matrix not changing to numeric when the class is changed to numeric? How else to convert coredata into numeric?
THanking you,
Yours sincerely,
AKSHAY M KULKARNI
[[alternative HTML version deleted]]
More information about the R-help
mailing list