Adarsh Pawar
Adarsh Pawar

Reputation: 746

How do I convert column having memory size Ex. MB & KB and I want all of the values in MB?

I'm having a column with memory sizes like in Mb and kb. I want to convert all the values to Mb in R.

$ Size : Factor w/ 462 levels "","1.0M","1.1M","0.98k"..

Upvotes: 2

Views: 309

Answers (2)

Ben G
Ben G

Reputation: 4338

A little clunky, but this works. Should also work if GB or G appears later on:

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   

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522396

This answer assumes that you only have kilobytes and megabytes. Here is a working base R solution:

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"

This solution uses grepl to find all matching kilobyte entries. For such entries, it extracts the numerical component, casts to numeric, and then scales down by a thousand to convert to megabytes.

Upvotes: 1

Related Questions