similar to: Define "local" function

Displaying 20 results from an estimated 20000 matches similar to: "Define "local" 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
2006 Feb 02
2
How to get the namespace of a function?
I declared the environment of the function myfun to be NULL as follows: environment(myfun) <- NULL Later on I called that myfun and got an error message because the function index() in the zoo package was called inside myfun and was not visible: Error in myfun(args) : couldn't find function "index" I tried to use zoo::index() instead of index(), but that did not work. In fact,
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
2011 Nov 10
3
optim seems to be finding a local minimum
Hello! I am trying to create an R optimization routine for a task that's currently being done using Excel (lots of tables, formulas, and Solver). However, otpim seems to be finding a local minimum. Example data, functions, and comparison with the solution found in Excel are below. I am not experienced in optimizations so thanks a lot for your advice! Dimitri ### 2 Inputs:
2008 Dec 02
4
Variables inside a for
Hi! I had a database with some variables in sequence. Let me say: TX01, TX02, TX03 and TX04. But I need to run some regressions changing the variables... so: variable <- paste("TX0", 1:4, sep="") for(i in 1:4){ test[i] <- lm(variable[i] ~ INCOME, data=database) } But doesn't work... lm tries to find a variable inside database named variable[i] ... Suggestions?
2005 Apr 16
2
Getting subsets of a data frame
I was reading in the Reference Manual about Extract.data.frame. There is a list of examples of expressions using [ and [[, with the outcomes. I was puzzled by the fact that, if sw is a data frame, then sw[, 1:3] is also a data frame, but sw[, 1] is just a vector. Since R has no scalars, it must be the case that 1 and 1:1 are the same: > 1 == 1:1 [1] TRUE Then why isn't sw[,1] =
2008 Jan 16
3
Rprofile.site not executed?
It seems that my Rprofile.site file is not executed when I start R. To test this I included the following code in that file: .First <- function(){ cat("\nWelcome at", date(), "\n") flush.console() } When I start R the message above is not displayed. I am running R 2.6.1 (rgui.exe) under Vista Home Premium. The Rprofile.site is in the etc folder and the R_PROFILE
2005 May 10
6
Does R have a command for sending emails?
Is there a way to have an R program send an email? Something like this: address <- 'abc at d.com' text <- 'This is the email body' send.email(address, text) Thanks. FS
2010 Mar 31
2
Simplifying particular piece of code
Hello, everyone I have a piece of code that looks like this: mrets <- merge(mrets, BMM.SR=apply(mrets, 1, MyFunc, ret="BMM.AV120", stdev="BMM.SD120")) mrets <- merge(mrets, GM1.SR=apply(mrets, 1, MyFunc, ret="GM1.AV120", stdev="GM1.SD120")) mrets <- merge(mrets, IYC.SR=apply(mrets, 1, MyFunc, ret="IYC.AV120",
2005 Sep 22
3
warning.expression?
Hi! I'm trying to catch all warning-messages for special handling. It seems options (warning.expression=myfunc ()) can be used for that. However, the question is: How can I get at the actual warning in myfunc ()? Apparently in S, you can use .C("get_last_message") for that. Is there a similar mechanism in R? Thanks for your help! Thomas
2005 Sep 22
3
warning.expression?
Hi! I'm trying to catch all warning-messages for special handling. It seems options (warning.expression=myfunc ()) can be used for that. However, the question is: How can I get at the actual warning in myfunc ()? Apparently in S, you can use .C("get_last_message") for that. Is there a similar mechanism in R? Thanks for your help! Thomas
2006 May 01
1
Problem with optim()
I am having a problem with optim() using the "L-BFGS-B" method. When I set the lower limit for the third parameter equal to zero I get an error message: > low.lim.3 <- 0 > phi_opt <- optim(phi_, model_lik, NULL, method = "L-BFGS-B", lower=c(0.2, -100, low.lim.3, 0), upper= c(10, 100, 10, 10), control = list(maxit = 1000, parscale = c(0.2, u1, 0.002, 0.002), trace =
2006 Apr 04
1
Indexing a vector by a list of vectors
Hello R-help - I have vec <- c("string1", "string2", "string3") ind <- list(c(1,2),c(1,2,3)) I want "vec" indexed by each vector in the list "ind". The first element of the list I want would be vec[c(1,2)], the second element would be vec[c(1,2,3)], like the following. [[1]] [1] "string1" "string2" [[2]] [1]
2011 Feb 18
1
debugger() fails if "..." in function arguments
Dear all, I'm having a problem with debugger() in both R 2.8.0 and R 2.12.0. Probably also versions in-between. I don't see it logged in the bug database, but it's hard for me to imagine that no-one else has encountered it. So my question is whether it's a known problem with a workaround, or do I log it as a new problem? The situation is that if I use