R: Convert from GiB, MiB, KiB to bytes

This function does the conversion, if there is no space between the number and the units:

convert_to_bytes <- function(value) {
    digits <- gsub("[^0-9.]", "", value)
    units <- gsub("[0-9.]", "", value)
    multiplier <- switch (units,
                          'GiB' = 1024^3,
                          'MiB' = 1024^2,
                          'KiB'= 1024,
                          'B' = 1)
    
    as.double(digits) * multiplier
}

Leave a Reply

Your email address will not be published.