[R] how to apply a self-written function to a data frame
Jens Bölte
boelte at psy.uni-muenster.de
Mon Jul 6 17:21:13 CEST 2009
Hello,
I have written a function in order to analyse gaze paths. It works with the test data but when I try to apply the function to a data frame that stores "the real data" in columns I receive the error message that the
" In if (pp > 1) { :
condition has length > 1 only the first element will be used
"
I interpret this error message as saying that only the first element of pp is used. However, I'd like to analyse each row of the data frame, row by row. Using
apply(final, 1, abst, gx,gy,tx,ty,p_pos) (with gx - p_pos being columns of the data frame final)
gives the error message
Error in FUN(newX[, i], ...) :
unused argument(s) (c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, aso
self-written function
abst=function(gx, gy, tx, ty, pp){
# Datum 22.6. 09 - 17:24
# gx, gy Gaze-Spur
# fx, fy Fixationspunkt
# tx, ty Target-Position
# pp Prime-Position (von 0 - 3)
px = 2*(pp%%2)-1
if (pp > 1) {px = -px }
py = 2*(pp%/%2)-1
px = fx + px * abs(tx - fx)
py = fy + py * abs(ty - fy)
# Koordinaten des Mittelpunkts
fx=512
fy=393
# Transformation auf Mittelpunkt (von links/oben auf Zentrum von T und P)
# tx=tx+90
# ty=ty-90
# px=px+90
# py=py-90
# Gerade f-t
a=(ty-fy)/(tx-fx)
b=(fx*ty-fy*tx)/(tx-fx)
# Senkrechte von gaze auf Gerade f-t
as=-1/a
bs=gy-gx*as
# Abstand gaze von Gerade f-t
lx=-(b-bs)/(a-as)
ly=((a+as)*lx+(b+bs))/2
# Vorzeichen
vp=2*(py<a*px+b)-1
vg=2*(gy<a*gx+b)-1
# gerichteter Abstand gaze von Gerade f-t (in pixel)
vg*vp*sqrt((lx-gx)^2+(ly-gy)^2)
}
thanks a lot in advance
Jens Bölte
More information about the R-help
mailing list