[R] why "object 'x' not found"?

William Dunlap wdunlap at tibco.com
Thu Feb 7 22:21:53 CET 2013


>> Notice that the '[[' function is superior in every way to the '$' function. 
> I'm curious to know whether you can point to some consolidated comparison?

Two problems are that List$name allows abbreviations of name and does not allow
variable names.

With List$name you get:
  > List <- list(Eleven=11, Twelve="xii")
  > str(List)
  List of 2
   $ Eleven: num 11
   $ Twelve: chr "xii"
  > List$E # $ accepts abbreviations, which might seem nice
  [1] 11
  > List$E <- List$E + 100 # but can bite you in replacements
  > str(List) # Eleven not incremented, new E created
  List of 3
   $ Eleven: num 11
   $ Twelve: chr "xii"
   $ E     : num 111
  > nm <- "Twelve" # want to use component named variable
  > List$nm  # no component named "nm"
  NULL
  > eval(parse(text=paste0("List", "$", nm))) # ugly dangerous hack
  [1] "xii"
  > eval(call("$", quote(List), nm)) # ugly safe hack
  [1] "xii"

With List[["name"]] syntax you get
  > List <- list(Eleven=11, Twelve="xii")
  > List[["E"]]
  NULL
  > List[["E"]] <- List[["E"]] + 100
  > str(List)
  List of 3
   $ Eleven: num 11
   $ Twelve: chr "xii"
   $ E     : num(0) 
  > List[["Eleven"]] <- List[["Eleven"]] + 100
  > str(List)
  List of 3
   $ Eleven: num 111
   $ Twelve: chr "xii"
   $ E     : num(0) 
  > nm <- "Twelve"
  > List[[nm]]
  [1] "xii"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Andrew Robinson
> Sent: Thursday, February 07, 2013 12:52 PM
> To: David Winsemius
> Cc: r-help
> Subject: Re: [R] why "object 'x' not found"?
> 
> Hi David,
> 
> The following is an interesting observation ...
> 
> On Friday, February 8, 2013, David Winsemius wrote:
> 
> >
> > On Feb 7, 2013, at 10:20 AM, Winfried Moser wrote:
> > ....
> >
> Notice that the '[[' function is superior in every way to the '$' function.
> >
> 
> I'm curious to know whether you can point to some consolidated comparison?
> 
> Best wishes,
> 
> Andrew
> 
> 
> --
> Andrew Robinson
> Director (A/g), ACERA
> Department of Mathematics and Statistics            Tel: +61-3-8344-6410
> University of Melbourne, VIC 3010 Australia               (prefer email)
> http://www.ms.unimelb.edu.au/~andrewpr              Fax: +61-3-8344-4599
> http://www.acera.unimelb.edu.au/
> 
> FAwR: http://www.ms.unimelb.edu.au/~andrewpr/FAwR/
> SPuR: http://www.ms.unimelb.edu.au/spuRs/
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.



More information about the R-help mailing list