R中是否有标准函数来转换表示字节数的字符串,例如
到整数个字节?
我humanReadable在包gdata中遇到过,但这反过来又转换了.我知道我可以解析字符串,然后自己做数学,但我想知道是否已存在某些东西.
一个简单的功能:
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
Run Code Online (Sandbox Code Playgroud)
您可能需要添加其他单位和转换,例如太字节.
优秀的fs 软件包fs_bytes具有可以双向处理此问题的功能。
## 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
Run Code Online (Sandbox Code Playgroud)