R tutorial: Converting imperial to metric

Поделиться
HTML-код
  • Опубликовано: 21 авг 2024
  • This tutorial shows you how you can convert imperial measurements (height and weight) into metric.
    library(tidyverse)
    library(readxl)
    #Height
    df _-data.frame(height=c("6'4\"", "5'11\"", "5'6\"", "5'4\""))
    View(df)
    #Separate
    df_-separate(df, height, into = c("feet", "inches"), convert = TRUE)
    View(df)
    #Calculate
    df$Heightcm_- df$feet*30.48 + df$inches*2.54
    View(df)
    ##if doing as part of a bigger df
    plane _- read_excel("M:/BOOK/Logistic regression in R/Plane_seats.xlsx")
    View(plane)
    plane_-separate(plane, Height, into = c("feet", "inches"), convert = TRUE)
    View(plane)
    plane$Heightcm_- plane$feet*30.48 + plane$inches*2.54
    View(plane)
    ##Weight
    df _-data.frame(weight=c("6st 4lbs", "7st 8lbs", "8st 6lbs", "9st 3lbs"))
    View(df)
    #separate
    df_-separate(df, weight, into = c("stone", "pounds"))
    View(df)
    ##drop letters
    df$stone _- as.integer(gsub('[a-zA-Z]', '', df$stone)) #remove st
    df$pounds _- as.integer(gsub('[a-zA-Z]', '', df$pounds)) #remove lbs
    #work out lbs
    df$totalpounds_-df$pounds + df$stone*14
    View(df)
    #convert to KG
    df$kg_-df$totalpounds*0.453592
    View(df)

Комментарии •