我正在嘗試在大型數據框中編制索引。 sdata框架有300萬觀察值和26個變量(在底部)。R中的向量化索引
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
}
gazedist <- matrix(nrow=nrow(sdata), ncol = setsize)
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)
}
sdata$gdist_T <- 0
sdata$gdist_T <- gazedist[ ,sdata$t_targLoc]
這裏的最後一行導致R崩潰。有什麼方法可以將sdata $ t_targLoc [i]的值插入gazedistance [i,d]的d值中。 for循環相當於將是:
for(i in 1:length(gazedist)){
sdata$gdist_T[i] <- gazedist[i,sdata$t_targLoc[i]]
}
但是,這將是R中慢...
SDATA結構
structure(list(RIGHT_GAZE_X = c(409.5, 409.6, 409.5, 409.4, 409.3,
409.2, 409.1, 409, 408.9, 408.8), RIGHT_GAZE_Y = c(291.9, 291.5,
290.9, 290.3, 290.3, 290.3, 289.8, 289.2, 288.7, 288.8), RECORDING_SESSION_LABEL = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "ET101", class = "factor"),
t_block = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), t_trialNum = c(129L,
129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L), t_subjNum = c(101L,
101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L), t_colCond = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "green", class = "factor"),
t_targLoc = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), t_targID = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "L", class = "factor"),
t_targShape = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "diamond", class = "factor"), t_singLoc = c(5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), t_singPres = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "P", class = "factor"),
t_singDist = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), t_singAngle = c(120L,
120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L), t_targAngle = c(120L,
120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L), t_RESP = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "L", class = "factor"),
t_ACC = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), t_RT = c(686.1062,
686.1062, 686.1062, 686.1062, 686.1062, 686.1062, 686.1062,
686.1062, 686.1062, 686.1062), TRIAL_START_TIME = c(1031031L,
1031031L, 1031031L, 1031031L, 1031031L, 1031031L, 1031031L,
1031031L, 1031031L, 1031031L), TIMESTAMP = c(1031030, 1031032,
1031034, 1031036, 1031038, 1031040, 1031042, 1031044, 1031046,
1031048), IP_START_TIME = c(1031031L, 1031031L, 1031031L,
1031031L, 1031031L, 1031031L, 1031031L, 1031031L, 1031031L,
1031031L), currtime = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18
), currsamp = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), gdist_T = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), gdist_S = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0), gdist_NS = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("RIGHT_GAZE_X",
"RIGHT_GAZE_Y", "RECORDING_SESSION_LABEL", "t_block", "t_trialNum",
"t_subjNum", "t_colCond", "t_targLoc", "t_targID", "t_targShape",
"t_singLoc", "t_singPres", "t_singDist", "t_singAngle", "t_targAngle",
"t_RESP", "t_ACC", "t_RT", "TRIAL_START_TIME", "TIMESTAMP", "IP_START_TIME",
"currtime", "currsamp", "gdist_T", "gdist_S", "gdist_NS"), row.names = 53170:53179, class = "data.frame")
You rea讓你不必上傳所有的行,也許只有前10個? –
「這裏的最後一行導致R崩潰。」 - >什麼錯誤信息?這條線本身是可以的,完全等同於循環(所以無論工作還是失敗)。 –