[R] lapply with argument "X"

Gabor Grothendieck ggrothendieck at myway.com
Wed Oct 6 19:23:45 CEST 2004


 <john.gavin <at> ubs.com> writes:

: 
: Hi,
: 
: > From: Peter Dalgaard [mailto:p.dalgaard <at> biostat.ku.dk]
: > <john.gavin <at> ubs.com> writes:
: > 
: > > Hi,
: > > 
: > > I am probably making a simple mistake but I can't see it
: > > 
: > > > X
: > > Error: Object "X" not found
: > > > exists("X")
: > > [1] FALSE
: > > > lapply("X", exists)
: > > [[1]]
: > > [1] TRUE
: > > 
: > > Why is lapply producing true?
: > > Is it something to do with the first
: > > argument of lapply also being called 'X'?
: > 
: > Internal variable capture. I think this is a bug.
: 
: Fyi, I wanted to show that users have to use single
: character variables with caution because of the existance
: of objects like 'c', 'q', 'T' and 'F'.
: So I tried to say
: 
: > xx <- c(letters, LETTERS) ; xx[sapply(xx, exists)]
:  [1] "c" "q" "t" "C" "D" "F" "I" "Q" "T" "X"
: 
: but the appearance of 'X' undermines my code.
: In that sense, it seems like a bug to me.
: (I have a local definition for 'Q' - quit without asking.)
: 
: > Also, try  lapply("FUN",get)
: 
: Agreed.
: 
: Regards,
: 
: John.


Try this:

R> xx <- c(letters, LETTERS) ; xx[sapply(xx, exists, envir = .GlobalEnv)]
 [1] "c" "n" "q" "t" "u" "v" "x" "C" "D" "F" "I" "T"




More information about the R-help mailing list