Dr Bob Phillips
2012-May-22 17:22 UTC
[R] Naming dimnames in an array using the results of an expression
dear all
i'm struggling with naming in an array
diag.data is one of a series of 2x2 diagnostic testing arrays, with
'Outcome' columns (true/false) and 'Test' rows (High-risk,
Low-risk),
drawn from a larger list object of 'results'
i can hard-code the names of the array using dimnames;
diag.data<-array(c(19,2,125,50),c(2,2)) #example to run - actually comes
out with rownames/colnames already attached
dimnames(diag.data)<-list(Rule=c("LR","HR"),Outcome=c("FALSE","TRUE"))
but i would really love to be able to soft-code it, using the different
'rule' names and slightly different 'outcomes' as extracted from
the
main list of results, rather than just 'rule' and 'outcome' e.g.
dimnames(diag.data)<-list(names(result[1])=rownames(diag.data),names(result[[1]][2])=colnames(diag.data))
this doesn't work
i understand that the dimnames command is NOT executing the function
'names' and giving me the result of this, and have struggled and failed
to use the 'eval' expression with alternative environments (which i
don't understand ...)
can anyone assist?
cheers - bob
--
Dr Bob Phillips
MRC Research Training Fellow
CRD
University of York
York
YO10 5DD
t: +44 (0)1904 321099
f: +44 (0)1904 321041
e: bob.phillips at york.ac.uk
www.york.ac.uk/inst/crd
www.crd.york.ac.uk/prospero
CRD is part of the National Institute for Health Research and is a
department of the University of York.
EMAIL DISCLAIMER: http://www.york.ac.uk/docs/disclaimer/email.htm
R. Michael Weylandt
2012-May-22 23:14 UTC
[R] Naming dimnames in an array using the results of an expression
I think you may be making this too hard, but before I venture a guess,
what does your "result" object look like -- can you dput() it for us?
Looking at the line you tried:
dimnames(diag.data)<-list(names(result[1])=rownames(diag.data),names(result[[1]][2])=colnames(diag.data))
there seems to be some asymmetry in how you have the row and column
names -- is that intentional?
You might be able to try something like this:
dn <- list(c("LR","HR"),
c("FALSE","TRUE"))
names(dn) <- c("Rule","Outcome")
attr(diag.data, "dimnames") <- dn
Best,
Michael
On Tue, May 22, 2012 at 1:22 PM, Dr Bob Phillips
<bob.phillips at york.ac.uk> wrote:>
> dear all
>
> i'm struggling with naming in an array
>
> diag.data is one of a series of 2x2 diagnostic testing arrays, with
> 'Outcome' columns (true/false) and 'Test' rows (High-risk,
Low-risk),
> drawn from a larger list object of 'results'
>
> i can hard-code the names of the array using dimnames;
>
> diag.data<-array(c(19,2,125,50),c(2,2)) #example to run - actually comes
> out with rownames/colnames already attached
>
>
dimnames(diag.data)<-list(Rule=c("LR","HR"),Outcome=c("FALSE","TRUE"))
>
> but i would really love to be able to soft-code it, using the different
> 'rule' names and slightly different 'outcomes' as extracted
from the
> main list of results, rather than just 'rule' and 'outcome'
e.g.
>
>
dimnames(diag.data)<-list(names(result[1])=rownames(diag.data),names(result[[1]][2])=colnames(diag.data))
>
> this doesn't work
>
> i understand that the dimnames command is NOT executing the function
> 'names' and giving me the result of this, and have struggled and
failed
> to use the 'eval' expression with alternative environments (which i
> don't understand ...)
>
> can anyone assist?
>
> cheers - bob
>
>
> --
>
>
> Dr Bob Phillips
> MRC Research Training Fellow
> CRD
> University of York
> York
> YO10 5DD
> t: ?+44 (0)1904 321099
> f: ?+44 (0)1904 321041
> e: ?bob.phillips at york.ac.uk
> www.york.ac.uk/inst/crd
> www.crd.york.ac.uk/prospero
>
> CRD is part of the National Institute for Health Research and is a
> department of the University of York.
>
> EMAIL DISCLAIMER: http://www.york.ac.uk/docs/disclaimer/email.htm
>
> ______________________________________________
> 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.
Maybe Matching Threads
- Incidence Function Model in R help
- [LLVMdev] clang looking for gold plugin when used with '-emit-llvm' option
- [LLVMdev] clang looking for gold plugin when used with '-emit-llvm' option
- [LLVMdev] clang looking for gold plugin when used with '-emit-llvm' option
- [LLVMdev] clang looking for gold plugin when used with '-emit-llvm' option