[R] converting a character matrix into numeric....

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Apr 12 21:16:29 CEST 2023


Às 19:57 de 12/04/2023, akshay kulkarni escreveu:
> 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]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

Those NA come from trying to coerce character strings with "%" to numeric.

Try instead to first remove the unwanted characters.



mat <- matrix(c(0.1, 0.2, "5.77%", "-2.03%"), ncol = 2L)

mat[] <- apply(mat, 2, \(x) sub("%", "", x))
class(mat) <- "numeric"
mat
#>      [,1]  [,2]
#> [1,]  0.1  5.77
#> [2,]  0.2 -2.03


Hope this helps,

Rui Barradas



More information about the R-help mailing list