[R] Dynamically populate a vector by iteratively applying a function to its previous element.
I want to dynamically populate a vector by iteratively applying a function to its previous element, without using a 'for' cycle. My solution, based on a question I posted some times ago for a more complicated problem (see "updating elements of a list of matrixes without 'for' cycles") was to define a matrix of indexes, and then apply the function to the indexes. Here's a trivial example: # my vector, all elements still unassigned v <- rep(NA, 10) # initialisation v[1] <- 0 # the function to be applied v.fun = function(x) { i <- x[1] return(v[i]+1) } # The matrix of array indices idx <- as.matrix(expand.grid(c(1:9))) # Application of the function r[2:10] <- invisible(apply(idx, 1, v.fun)) [Note that this example is deliberately trivial: v <-c(0:9) would solve the problem. In general, the function can be more complicated.] The trick works only for v[2]. I imagine this is because the vector is not dynamically updated during the iteration, so all values v[2:10] are retained as NA. How can I solve the problem, without using a 'for' cycle? Thanks for your help ! Matteo __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] speed issue in simulating a stochastic process
I wish to simulate the following stochastic process, for i = 1...N individuals and t=1...T periods: y_{i,t} = y_0 + lambda Ey_{t-1} + epsilon_{i,t} where Ey_{t-1} is the average of y over the N individuals computed at time t-1. My solution (below) works but is incredibly slow. Is there a faster but still clear and readable alternative? Thanks a lot. Matteo rm(list=ls()) library(plyr) y0 = 0 lambda = 0.1 N = 20 T = 100 m_e = 0 sd_e = 1 # construct the data frame and initialize y D = data.frame( id = rep(1:N,T), t = rep(1:T, each = N), y = rep(y0,N*T) ) # update y for(t in 2:T){ ybar.L1 = mean(D[D$t==t-1,"y"]) for(i in 1:N){ epsilon = rnorm(1,mean=m_e,sd=sd_e) D[D$id==i & D$t==t,]$y = lambda*y0+(1-lambda)*ybar.L1+epsilon } } ybar <- ddply(D,~t,summarise,mean=mean(y)) plot(ybar, col = "blue", type = "l") [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] denstrip package: densregion when density is not provided
I have several estimated time series, running from 2013 to 2050. 'y' values are constrained between 0 and 1. I would like to plot them using shaded colours of decreasing intensity, depending on an estimated density at each point x in 2013-2050. This is what I have done: require(denstrip) x <- 2013:2015 y <- seq(0, 1, length=100) z <- read.delim("clipboard") densregion(x, y, z) where I imported 'z' from MS Excel. 'z' looks like (I copied only the first 2 columns, l but I have 100 of them): run1 run2 1 0.6932324 0.7732179 2 0.6773456 0.7971804 ... 37 0.7260790 0.8724961 38 0.7290335 0.8755433 I get the following error message: Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = decreasing)) : undefined columns selected Could anybody please help me with fixing this? Thanks in advance [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] denstrip package: densregion when density is not provided
I need to estimate the density from the other time series, similarly to what denstrip does (however, I need a whole region, and not just a strip). This is my updated example, where I tried to estimate the density from my bootstrapped projections using density() - as in denstrip: require(denstrip) x <- 2013:2015 y0.df <- read.delim("clipboard") y0.num <- data.matrix(y0.df, rownames.force = NA) y.df <- read.delim("clipboard") y.num <- data.matrix(y.df, rownames.force = NA) z <- density(y.num) densregion(x, y, z) where y0.num contains my "central" projection: > head(y0.num) default [1,] 0.7086428 [2,] 0.7111570 [3,] 0.7117583 [4,] 0.7124711 [5,] 0.7166852 [6,] 0.7167453 and y.num contains the bootstrapped projections: run1 run2 run3 run4 run5 run6 [1,] 0.6932324 0.7732179 0.6201226 0.6712345 0.6974636 0.7399447 [2,] 0.6773456 0.7971804 0.5810890 0.6775887 0.7134176 0.7480797 [3,] 0.6704025 0.8122599 0.5655451 0.6836657 0.7438639 0.7540193 [4,] 0.6671754 0.8235685 0.5663680 0.6828320 0.7650042 0.7605239 [5,] 0.6644370 0.8287378 0.5643008 0.6846030 0.7921467 0.7655395 [6,] 0.6630486 0.8333708 0.5612197 0.6882252 0.8183746 0.7687452 This is the error I get: Error in rank(x, ties.method = "min", na.last = "keep") : unimplemented type 'list' in 'greater' Matteo On 3 October 2015 at 09:16, Matteo Richiardi wrote: > I have several estimated time series, running from 2013 to 2050. 'y' > values are constrained between 0 and 1. I would like to plot them using > shaded colours of decreasing intensity, depending on an estimated density > at each point x in 2013-2050. > > This is what I have done: > > require(denstrip) > x <- 2013:2015 > y <- seq(0, 1, length=100) > z <- read.delim("clipboard") > densregion(x, y, z) > > where I imported 'z' from MS Excel. 'z' looks like (I copied only the > first 2 columns, l but I have 100 of them): > > run1 run2 > 1 0.6932324 0.7732179 > 2 0.6773456 0.7971804 > ... > 37 0.7260790 0.8724961 > 38 0.7290335 0.8755433 > > I get the following error message: > Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = > decreasing)) : > undefined columns selected > > Could anybody please help me with fixing this? Thanks in advance > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Create a data.table by looping over variable names
I apologise for this very basic question, but I found no answers on the web. I want to create a data.table with n columns, named i1 ... i'n', with only one row with value = 100 for every variable. I can do it "by hand": M.dt <- data.table(i1=100,i2=100,i3=100) but I would like to make it in a loop, as 'n' is large in my case. Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Create a data.table by looping over variable names
Hi, thanks a lot to everybody for your help. Very nice suggestions! Matteo On 16 December 2015 at 12:53, Matteo Richiardi wrote: > I apologise for this very basic question, but I found no answers on the > web. > I want to create a data.table with n columns, named i1 ... i'n', with only > one row with value = 100 for every variable. > > I can do it "by hand": > > M.dt <- data.table(i1=100,i2=100,i3=100) > > but I would like to make it in a loop, as 'n' is large in my case. > > > Thanks. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Applying a function to a matrix using indexes as arguments
I have to evolve each element of a matrix W W <- matrix(0,2,3) according to some function which uses the indices of the matrix [i,j] as arguments: w.fun = function(i,j) { return A[i]*B[j]/(C[i,j]) } where A<-c(100,100) B<-c(200,200,200) C <- matrix( rnorm(6,mean=0,sd=1), 2, 3) How can I do it, without recurring to a loop? Also, in my application I need to pass the function another argument. Thanks a lot for your suggestions. Matteo [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Applying a function to a matrix using indexes as arguments
My problem is of course more complicated, and is obviously not a homework. I just wanted to provide a minimal working example. You can replace the matrix C with a matrix containing any number, for what matters. Btw, because numbers are extracted from a Gaussian distribution, the likelihood that you draw a 0 is actually zero. Apart from this, apologies for having posted an html version. On 17 December 2015 at 00:36, Jeff Newmiller wrote: > This calculation divides by values centered around zero. The only context > that I can think of that would require such silliness is a homework > problem, and this list has a no-homework policy. If not, then mentioning > the theory you are applying might help someone point you at an existing > function that achieves your goals while avoiding divide-by-zero errors. > > Since you also posted in HTML I gather that you have not read the Posting > Guide mentioned below. Avoiding HTML on this list is to your benefit, since > using it inevitably leads to others seeing a garbled version of what you > sent. Please read the PG for more important guidance. > -- > Sent from my phone. Please excuse my brevity. > > On December 16, 2015 4:18:56 PM PST, Matteo Richiardi < > matteo.richia...@gmail.com> wrote: > >> I have to evolve each element of a matrix W >> >> W <- matrix(0,2,3) >> >> according to some function which uses the indices of the matrix [i,j] as >> arguments: >> w.fun = function(i,j) { >> return A[i]*B[j]/(C[i,j]) >> } >> >> where >> A<-c(100,100) >> B<-c(200,200,200) >> C <- matrix( rnorm(6,mean=0,sd=1), 2, 3) >> >> How can I do it, without recurring to a loop? Also, in my application I >> need to pass the function another argument. >> >> Thanks a lot for your suggestions. >> Matteo >> >> [[alternative HTML version deleted]] >> >> -- >> >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, >> self-contained, reproducible code. >> >> [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Applying a function to a matrix using indexes as arguments
Hi David, that's a great answer, thanks so much. I imagined apply() was involved in the solution, but I was unable to find how myself. Thanks again. Matteo On 17 December 2015 at 01:37, David Winsemius wrote: > > > On Dec 16, 2015, at 5:34 PM, David Winsemius > wrote: > > > > > >> On Dec 16, 2015, at 4:18 PM, Matteo Richiardi < > matteo.richia...@gmail.com> wrote: > >> > >> I have to evolve each element of a matrix W > >> > >> W <- matrix(0,2,3) > >> > >> according to some function which uses the indices of the matrix [i,j] as > >> arguments: > >> w.fun = function(i,j) { > >> return A[i]*B[j]/(C[i,j]) > >> } > >> > >> where > >> A<-c(100,100) > >> B<-c(200,200,200) > >> C <- matrix( rnorm(6,mean=0,sd=1), 2, 3) > >> > >> How can I do it, without recurring to a loop? Also, in my application I > >> need to pass the function another argument. > > > > mapply( function( i,j,fac) {fac*A[i]*B[j]/C[i,j]}, > > i=row(W), > > j=col(W), > > MoreArgs=list(fac=10) ) > > [1] -86207.97 325768.16 -135764.41 -913036.95 -142509.39 > > [6] 243715.67 > > > > > > N.B. all of the *apply functions are really loops. > > Furthermore, you can neatly populate the W matrix with the `[<-` function: > > W[] <- mapply( function( i,j,fac) {fac*A[i]*B[j]/C[i,j]}, > i=row(W), j=col(W),MoreArgs=list(fac=10) ) > > > W > [,1] [,2] [,3] > [1,] -86207.97 -135764.4 -142509.4 > [2,] 325768.16 -913037.0 243715.7 > > > > > > > -- > > David. > >> > >> Thanks a lot for your suggestions. > >> Matteo > >> > >> [[alternative HTML version deleted]] > >> > >> __ > >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > >> https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > >> and provide commented, minimal, self-contained, reproducible code. > > > > David Winsemius > > Alameda, CA, USA > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > David Winsemius > Alameda, CA, USA > > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] assigning values to elements of matrixes
I am following the example I find on ?assign: a <- 1:4 assign("a[1]", 2) This appears to create a new variable named "a[1]" rather than changing the value of the vector. Am I missing something here? How can I assign a value to a specified element of a vector/matrix? Of course, my problem is slightly more involved, but I guess the above is its core. For those interested, I have the two matrixes M_a <- matrix(0,10,10) M_b <- matrix(0,10,10) I want to have a function that assigns a value to a specific element of one of the matrix, as in foo <- function(s,i,j,value){ assign(paste("M_",s)[i,j],value) } This however does not work: foo('a',1,1,1) Error in paste("M_", s)[1, j] : incorrect number of dimensions Following the ?assign help, I tried foo2 <- function(s,i,j,value){ assign(paste("M_",s,"[i,j]"),value, envir = .GlobalEnv) } but this produces the problem I described above (namely, a new variable is created, rather than replacing the specified element of the matrix). I know that in this example I could simply pass the matrix as an argument to the function, but I am interested in understanding how 'assign' work. Many thanks for your help. Matteo __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] assigning values to elements of matrixes
Dear all, Thanks very much for your help. This completely clarifies my question. Mayteo Il giorno 23/dic/2015 17:05, "Anthoni, Peter (IMK)" ha scritto: > Hi, > > How about the following: > foo2 <- function(s,i,j,value) > { > M = get(paste("M_",s,sep="")) > M[i,j] = value > assign(paste("M_",s,sep=""),M, envir = .GlobalEnv) > } > > foo2("a",1,2,15) > > cheers > Peter > > > On 23 Dec 2015, at 09:44, Matteo Richiardi > wrote: > > > > I am following the example I find on ?assign: > > > > a <- 1:4 > > assign("a[1]", 2) > > > > This appears to create a new variable named "a[1]" rather than > > changing the value of the vector. > > > > Am I missing something here? How can I assign a value to a specified > > element of a vector/matrix? > > > > Of course, my problem is slightly more involved, but I guess the above > > is its core. For those interested, I have the two matrixes > > M_a <- matrix(0,10,10) > > M_b <- matrix(0,10,10) > > > > I want to have a function that assigns a value to a specific element > > of one of the matrix, as in > > foo <- function(s,i,j,value){ > > assign(paste("M_",s)[i,j],value) > > } > > > > This however does not work: > > > > foo('a',1,1,1) > > > > Error in paste("M_", s)[1, j] : incorrect number of dimensions > > > > Following the ?assign help, I tried > > > > foo2 <- function(s,i,j,value){ > > assign(paste("M_",s,"[i,j]"),value, envir = .GlobalEnv) > > } > > > > but this produces the problem I described above (namely, a new > > variable is created, rather than replacing the specified element of > > the matrix). > > > > I know that in this example I could simply pass the matrix as an > > argument to the function, but I am interested in understanding how > > 'assign' work. > > > > Many thanks for your help. > > > > Matteo > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] creating cubes
What is the best way to store data in a cube? That is, I need to create a data structure D with three indexes, say i,j,h, so that I can access each data point D[i,j,h], and visualise sections like D[i,j,] or D[,,h]. I have tried to create an array of matrixes: D <-matrix(matrix(NA,i,j),h) but then D[i] returns a number, and not a matrix. I imagine this is a dummy question, but I did search for an answer on various R help sites, and found nothing straightforward. Being an inexperienced R user, I prefer a simple solution, even at some efficiency cost. Many thanks for your help. Matteo [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] creating cubes
Hi Daniel, thanks for your answer. How can I populate the array with the matrixes? Suppose I want to populate it with 10 matrixes matrix(NA,5,5) Matteo On 15 January 2016 at 22:26, Dalthorp, Daniel wrote: > How about: D<-array(dim=c(d1, d2, d3))? > > > > On Fri, Jan 15, 2016 at 2:20 PM, Matteo Richiardi > wrote: >> >> What is the best way to store data in a cube? That is, I need to create a >> data structure D with three indexes, say i,j,h, so that I can access each >> data point D[i,j,h], and visualise sections like D[i,j,] or D[,,h]. >> >> I have tried to create an array of matrixes: >> >> D <-matrix(matrix(NA,i,j),h) >> >> but then D[i] returns a number, and not a matrix. >> >> I imagine this is a dummy question, but I did search for an answer on >> various R help sites, and found nothing straightforward. Being an >> inexperienced R user, I prefer a simple solution, even at some efficiency >> cost. >> >> Many thanks for your help. >> Matteo >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > > > > -- > Dan Dalthorp, PhD > USGS Forest and Rangeland Ecosystem Science Center > Forest Sciences Lab, Rm 189 > 3200 SW Jefferson Way > Corvallis, OR 97331 > ph: 541-750-0953 > ddalth...@usgs.gov > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] updating elements of a list of matrixes without 'for' cycles
Hi, following an earlier suggestion from the list, I am storing my data in a "cube", i.e. an array of matrixes. Is there any smarter way of updating the elements of the cube through a function, other than the three 'for' cycles in the example below? (please note that the example is simplistic; in particular, my function is more complicated). # parameters I <- 2L J <- 2L H <- 2L # data container: an array of matrixes mycube <- array(dim=c(I,J,H)) # initialisation for (h in 1:H) { init <- matrix(c(rep(0,J)),nrow=I,ncol=J) mycube[,,h] <- init } # function foo = function(i,j,h){ mycube[i,j,h] <<- i*j*h } # update for(h in 1:H){ # males: for(i in 1:I) for(j in 1:J) foo(i,j,h) } Thanks a lot for your help. Matteo __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Getting the index of specific quantiles
Dear R-users, a very easy one for you, I guess. I need to extract the indexes of the elements corresponding to different quantiles of a vector. When a quantile is an interpolation between two adjacent values, I need the index of the value which is closer (the lower value - or the higher value for what matters - in case the quantile is exactly half way through). This is an example. > x <- c(9,9,1,3,2,7,6,10,5,6) > quantile(x) 0% 25% 50% 75% 100% 1.0 3.5 6.0 8.5 10.0 What I need is a vector 'index' which looks like > index 3 4 7 1 8 Many thanks for your help ! Matteo __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting the index of specific quantiles
Thanks Sarah (and all the others who replied) for your precious suggestions! Matteo On 22 November 2016 at 14:18, Sarah Goslee wrote: > Here's how to get one: > > x <- c(9,9,1,3,2,7,6,10,5,6) >> which.min(abs(x - quantile(x, .25))) > [1] 4 > > And here's one of the various ways to get the entire set: > >> xq <- quantile(x) >> sapply(xq, function(y)which.min(abs(x - y))) > 0% 25% 50% 75% 100% >34 7 18 > > Sarah > > On Tue, Nov 22, 2016 at 7:21 AM, Matteo Richiardi > wrote: >> Dear R-users, >> a very easy one for you, I guess. I need to extract the indexes of the >> elements corresponding to different quantiles of a vector. When a >> quantile is an interpolation between two adjacent values, I need the >> index of the value which is closer (the lower value - or the higher >> value for what matters - in case the quantile is exactly half way >> through). >> >> This is an example. >> >>> x <- c(9,9,1,3,2,7,6,10,5,6) >>> quantile(x) >> 0% 25% 50% 75% 100% >> 1.0 3.5 6.0 8.5 10.0 >> >> What I need is a vector 'index' which looks like >> >>> index >> 3 4 7 1 8 >> >> Many thanks for your help ! Matteo >> > -- > Sarah Goslee > http://www.functionaldiversity.org __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How can I eliminate a loop over a data.table?
I've two data.tables as shown below: *** N = 10 A.DT <- data.table(a1 = c(rnorm(N,0,1)), a2 = NA)) B.DT <- data.table(b1 = c(rnorm(N,0,1)), b2 = 1:N) setkey(A.DT,a1) setkey(B.DT,b1) *** I tried to change my previous data.frame implementation to a data.table implementation by changing the for-loop as shown below: *** for (i in 1:nrow(B.DT)) { for (j in nrow(A.DT):1) { if (B.DT[i,b2] <= N/2 && B.DT[i,b1] < A.DT[j,a1]) { A.DT[j,]$a2 <- B.DT[i,]$b1 break } } } *** I get the following error message: *** Error in `[<-.data.table`(`*tmp*`, j, a2, value = -0.391987468746123) : object "a2" not found *** I think the way I access data.table is not quite right. I am new to it. I guess there is a quicker way of doing it than cycling up and down the two datatables. I'd like to know if the loop shown above could be simplified/vectorised. The data.table data for copy/paste reads: *** # A.DT a1 a2 1 -1.4917779 NA 2 -1.0731161 NA 3 -0.7533091 NA 4 -0.3673273 NA 5 -0.159569 NA 6 -0.1551948 NA 7 -0.0430574 NA 8 0.1783496 NA 9 0.4276034 NA 10 1.0697412 NA # B.DT b1 b2 1 0.64229018 1 2 1.00527902 2 3 0.24746294 3 4 -0.50288835 4 5 0.34447791 5 6 -0.22205129 6 7 0.60099079 7 8 -0.70242284 8 9 0.6298599 9 10 0.08917988 10 *** The output I expect is: *** # OUTPUT a1 a2 1 -1.4917779 NA 2 -1.0731161 NA 3 -0.7533091 NA 4 -0.3673273 NA 5 -0.159569 NA 6 -0.1551948 NA 7 -0.0430574 NA 8 0.1783496 -0.50288835 9 0.4276034 0.24746294 10 1.0697412 0.64229018 *** The algorithm goes down one table, and for each row go up the other table, check some conditions and modify values accordingly. More specifically, it goes down B.DT, and for each row in B.DT goes up A.DT and assigns to a2 the first value of b1 such that b1 is smaller than a1. An additional condition is checked before assignment (b2 being equal or smaller than 5 in this example). 0.64229018 is the first value in B.DT, and it is assigned to the last unit of A.DT. 1.00527902 is the second value in B.DT, but it is left unassigned because it is bigger than all other values in A.DT. 0.24746294 is the third value in B.DT, and it is assigned to the second last unit in A.DT. -0.50288835 is the fourth value in B.DT, and it is assigned to unit #8 in A.DT 0.34447791 is the fifth value in B.DT, and it is left unassigned because it is too big. This is of course a simplified problem (and therefore may not make much sense). Thanks for your time and input. Matteo __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How can I eliminate a loop over a data.table?
I've two data.tables as shown below: *** N = 10 A.DT <- data.table(a1 = c(rnorm(N,0,1)), a2 = NA)) B.DT <- data.table(b1 = c(rnorm(N,0,1)), b2 = 1:N) setkey(A.DT,a1) setkey(B.DT,b1) *** I tried to change my previous data.frame implementation to a data.table implementation by changing the for-loop as shown below: *** for (i in 1:nrow(B.DT)) { for (j in nrow(A.DT):1) { if (B.DT[i,b2] <= N/2 && B.DT[i,b1] < A.DT[j,a1]) { A.DT[j,]$a2 <- B.DT[i,]$b1 break } } } *** I get the following error message: *** Error in `[<-.data.table`(`*tmp*`, j, a2, value = -0.391987468746123) : object "a2" not found *** I think the way I access data.table is not quite right. I am new to it. I guess there is a quicker way of doing it than cycling up and down the two datatables. I'd like to know if the loop shown above could be simplified/vectorised. The data.table data for copy/paste reads: *** # A.DT a1 a2 1 -1.4917779 NA 2 -1.0731161 NA 3 -0.7533091 NA 4 -0.3673273 NA 5 -0.159569 NA 6 -0.1551948 NA 7 -0.0430574 NA 8 0.1783496 NA 9 0.4276034 NA 10 1.0697412 NA # B.DT b1 b2 1 0.64229018 1 2 1.00527902 2 3 0.24746294 3 4 -0.50288835 4 5 0.34447791 5 6 -0.22205129 6 7 0.60099079 7 8 -0.70242284 8 9 0.6298599 9 10 0.08917988 10 *** The output I expect is: *** # OUTPUT a1 a2 1 -1.4917779 NA 2 -1.0731161 NA 3 -0.7533091 NA 4 -0.3673273 NA 5 -0.159569 NA 6 -0.1551948 NA 7 -0.0430574 NA 8 0.1783496 -0.50288835 9 0.4276034 0.24746294 10 1.0697412 0.64229018 *** The algorithm goes down one table, and for each row go up the other table, check some conditions and modify values accordingly. More specifically, it goes down B.DT, and for each row in B.DT goes up A.DT and assigns to a2 the first value of b1 such that b1 is smaller than a1. An additional condition is checked before assignment (b2 being equal or smaller than 5 in this example). 0.64229018 is the first value in B.DT, and it is assigned to the last unit of A.DT. 1.00527902 is the second value in B.DT, but it is left unassigned because it is bigger than all other values in A.DT. 0.24746294 is the third value in B.DT, and it is assigned to the second last unit in A.DT. -0.50288835 is the fourth value in B.DT, and it is assigned to unit #8 in A.DT 0.34447791 is the fifth value in B.DT, and it is left unassigned because it is too big. This is of course a simplified problem (and therefore may not make much sense). Thanks for your time and input. Matteo [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] detecting autocorrelation structure in panel data
Hello, I'm a newby in R. I have created a data.frame holding panel data, with the following columns: "id","time","y", say: periods = 100 numcases = 100 df = data.frame( id = rep(1:numcases,periods), time = rep(1:periods, each = numcases) ) df = transform(df,y=c(rnorm(numcases*periods)+id) I want to check whether "y" is autocorrelated. I came across the acf() function, but I cannot understand how to specify that my time variable is "time" and that individuals are identified by their "id". I have also tried to convert my data.frame in a ts or a xts object, but got really lost there. I'd really apreciate help... Thank you, Matteo __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] efficient ways to dynamically grow a dataframe
Hi, I'm trying to write a small microsimulation in R: that is, I have a dataframe with info on N individuals for the base-year and I have to grow it dynamically for T periods: df = data.frame( id = 1:N, x = ) The most straightforward way to solve the problem that came to my mind is to create for every period a new dataframe: for(t in 1:T){ for(i in 1:N){ row = data.frame( id = i, t = t, x = ... ) df = rbind(df,row) } } This is very inefficient and my pc gets immediately stucked as N is raised above some thousands. As an alternative, I created an empty dataframe for all the projected periods, and then filled it: df1 = data.frame( id = rep(1:N,T), t = rep(1:T, each = N), x = rep(NA,N*T) ) for(t in 1:T){ for(i in 1:N){ x = ... df1[df1$id==i & df1$t==t,"x"] = x } } df = rbind(df,df1) This is also too slow, and my PC gets stucked. I don't want to go for a matrix, because I'd loose the column names and everything will become too much error-prone. Any suggestions on how to do it? Thanks in advance, Matteo -- Matteo Richiardi University of Turin Faculty of Law Department of Economics "Cognetti De Martiis" via Po 53, 10124 Torino Email: matteo.richia...@unito.it Tel. +39 011 670 3870 Web page: http://www.personalweb.unito.it/matteo.richiardi/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] using StatEt IDE for Eclipse
Hi, I'm trying to use StatEt IDE for Eclipse as my R editor, but I'm completely lost. I've read all I could find online, made apparently all I had to do (installing rj, configuraing StatEt, etc.) but still cannot make R running. Below is the error log file. Thank you so much for assistance. Matteo !ENTRY de.walware.statet.r.console.ui 1 0 2011-12-05 16:21:51.355 !MESSAGE Launching the R Console was cancelled, because it seems starting the R engine failed. Please make sure that R package 'rj' (1.0.0 or compatible) is installed and that the R library paths are set correctly for the R environment configuration 'R-2.14.0'. !SESSION 2011-12-05 16:23:23.552 --- eclipse.buildId=M20110909-1335 java.version=1.6.0_21 java.vendor=Sun Microsystems Inc. BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=it_IT Command-line arguments: -os win32 -ws win32 -arch x86_64 !ENTRY org.eclipse.osgi 2 1 2011-12-05 16:23:29.901 !MESSAGE NLS unused message: FindReplaceAction_label in: de.walware.ecommons.ui.SharedMessages !ENTRY org.eclipse.ui 4 4 2011-12-05 16:23:30.712 !MESSAGE Class load Failure: 'org.eclipse.ecf.docshare.menu.DocShareRosterMenuContributionItem' !ENTRY org.eclipse.ui 4 0 2011-12-05 16:23:30.714 !MESSAGE Class load Failure: 'org.eclipse.ecf.docshare.menu.DocShareRosterMenuContributionItem' !STACK 1 org.eclipse.core.runtime.CoreException: Plug-in de.walware.ecommons.ltk.ui was unable to load class org.eclipse.ecf.docshare.menu.DocShareRosterMenuContributionItem. at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.throwException(RegistryStrategyOSGI.java:194) at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:176) at org.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:905) at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:243) at org.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:55) at org.eclipse.ui.internal.util.Util.safeLoadExecutableExtension(Util.java:879) at org.eclipse.ui.internal.menus.DynamicMenuContributionItem.createContributionItem(DynamicMenuContributionItem.java:222) at org.eclipse.ui.internal.menus.DynamicMenuContributionItem.getContributionItem(DynamicMenuContributionItem.java:215) at org.eclipse.ui.internal.menus.DynamicMenuContributionItem.fill(DynamicMenuContributionItem.java:195) at org.eclipse.jface.action.MenuManager.doItemFill(MenuManager.java:741) at org.eclipse.jface.action.MenuManager.update(MenuManager.java:822) at org.eclipse.jface.action.MenuManager.update(MenuManager.java:682) at org.eclipse.ui.internal.menus.WorkbenchMenuService.updateManagers(WorkbenchMenuService.java:330) at org.eclipse.ui.internal.menus.WorkbenchMenuService$4.propertyChange(WorkbenchMenuService.java:316) at org.eclipse.ui.internal.services.EvaluationAuthority$1.run(EvaluationAuthority.java:252) at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) at org.eclipse.ui.internal.services.EvaluationAuthority.fireServiceChange(EvaluationAuthority.java:246) at org.eclipse.ui.internal.services.EvaluationAuthority.endSourceChange(EvaluationAuthority.java:197) at org.eclipse.ui.internal.services.EvaluationAuthority.sourceChanged(EvaluationAuthority.java:135) at org.eclipse.ui.internal.services.ExpressionAuthority.sourceChanged(ExpressionAuthority.java:311) at org.eclipse.ui.internal.services.ExpressionAuthority.sourceChanged(ExpressionAuthority.java:290) at org.eclipse.ui.AbstractSourceProvider.fireSourceChanged(AbstractSourceProvider.java:99) at org.eclipse.ui.internal.services.WorkbenchSourceProvider.checkActivePart(WorkbenchSourceProvider.java:401) at org.eclipse.ui.internal.services.WorkbenchSourceProvider.checkActivePart(WorkbenchSourceProvider.java:300) at org.eclipse.ui.internal.services.WorkbenchSourceProvider.handleCheck(WorkbenchSourceProvider.java:286) at org.eclipse.ui.internal.services.WorkbenchSourceProvider.checkOtherSources(WorkbenchSourceProvider.java:855) at org.eclipse.ui.internal.services.WorkbenchSourceProvider$6.handleEvent(WorkbenchSourceProvider.java:839) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Display.filterEvent(Display.java:1262) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1052) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1058) at org.eclipse.swt.widgets.Decorations.WM_ACTIVATE(Decorations.java:1647) at org.eclipse.swt.widgets.Shell.WM_ACTIVATE(Shell.java:2137) at org.eclipse.swt.widgets.Control.windowProc(Control.java:4525) at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:341) at org.eclipse.swt.widgets.Decorations.windowProc(Decorations.java:1610) at org.eclipse.swt.widgets.Shell.windowProc(Shell.java:2061) at
[R] using StatEt IDE for Eclipse
Hi, I'm trying to use StatEt IDE for Eclipse as my R editor, but I'm completely lost. I've read all I could find online, made apparently all I had to do (installing rj, configuraing StatEt, etc.) but still cannot make R running. Below is the error log file. Thank you so much for assistance. Matteo !ENTRY de.walware.statet.r.console.ui 1 0 2011-12-05 16:21:51.355 !MESSAGE Launching the R Console was cancelled, because it seems starting the R engine failed. Please make sure that R package 'rj' (1.0.0 or compatible) is installed and that the R library paths are set correctly for the R environment configuration 'R-2.14.0'. !SESSION 2011-12-05 16:23:23.552 --- eclipse.buildId=M20110909-1335 java.version=1.6.0_21 java.vendor=Sun Microsystems Inc. BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=it_IT Command-line arguments: -os win32 -ws win32 -arch x86_64 !ENTRY org.eclipse.osgi 2 1 2011-12-05 16:23:29.901 !MESSAGE NLS unused message: FindReplaceAction_label in: de.walware.ecommons.ui.SharedMessages !ENTRY org.eclipse.ui 4 4 2011-12-05 16:23:30.712 !MESSAGE Class load Failure: 'org.eclipse.ecf.docshare.menu.DocShareRosterMenuContributionItem' !ENTRY org.eclipse.ui 4 0 2011-12-05 16:23:30.714 !MESSAGE Class load Failure: 'org.eclipse.ecf.docshare.menu.DocShareRosterMenuContributionItem' !STACK 1 org.eclipse.core.runtime.CoreException: Plug-in de.walware.ecommons.ltk.ui was unable to load class org.eclipse.ecf.docshare.menu.DocShareRosterMenuContributionItem. at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.throwException(RegistryStrategyOSGI.java:194) at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:176) at org.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:905) at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:243) at org.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:55) at org.eclipse.ui.internal.util.Util.safeLoadExecutableExtension(Util.java:879) at org.eclipse.ui.internal.menus.DynamicMenuContributionItem.createContributionItem(DynamicMenuContributionItem.java:222) at org.eclipse.ui.internal.menus.DynamicMenuContributionItem.getContributionItem(DynamicMenuContributionItem.java:215) at org.eclipse.ui.internal.menus.DynamicMenuContributionItem.fill(DynamicMenuContributionItem.java:195) at org.eclipse.jface.action.MenuManager.doItemFill(MenuManager.java:741) at org.eclipse.jface.action.MenuManager.update(MenuManager.java:822) at org.eclipse.jface.action.MenuManager.update(MenuManager.java:682) at org.eclipse.ui.internal.menus.WorkbenchMenuService.updateManagers(WorkbenchMenuService.java:330) at org.eclipse.ui.internal.menus.WorkbenchMenuService$4.propertyChange(WorkbenchMenuService.java:316) at org.eclipse.ui.internal.services.EvaluationAuthority$1.run(EvaluationAuthority.java:252) at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) at org.eclipse.ui.internal.services.EvaluationAuthority.fireServiceChange(EvaluationAuthority.java:246) at org.eclipse.ui.internal.services.EvaluationAuthority.endSourceChange(EvaluationAuthority.java:197) at org.eclipse.ui.internal.services.EvaluationAuthority.sourceChanged(EvaluationAuthority.java:135) at org.eclipse.ui.internal.services.ExpressionAuthority.sourceChanged(ExpressionAuthority.java:311) at org.eclipse.ui.internal.services.ExpressionAuthority.sourceChanged(ExpressionAuthority.java:290) at org.eclipse.ui.AbstractSourceProvider.fireSourceChanged(AbstractSourceProvider.java:99) at org.eclipse.ui.internal.services.WorkbenchSourceProvider.checkActivePart(WorkbenchSourceProvider.java:401) at org.eclipse.ui.internal.services.WorkbenchSourceProvider.checkActivePart(WorkbenchSourceProvider.java:300) at org.eclipse.ui.internal.services.WorkbenchSourceProvider.handleCheck(WorkbenchSourceProvider.java:286) at org.eclipse.ui.internal.services.WorkbenchSourceProvider.checkOtherSources(WorkbenchSourceProvider.java:855) at org.eclipse.ui.internal.services.WorkbenchSourceProvider$6.handleEvent(WorkbenchSourceProvider.java:839) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Display.filterEvent(Display.java:1262) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1052) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1058) at org.eclipse.swt.widgets.Decorations.WM_ACTIVATE(Decorations.java:1647) at org.eclipse.swt.widgets.Shell.WM_ACTIVATE(Shell.java:2137) at org.eclipse.swt.widgets.Control.windowProc(Control.java:4525) at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:341) at org.eclipse.swt.widgets.Decorations.windowProc(Decorations.java:1610) at org.eclipse.swt.widgets.Shell.windowProc(Shell.java:2061) at org