R中的物理常数

R中的物理常数,r,constants,units-of-measurement,R,Constants,Units Of Measurement,只是好奇-是否有包含物理常数值的包或数据集?我之所以这么问,是因为我现在输入的273.15(摄氏度到开尔文的转换)错误了好几次 封装marelac和电介质都有一些物理常数,但不是特别的物理常数marelac::convert\u T将进行温度转换。下载和解析此项应该相当容易: 看起来是固定格式,但这些数字三元组可能有点复杂。它没有你的273.15,因为根据某种定义,它可能不是一个“物理常数”。但是如果你需要屏蔽的太阳神来屏蔽质子,妈妈。比率,就在那里 [事实上,这个文件的格式不太好,而且有

只是好奇-是否有包含物理常数值的包或数据集?我之所以这么问,是因为我现在输入的
273.15
(摄氏度到开尔文的转换)错误了好几次

封装
marelac
电介质
都有一些物理常数,但不是特别的物理常数
marelac::convert\u T
将进行温度转换。

下载和解析此项应该相当容易:

  • 看起来是固定格式,但这些数字三元组可能有点复杂。它没有你的273.15,因为根据某种定义,它可能不是一个“物理常数”。但是如果你需要屏蔽的太阳神来屏蔽质子,妈妈。比率,就在那里

[事实上,这个文件的格式不太好,而且有几个常数甚至打破了近似的固定宽度格式。需要更巧妙的处理才能有用]

我可以推荐两个选项吗:


1) 如果您一直需要这些常数,您可以使用.First库,以
C2K的形式收集这些信息是一项相当大的任务,但marelac中提供了一些热力学常数:

> convert_T(0, "K")
  K       C       F
1 0 -273.15 -459.67
除了这些资源中的R资源外,NIST还有其他资源:

我承担了读取NIST常数表的任务,对其进行编辑,以便将其转换为数字是合理的,这是输入代码:

require(foreign)
fundconst <- read.fwf("allascii.txt", 
                      widths=diff(c(0,54, 77, 99, 111)),
                      header=FALSE, skip=12)
fundconst$V1 <- gsub("  ", "", fundconst[,1])  # strips some of hte white-space
fundconst$V2 <- as.numeric(gsub(" ", "", fundconst[,2]))
fundconst$V3 <- as.numeric(gsub(" ", "", fundconst[,3]))
names(fundconst) <- c("Quantity", "Value", "Uncertainty", "Unit")
dput(fundconst)
require(国外)

fundconst这里是NIST CODATA物理常数表的另一个解析。它与上面BondedDust的答案类似,但由于使用了
stringr
,所以(我认为)可读性稍微好一点


    #parsing NIST CODATA data for physical constants 
    require(stringr)    #simplifies regex usage syntax in R

    #web page with data
    link <- 'http://physics.nist.gov/cuu/Constants/Table/allascii.txt'

    allstr <- readLines(link)
    codata.str   <- allstr[11:345]  #data starts at line 11

    #at least 3 spaces separate columns
    codata.str2  <- str_replace_all(codata.str, '[ ]{3,100}', '\t') 

    #four cols in source table
    codata.mat   <- str_split_fixed(codata.str2, '\t', n=4) 

    #eliminate spaces separating every three decimal digits 
    codata.mat[,c(2,3)] <- str_replace_all(codata.mat[,c(2,3)], " ", "") 

    codata <- data.frame(quantity    = codata.mat[,1],
                            value       = as.numeric(codata.mat[,2]),
                            uncertainty = as.numeric(codata.mat[,3]),
                            units       = codata.mat[,4]
                )

#解析物理常数的NIST CODATA数据
require(stringr)#简化R中的正则表达式用法语法
#包含数据的网页

