mikeblazanin / gcplyr

gcplyr is an R package that facilitates wrangling and analysis of microbial growth curve data
https://mikeblazanin.github.io/gcplyr/
Other
29 stars 2 forks source link

Function from_excel: Improving code #136

Open discoleo opened 1 year ago

discoleo commented 1 year ago

Function from_excel

The code converts a string into ASCII codes and then combines the ASCII codes into a single number. There are alternative ways to implement this.

R Function charToRaw

charToRaw("ABC") # generates the ASCII Hex codes: 41 42 43 as.numeric(charToRaw("ABC")) # base 10 codes: 65 66 67

Note:

R Code

as.numletter = function(x) { lapply(x, function(s) { tmp = as.numeric(charToRaw(s)); tmp = ifelse(tmp >= 97, tmp - 96, tmp - 64); return(tmp); }); }

Example:

x = c("ABC", "abc", "Abc"); codes = as.numletter(x);

Encoding of Result

It is a different question, if the result should be kept as an array of integer-codes; or to combine the codes to a single number: but this latter solution requires continuous conversions back and force!

Note:

discoleo commented 1 year ago

Code in Other Functions

The function read_blocks (and possibly other functions) contains the following code: if(!is.null(startrow) & !all(is.numeric(startrow))) { startrow[!is.numeric(startrow)] <- from_excel(startrow[!is.numeric(startrow)]) startrow <- as.numeric(startrow) }

This code could be moved entirely to a helper function: as.numletter.list = function(x) { if( ! is.null(x) && ! all(is.numeric(x))) { isNotNum = ! is.numeric(x); x[isNotNum] = as.numletter(x[isNotNum]); x = as.numeric(x); # may be unnecessary; } return(x); }

Note: