Displaying 20 results from an estimated 30000 matches similar to: "Assigning variables using a loop"
2013 Dec 06
2
Using assign with mapply
I have a data frame whose first colum contains the names of the variables
and whose second colum contains the values to assign to them:
: kkk <- data.frame(vars=c("var1", "var2", "var3"),
vals=c(10, 20, 30), stringsAsFactors=F)
If I do
: assign(kkk$vars[1], kkk$vals[1])
it works
: var1
[1] 10
However, if I try with mapply
2010 May 26
2
writing function : can't find an object
Dear group,
Here is my function:
#return the daily PL for day y
PLDaily<-function(x,y)
{
#find elements in my directory with "LSCPos" in the name, keep the numeric
part in the name and
#create a list
l<-gsub("\\D","",dir()[grep("LSCPos",dir())])
#select in the list the desired elements
2009 Dec 17
2
issue with using rm: cannot generate on-the-fly list
Hello,
I have the following problem when trying to use rm:
In a top level script file I have a loop iterating over some index. The loop
is not contained within a function, so the scope of variables declared in
the loop is global. Within this loop I generate several variables which
should be removed at the end of each iteration. To do this, I wrote a
function to clean up the workspace. An example
2003 May 22
3
How to avoid function masking
Hi All,
I've been working on updating the 'genetics' package. As a consequence of
the upgrade, .First.lib() looks like:
.First.lib <- function(libname, pkgname)
{
if (!require(combinat))
warning("Unable to load 'combinat' library. Function
`diseq.ci' will fail.")
require(gregmisc)
genotype <-
2002 Aug 07
1
ESS assigns .Last.value to the wrong place in R
I repeat my emails of 11/15/01 and 2/26/02, since it looks like this ESS bug is
still not fixed in ESS 5.1.23, and I think some resolution is needed.
When help() is invoked, ESS makes a copy of .Last.value in the .GlobalEnv,
which is *not* where R normally stores it (R stores it in package:base).
When this copy becomes stale it leads to wrong answers. The bug is in
essd-r.el, lines 63-64:
2001 Sep 27
1
list of all objects - just being curious
Hello all,
to obtain a list of all objects in all search paths, I've found the
following to work:
> biglist <- sapply(1:length(search()), objects)
This more obvious one, however, does not work:
> biglist <- sapply(search(), objects)
Error in pos.to.env(pos) : invalid argument
Still, search() gives
[1] ".GlobalEnv" "package:ctest" "Autoloads"
2004 Sep 22
1
attaching to position 1
If an attempt is made to attach to position 1, it appears to work
(not even a warning) but in fact it doesn't work as many would
expect. "search" thinks that it gets placed in position 2, but nothing
is actually there (according to "ls").
This is guaranteed to be confusing (and annoying) to people who
are used to attaching to position 1 in S-PLUS.
I'm not clear on
2011 Sep 06
1
object.size() not recognized within .First()
I have a function called within .First(), as in
.First=function() {
...blah...
BIG(partofblah) #BIG is my function, n=partofblah when called
...foo...
}
The partofblah component of blah is a number obtained from readline(),
which is then an argument to BIG()
BIG=function(n=10,removeask=T) {
z <- sapply(ls(pos=1), function(x)object.size(get(x)))
...stuff... }
When .First()
2013 Jan 01
1
Behavior or as.environment in function arguments/call (and force() behaviors...)
Happy 2013!
Can someone with more knowledge of edge case scoping/eval rules explain
what is happening below? Happens in all the versions of R I have on hand.
Behavior itself is confusing, but ?as.environment also provides no clue.
The term used in that doc is 'search list', which is ambiguous, but the
see also section mentions search(), so I would *think* that is what is
intended.
2007 Feb 23
2
Functions that write functions in R packages
Dear all,
Another question related to my ggplot package: I have made some
substantial changes to the backend of my package so that plot objects
can now describe themselves much better. A consequence of this is
that a number of convenience functions that previously I wrote by
hand, can now be written automatically. What is the best practice for
creating these functions for bundling in a
2010 Jun 17
1
library(...,pos=) is not consistent
I want to be able to load a library in a specified position using the
pos= argument and have any subsequent library required by the one I'm
loading go into a specified library as well. For example, in loading
caret, it requires and loads lattice as well. When I specify that caret
goes into position 9, lattice goes into position 2 by default. Is there
a way to specify that by loading a
2007 Mar 06
2
bug: sticky symbol refs? (PR#9555)
Hello. What happens in the following is that I create two simple functions, f and g, on the workspace. Then I
replace g. When I then call f, it uses the old version of g. Now clearly, the circumstances for this to happen
must be quite special and rare. But I'd say they're not pathological. It seems to require two things: 1) masked versions
of f and g on a search position lower down the
2011 Jan 22
1
Truly Global Variables
Hello everybody,
I have a problem that is bothering me for quite a while now and I
don't know the answer... I want to create global variables (out of a
function) which I can access from all other functions... But somehow
that does not work too well. Attached some code for an example:
function_called <- function (){
result = globalvariable*globalvariable
print(result)
}
2000 Jan 10
1
'at' parameter in mtext(.., adj=0, outer=T) (PR#396)
Depending on the setting of par()$usr,
the 'at' setting in mtext(.., adj=0, outer=T) may cause the
text to appear in an anomalous position (e. g. in the first
instance below, at the left of the plot region rather than
at 'at=0' in the figure region), or the text may not appear
at all.
If one does not set the 'at' parameter the text appears
(with 'adj=0') on the
2010 Apr 23
1
help in conditional histogram
Dear Dr. Sarkar,
When I try to run the codes, I found the following problem:
> h<- sample(1:14, 319, rep=T)
> c<- sample(1:14, 608, rep=T)
> n<- sample(1:14, 1140, rep=T)
> vt<-c(h, c, n)
> ta<-rep(c("h", "c", "n"), c(319, 608, 1140))
>
> to<-data.frame(vt,ta)
> library(lattice)
Attaching package: 'lattice'
2009 May 12
1
questions on rpart (tree changes when rearrange the order of covariates?!)
Greetings,
I am using rpart for classification with "class" method. The test data is
the Indian diabetes data from package mlbench.
I fitted a classification tree firstly using the original data, and then
exchanged the order of Body mass and Plasma glucose which are the
strongest/important variables in the growing phase. The second tree is a
little different from the first one. The
1997 Jul 25
2
R-beta: R 0.50 alpha
The new code seems to have broken various things.
Autoloading of libraries doesn't seem to work:
> library(survival4)
Autoloading required library: splines
Error in pos.to.env(pos) : invalid "pos" argument
> search()
[1] ".GlobalEnv" "library:survival4" "library:date"
[4] "library:base"
The coxph function the
1997 Jul 25
2
R-beta: R 0.50 alpha
The new code seems to have broken various things.
Autoloading of libraries doesn't seem to work:
> library(survival4)
Autoloading required library: splines
Error in pos.to.env(pos) : invalid "pos" argument
> search()
[1] ".GlobalEnv" "library:survival4" "library:date"
[4] "library:base"
The coxph function the
2005 Jun 10
1
Exiting R and package detachment?
Hi,
is there away to assure that a package is detached when R quits? I know
about .Last(), but that requires the user to setup that function. What
I am looking for is a way for the package to do this itself, without
asking the user to edit "their" .Last(). From ?setHook I know that:
"...when an R is finished, packages are not detached and namespaces
are not unloaded, so
2011 Aug 23
2
Increase transparency: suggestion on how to avoid namespaces and/or unnecessary overwrites of existing functions
aDear list,
I'm aware of the fact that I posted on something related a while ago,
but I just can't sweat this off and would like to ask your for an opinion:
The problem:
Namespaces are great, but they don't resolve certain conflicts regarding
name clashes. There are more and more people out there trying to come up
with their own R packages, which is great also! Yet, it becomes