如何转换 R 中值的或向量的计量单位?
单个对象或项目的计量单位有很多。例如,重量可以用毫克、克、千克、吨、盎司、磅等来衡量。现在假设我们有两个变量属于同一计量单位,如可乐罐的重量和苹果汁的重量,如果这两个变量的重量单位不同,比如一个用克而另一个用盎司,那么我们可能想要转换其中的一个。这将帮助我们轻松地比较这两个变量,而不会混淆测量刻度。因此,我们可以在 R 中使用 measurements 包的 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
广告