Displaying 20 results from an estimated 40000 matches similar to: "Using object.size inside a function"
2011 Sep 08
2
Variable scoping question
I modified an example in the object.size help page to create a function
I want to be able to run:
"mysize" <- function() {
z <- sapply(ls(), function(w) object.size(get(w)))
as.matrix(rev(sort(z))[1:5])
}
mysize()
When I test the lines inside the function it works fine:
> z <- sapply(ls(), function(w) object.size(get(w)))
> as.matrix(rev(sort(z))[1:5])
2006 Dec 24
2
how to 'get' an object that is part of a list
This might be an trivial thing but I am stuck.
Consider:
xx <- list(a=1:5, b=letters[1:5])
Although object xx is accessible through its name,
how can object xx$b be accessed similarly through its name?
> get("xx")
$a
[1] 1 2 3 4 5
$b
[1] "a" "b" "c" "d" "e"
> get("xx$b")
Error in get(x, envir, mode, inherits)
2006 Aug 03
2
get() in sapply() in with()
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) :
2012 Oct 21
1
Changing a for loop to a function using sapply
Apparently there is one or more concepts that I do not fully understand
from the descriptions of a function and the apply material. I have
been reading the mail from this forum and have learned much but, in this
case, what I have been reading here and from the manual isn't enough.
The following code produces what I want with the for loop. From what I
have read from this forum, a for
2009 Nov 20
3
Remove leading and trailing white spaces
I have a character string and I would like to remove the leading and
tailing white spaces. The example for 'sub' shows how to remove the
trailing white spaces, but I still can't figure out how to remove both
trailing and leading white spaces because I can't find any documentation
for what "+$" means or what "\\s+$" means. Maybe its because I don't
have a
2010 Jul 07
3
Large discrepancies in the same object being saved to .RData
Hi developers,
After some investigation I have found there can be large discrepancies in the same object being saved as an external "xx.RData" file. The immediate repercussion of this is the possible increased size of your .RData workspace for no apparent reason.
The function and its three scenarios below highlight these discrepancies. Note that the object being returned is exactly
2012 Oct 25
3
problem in finding sizes of objects using a for loop
Dear All,
I wanted to extract the sizes of all created objects. For E.g when I
created 2 objects(x and y), I got their sizes using the following
code:
> x<-rnorm(10000)
> y<-runif(100,min=40,max=1000)
> ls()
[1] "x" "y"
> object.size(x)
80024 bytes
> object.size(y)
824 bytes
However, I was unable to get their sizes when I used a for loop in the
following
2011 Sep 08
3
How to specify a variable name in the regression formula without hard coding it
I have a matrix called mat and y is the column number of my response and
x is a vector of the column numbers of my terms. The variable name of y
can change, so I don't want to hardcode it. I can find out the name as
follows:
> names(mat)[y]
[1] "er12.l"
Then I can run the regression by hard coding the variable name as
follows:
> mod <-
2012 May 26
2
avoid error within for loop, try, trycatch, while, move to next iteration, unlist
Hi there,
I would like to ask something about how to avoid a possible error message within a for loop. I am running a simulation and in some repetitions there may be an error that will cause a crash and stop the whole procedure, what I want is to simply move on to the next iteration automatically and discard the "bad" repetitions from my results. I used the "try" function to
2015 Apr 29
2
Formula evaluation, environments and attached packages
Hi!
Some time ago, I replaced calls to library() with calls to
requireNamespace() in my package logmult, in order to follow the new
CRAN policies. But I just noticed it broke jackknife/bootstrap using
several workers via package parallel.
The reason is that I'm running model replicates on the workers, and the
formula includes non-standard terms like Mult() which are provided by
gnm. If gnm
2012 Oct 07
3
get: problem with environments
Dear R users,
I am running R-2.15.1 in Linux Slackware64-14.0. Here is my minimal working example:
testfun <- function (x) {
a <- 0;
sapply(X="a", FUN=get, envir=sys.frame(which=x));
}
Inside R, that is R called from within a Linux terminal, the following code works:
testfun(x=5)
print(testfun(x=6))
But within rkward the above code fails and the following works:
testfun(x=1)
2011 Jan 03
7
Saving objects inside a list
Hello there,
any ideas on how to save all the objects on my workspace inside a list
object?
For example, say my workspace is as follows
ls()
[1] "x" "y" "z"
and suppose I want to put these objects inside a list object, say
object.list <- list()
without having to explicitly write down their names as in
object.list$x = x
object.list$y = y
object.list$z = z
Is
2003 Oct 16
2
returning dynamic variable names from function
Within a function I'm assigning dynamic variable names and values to them
using the "assign" function. I want to pass back the results but am
uncertain how to do this.
Basically, my function reads a number of data files and uses the filename of
each file as the variable name for a list-to-become-dataframe. I want then
to pass all these lists back, but again, the names of the
2011 Oct 12
2
goofy class question
Dear R People:
Here is a really goofy question.
I have some objects which have 2 classes: data.frame and ucr.
Also, the classes will always be in that order.
I have tried all sorts of things, but to no avail.
listucrModels <- function(envir=.GlobalEnv, ...) {
objects <- ls(envir=envir, ...)
if (length(objects) == 0) NULL
else objects[sapply(objects,
function(.x)
2010 Nov 07
2
stupid R tricks
Hi all,
Just thought I'd post this (maybe) helpful tool I wrote. For people
like me who are bad at keeping a clean environment, it's a time-saver.
#simple command to get only one type of object in current environment
lstype<-function(type='closure'){
inlist<-ls(.GlobalEnv)
if (type=='function') type <-'closure'
2005 Jan 21
6
how to use do.call("rbind", get(list(mlist)))
I have around 200 data frames I want to rbind in a vectorized way.
The object names are:
m302
m303
...
m500
So I tried:
mlist <- paste("m",302:500,sep="")
dat <- do.call("rbind", get(list(mlist)))
and I get "Error in get(x, envir, mode, inherits) : invalid first argument"
I know "rbind" is valid because
dat <- rbind(m302, m303,
2011 Aug 27
2
Am having trouble calling a function
In my main R program, I have
source("retaanalysis/Functions/doAirport.R")
.... stuff to read data and calculate ads
sapply(ads, function(x) {doAirport(x, base)} )
And doAirport has
# analyze the flights for a given airport
doAirport = function(df, base) {
# Get rid of unused runway factor levels (from other airports)
df$lrw <- drop.levels(df$lrw) # In gdata package
#
2008 May 02
1
How to parse XML
I would like to learn how to parse a mixed text/xml document I
downloaded from the sec.gov website (see example below). I would like
to parse this to get the value for each xml tag and then access it
within R, but I don't know much about xml so I don't even know where to
start debugging the errors I am getting in this example code. Can
anyone help me get started?
Thanks, Roger
ftp
2010 Aug 26
3
Help with ddply to eliminate a for..loop
I created a small example to show something that I do a lot of. "scale"
data by month and return a data.frame with the output. "id" represents
repeated observations over "time" and I want to scale the "slope"
variable. The "out" variable shows the output I want. My for..loop
does the job but is probably very slow versus other methods. ddply
2004 Aug 31
2
enter browser on error
Is there a way I can get R to automatically enter the browser inside a user-defined function on the generation of an error? Specifically, I'm trying to debug this:
Error in as.double.default(sapply(lis, FUN)) :
(list) object cannot be coerced to double
In addition: There were 38 warnings (use warnings() to see them)
> traceback()
8: as.double.default(sapply(lis, FUN))
7: