Re: [R] Listing function

2007-03-26 Thread Michael T. Mader
Lst - list()
Lst[[1]] - list(name=Fred, wife=Mary, no.children=3, 
cild.ages=c(4,7,9))
Lst[[2]] - list(name=Barney, wife=Liz, no.children=2, cild.ages=c(3,5))

I.e. a list of lists

Regards

Michael

Schmitt, Corinna wrote:
 Hallo,
 
 I build a list by the following way:
 
 Lst = list(name=Fred, wife=Mary, no.children=3, cild.ages=c(4,7,9))
 
 I know how I can extract the information one by one. But now I want to
 add a new entry which looks like
 
 name=Barney, wife=Liz, no.children=2, cild.ages=c(3,5)
 
 How can I add this information to Lst without overwriting the first
 entry?
 How can I then extract the corresponding information if I have both
 entries in Lst?
 
 Thanks for helping,
 
 Corinna
 
 __
 R-help@stat.math.ethz.ch 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.
 

-- 
Michael T. Mader
Institute of Stem Cell Research
GSF - National Research Center for Environment and Health
Ingolstaedter Landstrasse 1
D-85764 Neuherberg
0049-89-3187-3683

Program testing can be quite effective for showing the presence of bugs, 
but is hopelessly inadequate for showing their absence.
E. W. Dijkstra

__
R-help@stat.math.ethz.ch 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.


Re: [R] Listing function

2007-03-26 Thread Dimitris Rizopoulos
maybe it'd be better to use a data.frame(), e.g.,

dat - data.frame(name = I(Fred), wife = I(Mary), no.children = 3,
cild.ages1 = 4, cild.ages2 = 7, cild.ages3 = 9)
##
new.info - c(name = Barney, wife = Liz, no.children=2,
cild.ages1 = 3, cild.ages2 = 5, cild.ages3 = NA)

rbind(dat, new.info)


I hope it helps.

Best,
Dimitris


Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
 http://www.student.kuleuven.be/~m0390867/dimitris.htm


- Original Message - 
From: Schmitt, Corinna [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Sent: Monday, March 26, 2007 3:26 PM
Subject: [R] Listing function


 Hallo,

 I build a list by the following way:

 Lst = list(name=Fred, wife=Mary, no.children=3, 
 cild.ages=c(4,7,9))

 I know how I can extract the information one by one. But now I want 
 to
 add a new entry which looks like

 name=Barney, wife=Liz, no.children=2, cild.ages=c(3,5)

 How can I add this information to Lst without overwriting the first
 entry?
 How can I then extract the corresponding information if I have both
 entries in Lst?

 Thanks for helping,

 Corinna

 __
 R-help@stat.math.ethz.ch 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.
 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
R-help@stat.math.ethz.ch 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.


Re: [R] Listing function

2007-03-26 Thread Petr Klasterecky
Use c() to extend your list creating a list of lists:

Lst = list(name=Fred, wife=Mary, no.children=3, cild.ages=c(4,7,9))

Lst - c(list(Lst), list(list(name=Barney, wife=Liz, no.children=2, 
cild.ages=c(3,5

You can give names to the components:

Lst - c(Fred=list(Lst), Barney=list(list(name=Barney, wife=Liz, 
no.children=2, cild.ages=c(3,5

See ?c for more information. You can also use a data.frame if this looks 
too messy.
Petr

Schmitt, Corinna napsal(a):
 Hallo,
 
 I build a list by the following way:
 
 Lst = list(name=Fred, wife=Mary, no.children=3, cild.ages=c(4,7,9))
 
 I know how I can extract the information one by one. But now I want to
 add a new entry which looks like
 
 name=Barney, wife=Liz, no.children=2, cild.ages=c(3,5)
 
 How can I add this information to Lst without overwriting the first
 entry?
 How can I then extract the corresponding information if I have both
 entries in Lst?
 
 Thanks for helping,
 
 Corinna
 
 __
 R-help@stat.math.ethz.ch 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.
 

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic

__
R-help@stat.math.ethz.ch 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.


Re: [R] Listing function displayed as a table

2007-03-26 Thread Adaikalavan Ramasamy
Something ugly like this?

Lst - list()
Lst[[1]] - list(name=Fred, wife=Mary, no.children=3, 
child.ages=c(4,7,9))
Lst[[2]] - list(name=Barney, wife=Liz, no.children=2, 
child.ages=c(3,5))

cbind( do.call(rbind, as.list(Lst))[ ,-4],
child.ages=sapply( Lst, function(myli)
  paste(myli$child.ages, collapse=,) ))


Why don't you just save the data in a dataframe instead of a list to 
begin with ? The only variable I can see that has multiple values is 
child.ages. Or create one row per record as in most databases. The 
choice depends on your input.

  df - rbind( c(Fred, Mary, 4), c(Fred, Mary, 7),
   c(Fred, Mary, 9), c(Barney, Liz, 3),
   c(Barney, Liz, 5) )
  df - data.frame(df)
  colnames(df) - c(Father, Mother, Child.Age)
  df$Child.Age - as.numeric(as.character(df$Child.Age))

  parents - paste( df$Father, df$Mother, sep=+ )

  getstats - function(x) c( values=paste(x, collapse=,),
  mean=round(mean(x),2), youngest=min(x), oldest=max(x) )

  do.call( rbind, tapply( df$Child.Age, parents, getstats ) )

  values  mean   youngest oldest
  Barney+Liz 3,5   43  5
  Fred+Mary  4,7,9 6.67 4  9


Regards, Adai



Schmitt, Corinna wrote:
 Hallo,
 good idea it is working. A new question appears: How can I display the 
 entries in a table like
 
  name   wife  no.children  child.ages
 FredMary3   4,7,9
 Barney  Liz 2   3,5
 
 Thanks, Corinna
 
 
 -Ursprüngliche Nachricht-
 Von: Michael T. Mader [mailto:[EMAIL PROTECTED] 
 Gesendet: Montag, 26. März 2007 15:32
 An: Schmitt, Corinna; r-help@stat.math.ethz.ch
 Betreff: Re: [R] Listing function
 
 Lst - list()
 Lst[[1]] - list(name=Fred, wife=Mary, no.children=3, 
 cild.ages=c(4,7,9))
 Lst[[2]] - list(name=Barney, wife=Liz, no.children=2, cild.ages=c(3,5))
 
 I.e. a list of lists
 
 Regards
 
 Michael
 
 Schmitt, Corinna wrote:
 Hallo,

 I build a list by the following way:

 Lst = list(name=Fred, wife=Mary, no.children=3, cild.ages=c(4,7,9))

 I know how I can extract the information one by one. But now I want to
 add a new entry which looks like

 name=Barney, wife=Liz, no.children=2, cild.ages=c(3,5)

 How can I add this information to Lst without overwriting the first
 entry?
 How can I then extract the corresponding information if I have both
 entries in Lst?

 Thanks for helping,

 Corinna

 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.