Hi, I am probably making a simple mistake but I can't see it> XError: 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'?> version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 0.0 year 2004 month 10 day 04 language R Regards, John. John Gavin <john.gavin at ubs.com>, Quantitative Risk Models and Statistics, UBS Investment Bank, 6th floor, 100 Liverpool St., London EC2M 2RH, UK. Phone +44 (0) 207 567 4289 Fax +44 (0) 207 568 5352 Visit our website at http://www.ubs.com This message contains confidential information and is intend...{{dropped}}
<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. Also, try lapply("FUN",get) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Wed, 6 Oct 2004 john.gavin at ubs.com wrote:> 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'?Yes. Note that the first argument of lapply is supposed to be a list, so what you are really using is lapply(as.list("X"), exists) and indeed an object named "X" exists in the environment within which exists() is run. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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. John Gavin <john.gavin at ubs.com>, Quantitative Risk Models and Statistics, UBS Investment Bank, 6th floor, 100 Liverpool St., London EC2M 2RH, UK. Phone +44 (0) 207 567 4289 Fax +44 (0) 207 568 5352> -----Original Message----- > From: Peter Dalgaard [mailto:p.dalgaard at biostat.ku.dk] > Sent: 06 October 2004 17:31 > To: Gavin, John > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] lapply with argument "X" > > > <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. > > Also, try lapply("FUN",get) >Visit our website at http://www.ubs.com This message contains confidential information and is intend...{{dropped}}
Seemingly Similar Threads
- defining a template for functions via do.call and substitute.
- defining a template for functions via do.call and substit ute.
- Rconsole - setting the size and location of Windows help files (Rgui)
- lattice xyplot - aligning date labels so that they align with the grid lines in panel.grid
- Sweave - documenting a long function