similar to: assign to an element of a vector

Displaying 20 results from an estimated 10000 matches similar to: "assign to an element of a vector"

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 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 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
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 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
2007 Nov 05
5
Sending E-Mail from R
Hi, Has anyone used R to send out an email via an SMTP server? I run R from a script and would like to be able to send out an email from there also. I could shell out to an email program, but was hoping maybe someone had a package for R or a smooth way to do it. Thanks, Josh [[alternative HTML version deleted]]
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 Apr 11
1
How to assign variable name (of a vector, matrix, array) from element of another vector or by using paste?
Dear All, My problem is the following > varname <- c("age","sex","edu","mrt") > varstates <- c(10,2,4,3) # 10 age groups, 2 sexes, 4 education group and 3 marital status.. Now, I want four variables (vector with length1) named nrage, nrsex, nredu, nrmrt and assing some values > for (i in 1:4) paste("nr",varname[i],sep="")
2012 Mar 18
3
assign a value to an element
Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible URL : <https://stat.ethz.ch/pipermail/r-help/attachments/20120318/d68edf35/attachment.pl>
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 Nov 23
1
assign() problem
I've written a piece of code (see below) to do a wavelet image decomposition, during the evaluation of this code I would like to write the results of some calculations back to the R root directory. I used assign() to do so because the names should vary when going thrue a while() loop. For some unknown reason I get an error that says: Error in assign(varname[i], imwrImage) :
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
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 =
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
2005 Apr 15
4
function corresponding to map of perl
Is there a function in R that corresponds to the function ``map'' of perl? It could be called like: vector.a <- map( vector.b, FUN, args.for.FUN ) It should execute for each element ele.b of vector.b: FUN( vector.b, args.for.FUN) It should return a vector (or data.frame) of the results of the calls of FUN. It nearly works using: apply( data.frame( vector.b ), 1, FUN,