[R] use of tapply?

From: tom wright <tom_at_maladmin.com>
Date: Fri 30 Dec 2005 - 02:43:27 EST


I'm still learning how to program with R and I was hoping someone could take the time to show me how I can rewrite this code? Many thanks
Tom

data.intersects<-data.frame(
    x=c(0.230,0.411,0.477,0.241,0.552,0.230),
    y=c(0.119,0.515,0.261,0.431,0.304,0.389),
    angle=vector(length=6),
    length=vector(length=6),
    row.names=c('tbr','trg','dbr','dbg','pbr','pbg'))     

calcDist<-function(x,y){

    #calcualates distance from origin (C)     origin<-data.frame(x=0.34,y=0.36)
    dx<-origin$x-x
    dy<-origin$y-y

    length<-sqrt(dx^2+dy^2)
    angle<-asin(dy/length)
    return(list('length'=length,'angle'=angle))
}

for(iLoc in 1:length(data.intersects[,1])){

    result<-calcDist(data.intersects[iLoc,]$x,data.intersects[iLoc,]$y)     data.intersects[iLoc,]$angle<-result$angle     data.intersects[iLoc,]$length<-result$length
}



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Dec 30 07:41:42 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:44 EST