9

Is there a standard function in R to convert strings representing numbers of bytes such as

  • 11855276K
  • 113M
  • 2.40G

to integer numbers of bytes?

I came across humanReadable in the package gdata, but this does the conversion the other way round. I know that I can parse the string and then do the maths myself, but I wondered whether something exists already.

daroczig
  • 28,004
  • 7
  • 90
  • 124
loris
  • 450
  • 8
  • 20

2 Answers2

8

A simple function to do this:

x <- c("11855276K", "113M", "2.40G", "1234")

convb <- function(x){
  ptn <- "(\\d*(.\\d+)*)(.*)"
  num  <- as.numeric(sub(ptn, "\\1", x))
  unit <- sub(ptn, "\\3", x)             
  unit[unit==""] <- "1" 

  mult <- c("1"=1, "K"=1024, "M"=1024^2, "G"=1024^3)
  num * unname(mult[unit])
}

convb(x)
[1] 12139802624   118489088  2576980378        1234

You may want to add additional units and conversions, e.g. terabytes.

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • 1
    OK, that's the parsing-the-string-then-doing-the-maths-myself approach. My question was really whether there is an existing function in a CRAN package. – loris Jun 06 '12 at 13:28
  • Add this as a first line to avoid problems with , or . : x = gsub(",",".",x) – Gildas Sep 18 '18 at 15:36
1

The excellent fs package has the function fs_bytes that can handle this in both directions.

## Create some number of bytes of differing units, and "convert" to fs_bytes
sizes <- c("11855276K", "113M", "2.40G", "1234") |>
  fs::fs_bytes()

## It picks sensible human-readable default units for printing
sizes
## 11.31G 113M   2.4G   1.21K

## The data is stored as numeric
str(sizes)
## 'fs_bytes' num [1:4] 11.31G 113M 2.4G 1.21K

## As a result, you can perform numeric operations on them
sum(sizes)
## 13.8G

## Use as.numeric to print the values as bytes
as.numeric(sizes)
## [1] 12139802624   118489088  2576980378        1234
dnlbrky
  • 9,396
  • 2
  • 51
  • 64