Displaying 20 results from an estimated 10000 matches similar to: "How to get the namespace of a function?"
2005 Apr 27
3
Time series indexes
I tried to assign values to specific elements of a time series and got
in trouble. The code below should be almost self-explanatory. I wanted
to assign 0 to the first element of x, but instead I assigned zero to
the second element of x, which is not what I wanted. Is there a
function that will allow me to do this without going into index
arithmetic (which would be prone to errors)?
FS
>
2006 May 20
3
sapply and Date objects
This is probably a dumb question, but I cannot figure it out. Why does
this happen?
dt <- as.Date("1954-02-01")
> as.character(dt)
[1] "1954-02-01"
> sapply(c(dt), as.character)
[1] "-5813"
Thanks.
FS
2005 Apr 27
3
assign to an element of a vector
I am trying to find a way to assign values to elements of a vector
that will be defined by a user. So I don't have the name of the vector
and cannot hard code the assignment in advance. In the example below I
have to get() the vector using its name. When I try to assign to an
element I get an error:
> a <- c(1,2,3)
> get('a')[1] <- 0
Error: Target of assignment expands to
2005 Apr 26
2
Extending time series
I noticed that when one tries to extend a time series (ts) object by a
single period using window() actually two observations (NAs) are
added. See below for an example. Does anyone know the reason for this
behavior and how to avoid it?
Thanks.
FS
> x<-ts(c(1,2,3))
> x1<- window(x, start(x), end(x) + 1, extend = TRUE)
> x1
Time Series:
Start = 1
End = 5
Frequency = 1
[1] 1 2
2005 Apr 15
2
Define "local" function
I discovered a bug in a program I am writing which was due to the
program using a global variable within a function.
For example,
myfunc <- function(x) { y}
That is, I made a mistake when defining the function and wrote "y"
when I should have written "x".
However, there was a variable y in the global environment and the
function "worked" but gave the wrong
2005 Apr 26
1
Time alignment of time series
One thing that has given me trouble is the fact that the time series
implementation in the class ts relies on the concept of a "start" to
the time series. For example, if I have
ts1 <- ts(c(1,2,3))
dts1 <- diff(ts1)
then dts1 will be a vector c(1,1) but with the attribute start = 2.
Similarly, when one takes lags the "start" is moved around and the
underlying vector
2008 Mar 22
1
Geting names of variables
I wanted to create a list with the names of variables and their
values. So I wrote the following function.
add.to.list.names.vars.1 <- function(lnv, vnv) {
i <- 1
while (i < length(vnv)) {
z <- as.character(eval(substitute(quote(vnv[i+1])))[[2]][[i + 2]])
lnv[[vnv[i]]] <- list(Name = z, Value = vnv[i+1])
i <- i + 2
}
lnv
}
It works, but it is very
2008 Apr 27
1
system.time()
I thought system.time() would return three numbers, the first two
adding up to the third one. However, this does not hold in my system
running Windows Vista Home Premium, with an Intel Core 2 Duo
processor. For example, using the code in the help for system.time()
(with one zero added), I got:
> system.time(for(i in 1:100) mad(runif(10000)))
user system elapsed
0.27 0.00 0.25
Is
2005 Apr 12
1
Regression and time series
Can someone shed some light on this obscure portion of the help for lm?
"Considerable care is needed when using 'lm' with time series.
Unless 'na.action = NULL', the time series attributes are stripped
from the variables before the regression is done. (This is
necessary as omitting 'NA's would invalidate the time series
attributes, and if
2010 May 10
1
Corrupt R installation?
I installed the lattice package, and got an error that R was not able
to remove the previous version of lattice. Now my installation seems
to be currupt, even affecting other packages. I am getting this error
when loading TTR:
> library(TTR)
Loading required package: xts
Loading required package: zoo
Error in loadNamespace(i, c(lib.loc, .libPaths())) :
there is no package called
2005 Feb 01
2
Rcmdr doesn't seem to work
Context: Windows XP - R 2.0.1 with the latest updated packages (including
Rcmdr)
I'm trying to load Rcmdr to use the 3d plots (e.g. scatter3d) but here it
is what happens:
> library(Rcmdr)
Loading required package: zoo
Loading required package: strucchange
Loading required package: sandwich
Loading required package: relimp
Loading required package: mvtnorm
Loading required package:
2010 Sep 03
3
S4 Method Signatures
Hello,
If the signature of a method defines which generic it implements then I'm confused about why this minimal example I invented won't work :
setGeneric("myFun", function(rs, ...){standardGeneric("myFun")})
setGeneric("myFun", function(cs, ...){standardGeneric("myFun")})
setMethod("myFun", "numeric", function(rs, colour =
2011 Feb 02
1
pass nrow(x) to dots in function(x){plot(x,...)}
Dear Rers,
I have a function to barplot() a matrix, eg
myfun <- function(x, ...) { barplot(x , ... )}
(The real function is more complicated, it does things to the matrix first.)
So I can do:
m1 <- matrix(1:20,4)
myfun(m1)
myfun(m1, main="My title")
I'd like to be able to add the number of rows of the matrix passed to
the function to the "..." argument, eg
2009 Jun 12
1
how to trigger variable creation?
Hello R users,
i'm wondering how to trigger variable creation.
Whenever a variable is created i want my own function myFun(...) to be
started.
if (exists("x")) {rm(x)} # after removal of x
# any of these calls
x<-10 # should call myFun
x=10 # should call myFun
assign(x,10) # should call myFun
etc.
2015 Oct 22
2
Changed behaviour when passing a function?
Of course (and unsurprisingly) Duncan is correct. I see that behavior in R
3.1.0, as well as the modern ones Duncan mentioned.
What I said is true, as far as it goes, but the symbol being resolved is
FUN, so when looking for a function it doesn't find the function version of
round.
Did you perhaps have a function named FUN in your global environment? If so
you are being bitten by what I
2002 Jan 07
1
Mishandling missing "..." (PR#1247)
R> myfun <- function(x, ...) {x[...] <- 0; x}
R> myfun(3)
Error in myfun(3) : SubAssignArgs: invalid number of arguments
It fails because no ... was passed. The workaround (and desired behavior) is:
R> myfun <- function(x, ...) {if (missing(...)) x[] <- 0 else x[...] <- 0; x}
R> myfun(3)
[1] 0
Deja vu? This is the one piece of my PR#1110 (Oct 3, 2001) that I
2011 Jul 23
1
call a function with explicitly not setting an argument
Is there a way to call a function, and explicitly set an argument to 'not
specified'? My situation is the following. I have a function which passes on
most of its arguments to another function. The second function, myfun2,
serializes all arguments and is out of my control.
myfun <- function(...){
return(myfun2(...));
}
now, the value for arguments of myfun are stored in variables.
2012 Dec 05
1
NAMESPACE problem: import(zoo) but 'zoo' could not be loaded
Hello:
I'm having problems creating a real NAMESPACE to replace the pro
forma one in the fda package on R-Forge. "R CMD check" complains,
"Error: package 'zoo' could not be loaded ... there is no package called
'zoo'"; see below. I get this both with and without "import(zoo)" in
NAMESPACE.
Suggestions?
Thanks,
2012 Jul 05
1
Extracting srcref for S4 methods
Hi,
on R version 2.15.1 (2012-06-22) (Platform: i686-pc-linux-gnu (32-bit))
sourced functions have srcref attached as an attribute.
Are such data also available for S4 generics and methods? How? (See
sample code below)
Thank you.
Bests,
Renaud
f <- textConnection(
"
f <- function(){}
setGeneric('myfun', function(x, ...) standardGeneric('myfun'))
2007 Feb 23
1
how to use apply with two variables
Hi,
this is a made-up example. Function "myfun" returns two arguments. Can
"apply" be used so that "myfun" is called only once?
Thanks
Serguei
mat<-matrix(runif(50),nrow=10,ncol=5)
myfun<-function(x) {
mymean<-mean(x)
mysd<-sd(x)
return(mymean,mysd)
}
out1<-t(apply(mat,1,function(x) myfun(x)$mymean))
out2<-t(apply(mat,1,function(x)