链接问题可能会有帮助,而且可能(几乎)是重复的。我们可以把它包装在CRAN上吗?r-forge上有一个“constants”包,它意味着做一些类似的事情,但我从来没有让它真正有用过。我认为这些数据,可能也被包装到data.frame的超类中,以便很好地显示单位和名称(使用打印方法等),可以为许多人提供有用的参考。我们还可以包括一些转换函数、分类、c、R等常用常量的别名,以及一个好的搜索函数。@baptiste:我认为这是个好主意。如果你已经开始一个常量包,我会很高兴成为一个贡献者。我的想法是围绕基本单位组织一个包:长度、时间、质量、电荷,然后包括衍生类别,如密度、浓度(计数/体积)、沸点、热容等@Dwin是的,我想知道捕捉单位特定属性的最佳格式是什么。面向对象的框架在这里可以工作吗?也许是参考课?一个数字会附加一个维度(单位),并且会有相关的方法在不同的单位系统之间进行转换。这个特定的数据集实际上并不包含很多关于单位转换的信息——例如,它不知道(除了在几个值的描述中隐含的)任何关于摄氏度的信息。所以,也许“物理常数”这个词对我的例子来说是错误的。@肯·威廉姆斯:同意。其中有一些值对转换有用,但除了粒子物理学家外,它们中的大多数似乎对任何人都不是特别有用。其中一些像阿伏伽德罗数,真空中的光速,重力的标准加速度,一个大气的值,还有一些看起来非常有用。这听起来是一个非常有趣的方法。您能否详细说明一下,如果存储了多个转换,我将如何让源代码调用特定的转换?另外,这是一种将vim设置为编辑器的非常简单的方法!如何将命令传递回R(我尝试了CTRL+R,但没有成功)?它的格式不是很好,但也不是很糟糕。至少它是固定宽度的。一旦定义了宽度,只需要一些
gsub
as.numeric
。。。简单明了,在数据清理练习的宏伟计划中!

    #parsing NIST CODATA data for physical constants 
    require(stringr)    #simplifies regex usage syntax in R

    #web page with data
    link <- 'http://physics.nist.gov/cuu/Constants/Table/allascii.txt'

    allstr <- readLines(link)
    codata.str   <- allstr[11:345]  #data starts at line 11

    #at least 3 spaces separate columns
    codata.str2  <- str_replace_all(codata.str, '[ ]{3,100}', '\t') 

    #four cols in source table
    codata.mat   <- str_split_fixed(codata.str2, '\t', n=4) 

    #eliminate spaces separating every three decimal digits 
    codata.mat[,c(2,3)] <- str_replace_all(codata.mat[,c(2,3)], " ", "") 

    codata <- data.frame(quantity    = codata.mat[,1],
                            value       = as.numeric(codata.mat[,2]),
                            uncertainty = as.numeric(codata.mat[,3]),
                            units       = codata.mat[,4]
                )

    > codata[str_detect(codata$quantity, 'electron mass'),]
                                      quantity        value uncertainty units
    2       alpha particle-electron mass ratio 7.294300e+03     2.9e-06      
    63            deuteron-electron mass ratio 3.670483e+03     1.5e-06      
    89                           electron mass 9.109383e-31     4.0e-38    kg
    90         electron mass energy equivalent 8.187105e-14     3.6e-21     J
    91  electron mass energy equivalent in MeV 5.109989e-01     1.1e-08   MeV
    92                      electron mass in u 5.485799e-04     2.2e-13     u
    130             helion-electron mass ratio 5.495885e+03     5.0e-06      
    195               muon-electron mass ratio 2.067683e+02     5.2e-06      
    223            neutron-electron mass ratio 1.838684e+03     1.1e-06      
    264             proton-electron mass ratio 1.836153e+03     7.5e-07      
    310                tau-electron mass ratio 3.477150e+03     3.1e-01      
    320             triton-electron mass ratio 5.496922e+03     5.0e-06

    > me.u < codata[str_detect(codata$quantity, 'electron mass in u'),'value']
    > me.u
    [1] 0.0005485799