如何在R中转换值或向量的度量单位?

单个对象或物品有许多度量单位。例如,重量可以毫克,克,公斤,吨,盎司,磅等进行度量。现在假设我们有两个变量属于同一个计量单位,即可口可乐罐的重量和苹果汁的重量,如果这两个变量的权重具有不同的单位,例如一个单位为g,另一个单位为oz,那么我们可能要转换其中一个。这将有助于我们轻松比较这两个变量,而不会影响测量范围。因此,我们可以在R中使用测量包的conv_units函数。

例子

> 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