[R] Naming dimnames in an array using the results of an expression

Dr Bob Phillips bob.phillips at york.ac.uk
Wed May 23 10:38:28 CEST 2012


Wonderful!

Thanks very much for this: the step I'd clearly missed was to

1. put the rownames/colnames in a list object
2. *name* these with the titles separately ... allowing the expression
to be evaluated

names(dn)<-c(names(result[[1]]),names(result[[1]][2]))

3. apply this to the array

Ta v much - Bob

BTW - result contains a 1st order(?) list of rules, evaluated for a 2nd
order list of outcomes, hence the asymmetry of result[[1]] for rule-name
and result[[1]][2] of outcome under study



On 23/05/2012 00:14, R. Michael Weylandt wrote:
> 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.

-- 


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



More information about the R-help mailing list