[R] use of tapply?

tom wright tom at maladmin.com
Thu Dec 29 16:43:27 CET 2005


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
}




More information about the R-help mailing list