How to convert unit of measurements of a value or a vector in R?

R ProgrammingServer Side ProgrammingProgramming

There are many units of measurements for a single object or item. For example, weight can be measured in milligrams, grams, kilograms, tons, oz, lbs, etc. Now suppose we have two variables that belong to the same unit of measurement as weight of a coca cola cans and weight of apple juice, if the weights given for both of these variables have different units like one having grams and the other having oz then we might want to convert one of them. This will help us to compare both the variables easily without conflicting the scale of measurements. Therefore, we can use conv_units function of measurements package in R.

Examples

> library(measurements)
> Converting_KG_to_Grams<-conv_unit(10,"kg","g")
> Converting_KG_to_Grams
[1] 10000
> Converting_KG_to_Grams_vector<-conv_unit(c(10,20,30),"kg","g")
> Converting_KG_to_Grams_vector
[1] 10000 20000 30000
> Converting_KG_to_MilliGrams_vector<-conv_unit(c(10,20,30),"kg","mg")
> Converting_KG_to_MilliGrams_vector
[1] 1e+07 2e+07 3e+07
> Converting_KG_to_oz_vector<-conv_unit(c(10,20,30),"kg","oz")
> Converting_KG_to_oz_vector
[1] 352.7396 705.4792 1058.2189
> Converting_KG_to_lbs_vector<-conv_unit(c(10,20,30),"kg","lbs")
> Converting_KG_to_lbs_vector
[1] 22.04622 44.09245 66.13867
> Converting_Minute_to_hour<-conv_unit(c(10,30,60),"min","hr")
> Converting_Minute_to_hour
[1] 0.1666667 0.5000000 1.0000000
> Converting_Minute_to_seconds<-conv_unit(c(10,30,60),"min","sec")
> Converting_Minute_to_seconds
[1] 600 1800 3600
> Converting_Minute_to_day<-conv_unit(c(10,30,60),"min","day")
> Converting_Minute_to_day
[1] 0.006944444 0.020833333 0.041666667
> Converting_day_to_hours<-conv_unit(c(10,30,60),"day","hr")
> Converting_day_to_hours
[1] 240 720 1440
> Converting_day_to_week<-conv_unit(c(10,30,60),"day","wk")
> Converting_day_to_week
[1] 1.428571 4.285714 8.571429
> Converting_centimeter_to_km<-conv_unit(c(10,30,60),"cm","km")
> Converting_centimeter_to_km
[1] 1e-04 3e-04 6e-04
> Converting_km_to_cm<-conv_unit(c(10,30,60),"km","cm")
> Converting_km_to_cm
[1] 1e+06 3e+06 6e+06
> Converting_celcius_to_K<-conv_unit(c(50,90,120),"C","K")
> Converting_celcius_to_K
[1] 323.15 363.15 393.15
> Converting_centimeter_to_inch<-conv_unit(c(50,90,120),"cm","inch")
> Converting_centimeter_to_inch
[1] 19.68504 35.43307 47.24409
raja
Published on 04-Sep-2020 08:39:36
Advertisements