简体   繁体   中英

Fast and efficient way to convert character matrix to numeric data frame?

The original data is

df1 = structure(c(3, 5, 0, 0, 5, 8, 6, 8, 0, 0), .Dim = c(5L, 2L))

The following command gives me a matrix of characters, and I need to convert it to a data frame of numerics.

install.packages("rcdd")
library("rcdd")
foo = makeV(points = d2q(df1))[,-c(1,2)]

What would be the fastest way, since it is used in a loop and meant to be repeated many times?

I don't think it is the best method, but i publish it, at least it while serve as reference to beat:

library("rcdd")
library(microbenchmark)

df1 = structure(c(3, 5, 0, 0, 5, 8, 6, 8, 0, 0), .Dim = c(5L, 2L))
foo = makeV(points = d2q(df1))[,-c(1,2)]

microbenchmark(
  data.frame(cbind(as.numeric(foo)[1:nrow(foo)],
                   as.numeric(foo)[-c(1:nrow(foo))]))
)

Mean time is 105.77 microseconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM