The Fastest Way to Convert Numeric to Character in R

The fastest way to convert numeric to character in R

Since you've rounded a to finite precision, do the conversion of the unique values once, and look these up

f0 = formatC
f1 = function(x) { ux = unique(x); formatC(ux)[match(x, ux)] }

This gives identical results

> identical(f0(a), f1(a))
[1] TRUE

and is faster at least for the sample data set.

> microbenchmark(f0(a), f1(a))
Unit: milliseconds
expr min lq mean median uq max neval
f0(a) 46.05171 46.89991 47.33683 47.42225 47.58196 52.43244 100
f1(a) 10.97090 11.39974 11.48993 11.52598 11.58505 11.90506 100

(though is this efficiency really relevant in R?)

Fastest way to convert a list of character vectors to numeric in R

This is twice as fast on my system:

x <- paste(myT, collapse = "\n")
library(data.table)
DT <- fread(x)
newT2 <- c(t(DT))

How do I convert all numeric columns to character type in my dataframe?

In base R, we may either use one of the following i.e. loop over all the columns, create an if/else conditon to change it

dataframe[] <- lapply(dataframe, function(x) if(is.numeric(x)) 
as.character(x) else x)

Or create an index for numeric columns and loop only on those columns and assign

i1 <- sapply(dataframe, is.numeric)
dataframe[i1] <- lapply(dataframe[i1], as.character)

It may be more flexible in dplyr

library(dplyr)
dataframe <- dataframe %>%
mutate(across(where(is.numeric), as.character))

Converting numeric character vectors to numeric leaving non-numeric character vectors unchanged in data.frame

If this needs to be automatic, use type.convert which picks up the column type based on the values and return the appropriate type. Note that this may not work if there are any values in the column have mixed type i.e. c(1, 0, 'a') which remains as character because of the type precedence

type.convert(data, as.is = TRUE)

convert characters to numeric range in r?

It's difficult to know what you mean by the expected output. Taking you literally, you want the strings converted to calls:

 lapply(a, function(x) as.call(parse(text = x))[[1]])
#> [[1]]
#> 10:20
#>
#> [[2]]
#> 25:30

My guess is that this is not what you meant, and instead you want the strings to be evaluated, in which case you could do:

lapply(a, function(x) eval(parse(text = x)))
#> [[1]]
#> [1] 10 11 12 13 14 15 16 17 18 19 20
#>
#> [[2]]
#> [1] 25 26 27 28 29 30

However, this is fairly unsafe, since it would be easy to write a malicious string that caused problems on your computer if the data was not always checked for safety beforehand.

A safer way to do this would be

lapply(strsplit(a, ":"), function(x) x[1]:x[2])
#> [[1]]
#> [1] 10 11 12 13 14 15 16 17 18 19 20
#>
#> [[2]]
#> [1] 25 26 27 28 29 30

which will throw an error if the string isn't in the correct format rather than running arbitrary code on your computer.



Related Topics



Leave a reply



Submit