R 中的单位转换

发布于 2024-12-01 17:09:38 字数 75 浏览 2 评论 0原文

我想在 R 中将英制单位转换为公制单位,反之亦然。我该怎么做?

如果当前没有方法可以做到这一点,我怎样才能创建一个包呢?

I would like to convert from imperial units to metric and vice versa in R. How do I go about doing that?

If there is no current way of doing that, how can I create a package that would?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

李白 2024-12-08 17:09:38

编辑:现在有一个百科全书式的单位包: NISTunits

单位的性质可能会影响存在或不存在。不幸的是,总的来说,我认为@gsk3是正确的。 'Hmisc'、'gdata' 和 'marelac' 包中有一些功能:

设置单位属性(不仅仅是时间对象):
http://finzi.psych.upenn.edu/R/ Library/Hmisc/html/units.html

医疗:
http://finzi.psych.upenn.edu/R/ Library/gdata/html/ConvertMedUnits.html

'marelac' 索引(主要针对海洋学)
http://finzi.psych.upenn.edu/R/ Library/marelac/html/00Index.html

温度:
http://finzi.psych.upenn.edu/R/ Library/marelac/html/convert_T.html

气压:
http://finzi.psych.upenn.edu/R/ Library/marelac/html/convert_p.html

@Brian Diggs 在回答类似问题时提到了包“dielectric”:
链接到包“dielectric”中的“constants”帮助页面

我认为这些将提供这样做的示例,并且函数 package.sculpture 应该有助于包创建的机制。我认为这个包将是一个受欢迎的补充。

更新:Gabor Grothendieck 在 2012 年 7 月 23 日向 rhelp 提出的类似问题中提到了“udunits2”软件包。它似乎需要安装同名的独立操作系统特定软件包。如果没有这样的安装,您会收到一条相当无用的错误消息:

> ud.convert(1, "miles", "km")  
Error in ud.convert(x, "miles", "km") : 
  Units miles and km are not convertible

Edit: There is now an encyclopedic units package: NISTunits

The nature of the units may affect the presence or absence. In general I think, unfortunately, that @gsk3 is correct. There are some function in the 'Hmisc', 'gdata', and 'marelac' packages:

Setting a units attribute (for more than just time objects):
http://finzi.psych.upenn.edu/R/library/Hmisc/html/units.html

Medical:
http://finzi.psych.upenn.edu/R/library/gdata/html/ConvertMedUnits.html

'marelac' Index (mostly specialized to oceanography)
http://finzi.psych.upenn.edu/R/library/marelac/html/00Index.html

Temperature:
http://finzi.psych.upenn.edu/R/library/marelac/html/convert_T.html

Barometric:
http://finzi.psych.upenn.edu/R/library/marelac/html/convert_p.html

Package "dielectric" was mentioned by @Brian Diggs in an answer to a similar question:
Link to 'constants' help page in package 'dielectric'

I think those will provide examples of doing so and the function package.skeleton should help with the mechanics of package creation. I think the package would be a welcome addition.

Update: The 'udunits2' package was mentioned by Gabor Grothendieck in a similar question to rhelp July 23, 2012. It appears to require installation of a stand-alone OS-specific package of the same name. Without such installation you get a rather unhelpful error message:

> ud.convert(1, "miles", "km")  
Error in ud.convert(x, "miles", "km") : 
  Units miles and km are not convertible
束缚m 2024-12-08 17:09:38

我知道这已经很晚了,但是 measurements 包有一个 conv_unit() 函数,它可能就是您正在寻找的。您输入您拥有的英制值、要转换的单位(例如“ft”)以及要转换为的单位(例如“km”)。它有多种不同的尺寸(不仅仅是长度)。

I know this is very late, but the package measurements has a function conv_unit() that may be what you're looking for. You enter the imperial value you have, what unit you're converting from (e.g. 'ft') and what you want to convert to (e.g. 'km'). It has a variety of different dimensions (not just length).

累赘 2024-12-08 17:09:38

今天有一个名为 units 的“新”R 软件包,它是基于不久前发布的 udunits2 R 软件包构建的。

查看:https: //cran.r-project.org/web/packages/units/vignettes/units.html#setting-units-unit-conversion

library(units)
(spd1 = 1:5 * with(ud_units, m/s))
##Units: m/s
##[1] 1 2 3 4 5

(spd2 = 1:5 * with(ud_units, km/h))
#Units: km/h
#[1] 1 2 3 4 5

spd1 + spd2                   # automatic conversion
#Units: m/s
#[1] 1.277778 2.555556 3.833333 5.111111 6.388889

spd1 * spd2                   # unit derivation
#Units: km*m/h/s
#[1]  1  4  9 16 25

spd1 * 10 * with(ud_units, s) # unit simplification
#Units: m
#[1] 10 20 30 40 50

spd1 + 10 * with(ud_units, s) # error checking
#Error in `units<-.units`(`*tmp*`, value = list(numerator = "m", denominator = "s")) : 
cannot convert s into m/s

There is a "new" R package called units today, that was built upon the udunits2 R package, which is available a while ago.

Have a look to: https://cran.r-project.org/web/packages/units/vignettes/units.html#setting-units-unit-conversion

library(units)
(spd1 = 1:5 * with(ud_units, m/s))
##Units: m/s
##[1] 1 2 3 4 5

(spd2 = 1:5 * with(ud_units, km/h))
#Units: km/h
#[1] 1 2 3 4 5

spd1 + spd2                   # automatic conversion
#Units: m/s
#[1] 1.277778 2.555556 3.833333 5.111111 6.388889

spd1 * spd2                   # unit derivation
#Units: km*m/h/s
#[1]  1  4  9 16 25

spd1 * 10 * with(ud_units, s) # unit simplification
#Units: m
#[1] 10 20 30 40 50

spd1 + 10 * with(ud_units, s) # error checking
#Error in `units<-.units`(`*tmp*`, value = list(numerator = "m", denominator = "s")) : 
cannot convert s into m/s
暮年 2024-12-08 17:09:38

grid 包中有 unit()convertUnit() 函数用于指定不同的长度和尺寸单位。这可能会满足您的要求,或者如果没有的话,也可以为您提供一个起点。

There is the unit() and convertUnit() functions in the grid package for specifying different length and dimension units. That may do what you want, or give you a place to start if not.

清音悠歌 2024-12-08 17:09:38

udunits2 包就是这样做的。它包含强大的 UDUNITS 库

udunits2::ud.convert(1, "mi", "km")
## [1] 1.609344

最重要的是, units 包(正在进行中)旨在提供一个类型安全的系统带单位的算术:

with(ud_units, 1 * mi + 2 * km)
## 2.242742 mi
with(ud_units, 100 * km / (2 * h))
## 50 km/h
with(ud_units, 1 * mi + 2 * lb)
## Error: cannot convert lb into mi

The udunits2 package does just that. It wraps the powerful UDUNITS library:

udunits2::ud.convert(1, "mi", "km")
## [1] 1.609344

On top of that, the units package (work in progress) aims at providing a type-safe system for doing arithmetics with units:

with(ud_units, 1 * mi + 2 * km)
## 2.242742 mi
with(ud_units, 100 * km / (2 * h))
## 50 km/h
with(ud_units, 1 * mi + 2 * lb)
## Error: cannot convert lb into mi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文