Re: [R] unit attribute to list elements

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri, 28 Dec 2007 15:06:58 -0500

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.
>



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. Received on Fri 28 Dec 2007 - 20:26:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 28 Dec 2007 - 22:30:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.