Jens Bölte
2009-Jul-06 15:21 UTC
[R] how to apply a self-written function to a data frame
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
Richard.Cotton at hsl.gov.uk
2009-Jul-06 16:17 UTC
[R] how to apply a self-written function to a data frame
> 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 > "This means that pp is a vector (and hence pp > 1 is also a vector), but 'if' requires a scalar logical value. Try, e.g. if(1:10 > 5) message("foo") for a simpler way of recreating this error message.> 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)By the looks of things, when you call apply(final, 1, abst, gx,gy,tx,ty,p_pos), the value of pp (inside the abst function) is being taken as the value of p_pos, which is all the relevant columns of the data frame. Are you sure that this is the value you meant for pp? There is a vectorised version of if, namely the ifelse function. Perhaps the line if (pp > 1) {px = -px } should be px <- ifelse(pp > 1, -px, px) or even more simply, px[pp > 1] <- -px[pp > 1] Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}