Dear All, applying some function within a with() function I wanted to use also sapply() and get() to form a data.frame, but did not succede. Below is a simplified example. It is possible to use sapply() within a with() function, it is also possible to use get() within a with() function, but when I try to use get within sapply within with I arrive at "Error in get(x, envir, mode, inherits) : variable "v5" was not found". Is there a solution? Thanks, Heinz T?chler ## example df1 <- data.frame(v5=16:20, v6=21:25, v7=I(letters[16:20]), v8=letters[16:20]) with(df1, sapply(c('v5', 'v6'), get) ) ## Error, see next line ## Error in get(x, envir, mode, inherits) : variable "v5" was not found with(df1, sapply(list(v5, v6), mean) ) # does work with(df1, get('v5') ) # does work platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status Patched major 2 minor 3.1 year 2006 month 07 day 23 svn rev 38687 language R version.string Version 2.3.1 Patched (2006-07-23 r38687)
On Thu, 3 Aug 2006, Heinz Tuechler wrote:> Dear All, > > applying some function within a with() function I wanted to use also > sapply() and get() to form a data.frame, but did not succede. > Below is a simplified example. > It is possible to use sapply() within a with() function, it is also > possible to use get() within a with() function, but when I try to use get > within sapply within with I arrive at "Error in get(x, envir, mode, > inherits) : variable "v5" was not found". > Is there a solution?This isn't what you want to hear, but the solution is probably to not use get(). How to not use get() will depend on what your problem really is.> > ## example > df1 <- data.frame(v5=16:20, v6=21:25, v7=I(letters[16:20]), v8=letters[16:20]) > > with(df1, sapply(c('v5', 'v6'), get) ) ## Error, see next line > ## Error in get(x, envir, mode, inherits) : variable "v5" was not foundget() looks in the environment it was called from, which is the environment inside lapply(), whose parent is the environment of the base package. There is no "v5" there.> with(df1, sapply(list(v5, v6), mean) ) # does workThis works because list(v5,v6) is evaluated in df1.> with(df1, get('v5') ) # does workThis works because get() looks in the environment it was called from, which is df1. -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
Thank you, Thomas! It helps a lot to know that something is impossible and that I have to look for a different kind of solution. Heinz At 07:33 03.08.2006 -0700, Thomas Lumley wrote:>On Thu, 3 Aug 2006, Heinz Tuechler wrote: > >> Dear All, >> >> applying some function within a with() function I wanted to use also >> sapply() and get() to form a data.frame, but did not succede. >> Below is a simplified example. >> It is possible to use sapply() within a with() function, it is also >> possible to use get() within a with() function, but when I try to use get >> within sapply within with I arrive at "Error in get(x, envir, mode, >> inherits) : variable "v5" was not found". >> Is there a solution? > >This isn't what you want to hear, but the solution is probably to not use >get(). How to not use get() will depend on what your problem really is. > >> >> ## example >> df1 <- data.frame(v5=16:20, v6=21:25, v7=I(letters[16:20]),v8=letters[16:20])>> >> with(df1, sapply(c('v5', 'v6'), get) ) ## Error, see next line >> ## Error in get(x, envir, mode, inherits) : variable "v5" was not found > >get() looks in the environment it was called from, which is the >environment inside lapply(), whose parent is the environment of >the base package. There is no "v5" there. > >> with(df1, sapply(list(v5, v6), mean) ) # does work > >This works because list(v5,v6) is evaluated in df1. > >> with(df1, get('v5') ) # does work > >This works because get() looks in the environment it was called from, >which is df1. > > > -thomas > >Thomas Lumley Assoc. Professor, Biostatistics >tlumley at u.washington.edu University of Washington, Seattle > >
Reasonably Related Threads
- How to get the name of the first argument in an assignment function?
- how to print a data.frame without row.names
- From FAQ 7.21 to a command like apply(sapply(list(f1,f2,f3),is.na),2,sum)
- Adding complex new columns to data frame depending on existing column
- How to know if a bug was recognised