[R] unit attribute to list elements
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Dec 28 21:06:58 CET 2007
Is this what you want?
> Lines <- "cel 3.0 m/s
+ Z0 367 ohm
+ eps0 8.9e-12 F/m
+ "
> Constants.DF <- read.table(textConnection(Lines), as.is = TRUE)
> Constants <- as.list(Constants.DF[[2]])
> names(Constants) <- Constants.DF[[1]]
> for(i in seq_along(Constants)) comment(Constants[[i]]) <- Constants.DF[i, 3]
>
> # get value
> Constants$Z0
[1] 367
> # get comment
> comment(Constants$Z0)
[1] "ohm"
> # add another Constant
> Constants$e <- 2.7
> comment(Constants$e) <- "exp"
On Dec 28, 2007 2:33 PM, baptiste Auguié <ba208 at exeter.ac.uk> wrote:
> Hi,
>
> I've started my own (first) package, part of which consists in
> listing common physical constants (Planck's constant, the speed of
> light in vacuum, etc). I'm wondering what would be a good way of
> dealing with pairs of value/unit.
>
>
> > constants <- list( cel = 2.99792458e8 , #m/s
> > Z0 = 376.730313461, #ohm
> > eps0 = 8.854187817e-12,#F/m
> > mu0 = 4*pi*1e-7,#N/A^2
> > G = 6.67428e-11 # m^3 kg-1 s-2
> > )
>
>
> I thought I could include the unit in the names attribute of each
> element, as in :
>
> > names(constants$cel)<- " speed of light in vacuum [m.s^-1]"
>
>
> Writing this for every element is very redundant... Is there any way
> to access and set the name of each first level element of the list?
>
> > namesFirstLevelElements(constants)<- c(" speed of light in vacuum
> > [m.s^-1]",
> > "impedance of vacuum [some unit]",
> > ...)
>
>
> Quite possibly, I'm completely on the wring track;
>
> - maybe such a package already exists
>
> - a custom class or structure would be more appropriate? I don't
> really know how to deal with classes, though, and I'd like to keep
> the access to the constants' values as direct and generic as possible.
>
> Many thanks in advance,
>
> baptiste
>
> _____________________________
>
> Baptiste Auguié
>
> Physics Department
> University of Exeter
> Stocker Road,
> Exeter, Devon,
> EX4 4QL, UK
>
> Phone: +44 1392 264187
>
> http://newton.ex.ac.uk/research/emag
> http://projects.ex.ac.uk/atto
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list