[R] help on R programming.

Simon Blomberg Simon.Blomberg at anu.edu.au
Tue Jun 24 04:48:29 CEST 2003


> -----Original Message-----
> From: Murad Nayal [mailto:mn216 at columbia.edu]
> Sent: Tuesday, 24 June 2003 12:02 PM
> Cc: r-help at stat.math.ethz.ch
> Subject: [R] help on R programming.
> 
> 
> 
> 
> Hello all,
> 
> I am looking for books to help me gain a firmer grasp on the S/R
> programming language ,  programing / data structures etc. it 
> seems that
> for this purpose two books are typically recommended: 
> 
> Programming with Data: A Guide to the S Language, John M. Chambers and
> S Programming by Venables & Ripley.
> 
> - The Chambers book is published 1998. is it a bit dated at 
> this point.
> - is the Venables and Ripley's book a good source on the design and
> manipulation of data structures in R (it seems mostly focused on R
> extensions).
> - are there any other books, possibly published more 
> recently, that you
> could recommend.


I like Venables and Ripley's "S Programming". Of course it is focussed on R (S) extensions, since programming in S is often extending the language. I think there is enough information in there on data structures (don't have the book in front of me). S has very simple data structures.

> 
> 
> I also have a couple of particular programming questions:
> 
> -coming from a C++/java programming background I found that I 
> often end
> up in R with lists of objects (each constructed, in turn, as 
> a list, say
> list(x=x,y=y,z=z)). often, these individual objects have recursive
> 'attributes' so a matrix representation of this set of 
> objects is not an
> option. although a data.frame might be. I typically need to access
> certain attributes of these objects for plotting or analysis etc.
> however, I have not been able to come up with a clean way to do this?
> e.g.
> 
> object.list = list(o1=list(x=1,y=2,z=3), o2=list(x=11,y=22,z=33))
> 
> what I would like to do is say get a vector of x values for 
> the objects
> in object.list, but something like
> object.list[[1:length(object.list)]]$x, for example, returns NULL.
> 
> is there a better way to set up such an object list data 
> structure that
> will allow me to do this?

try sapply(object.list, function (element) element$x)

(I come from a Lisp background, so this seems natural to me. There may be better ways! *sighs with fond memories of mapcar and lambda*)
> 
> - what is the correct way to -remove- a component from a list. this
> seems to do the trick: list[[1]] = NULL, however, you'd think this
> should simply attach a NULL object at the first component position? 

You'd think that, but you would be wrong. :-). To add a NULL object to the front of a list:

c(list(NULL), object.list)

Cheers,

Simon.

Simon Blomberg, PhD
Depression & Anxiety Consumer Research Unit
Centre for Mental Health Research
Australian National University
http://www.anu.edu.au/cmhr/
Simon.Blomberg at anu.edu.au  +61 (2) 6125 3379




More information about the R-help mailing list