Vectorizing Large For Loop in R -


i trying vectorize large loop in r. sdata frame has 3 million observations , 26 variables (i can't upload here).

setsize <- 6 eccent <- 150 ctrx <- 400 ctry <- 300  xyrotate <- function(x,y,ctrx,ctry,angle){   distx <- x - ctrx;   disty <- y - ctry;   radians <- angle * (pi/180);   rotx <- ctrx + (distx*cos(radians)) - (disty*sin(radians));   roty <- ctry + (distx*sin(radians)) + (disty*cos(radians));   coordinates <- list("x" = rotx,"y" = roty)   return(coordinates) }  loc <- data.frame(x = numeric(setsize),               y = numeric(setsize)) loc$x[1] <- ctrx loc$y[1] <- ctry - eccent for(i in 2:setsize){   coord <- xyrotate(loc$x[1], loc$y[1],ctrx,ctry,(i-1)*(360/setsize))    loc$x[i] <- coord$x   loc$y[i] <- coord$y } for(d in 1:setsize){   x <- sdata$right_gaze_x-loc$x[d]   y <- sdata$right_gaze_y-loc$y[d]   gazedist[,d] <- sqrt(x^2+y^2) } 

with code, keep getting error:

error in gazedist[, d] <- sqrt(x^2 + y^2) :    incorrect number of subscripts on matrix 

any ideas on how fix it? gazedist[d, ] not work either.

how define gazedist? code above works assuming sdata data.frame, , gazedist defined gazedist <- matrix(nrow=nrow(sdata), ncol = setsize)


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -