Dear all, I would like to know if it?s possible to create an index for a variable/vector that specifies the value in the n position before the actual value. For example, if X <- c(letters[1:10]), x[10] is ?j?. I would like to create an index exp3 (the third position before) such as if z <- x[exp3], then z[10] = ?g?. In Stata that index is x[-3], but not in R. I have to input it in a very complex function to be minimised with the mle command, so I can?t solve this problem with a loop. Can you help me?
Is exp3 <- c(rep(NA, 3), seq_along(x)) what you are looking for?> x[exp3][10][1] "g" (I presume you meant x <- letters[1:10]: R is case-sensitive and you don't need c() to concatenate a single vector.) On Wed, 2 Jan 2008, Antonio Gasparrini wrote:> Dear all, > > I would like to know if it's possible to create an index for a > variable/vector that specifies the value in the n position before the > actual value. > > For example, if X <- c(letters[1:10]), x[10] is 'j'. I would like to > create an index exp3 (the third position before) such as if z <- > x[exp3], then z[10] = 'g'. > > In Stata that index is x[-3], but not in R. > > I have to input it in a very complex function to be minimised with the > mle command, so I can't solve this problem with a loop. > > Can you help me?-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
> I would like to know if it?s possible to create an index for a > variable/vector that specifies the value in the n position before > the actual value. > > For example, if X <- c(letters[1:10]), x[10] is ?j?. I would like to > create an index exp3 (the third position before) such as if z <- > x[exp3], then z[10] = ?g?.x<- letters[1:10] Then either z<- c(rep(NA,3), x) z[10] #"g" or exp3<- 1:length(x) - 3; exp3[exp3<=0] <- NA z <- x[exp3] z[10] #"g" Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:21}}