我有一列的内存大小如Mb和kb。我想将所有值转换为R中的Mb。
$ Size : Factor w/ 462 levels "","1.0M","1.1M","0.98k"..
答案 0 :(得分:1)
此答案假定您只有千字节和兆字节。这是一个可行的基础R解决方案:
input <- c("Varies with device", "9.4M", "201k", "0.98k")
output <- sapply(input, function(x) {
ifelse(grepl("k$", x), paste0(0.001*as.numeric(sub("(\\d+(?:\\.\\d+)?)k", "\\1", x)), "M"), x)
})
output
[1] "Varies with device", "9.4M", "0.201M", "0.00098M"
此解决方案使用grepl
查找所有匹配的千字节条目。对于此类条目,它将提取数字分量,转换为数字,然后按比例缩小一千以转换为兆字节。
答案 1 :(得分:0)
有些笨拙,但这是可行的。如果GB或G稍后出现,则也应该起作用:
library(tidyverse)
library(stringr)
SampleData <- c("19M", "14M", "24M", "Varies with device", "1.1M", "9.4M", "Varies with device", "201k", "360k")
data <- tibble(strings = SampleData)
data %>%
mutate(number = as.double(str_extract(strings, ".+(?=[:alpha:])")), #extract the numeric portion and make it a double variable
letters = str_extract(strings, "[:alpha:]+"),
number = if_else(letters == "k", number/1000, number),
combined = paste0(number, "M"),
strings = if_else(is.na(number), strings, combined)) %>%
select(strings)
# A tibble: 9 x 1
strings
<chr>
1 19M
2 14M
3 24M
4 Varies with device
5 1.1M
6 9.4M
7 Varies with device
8 0.201k
9 0.36k