mercredi 7 décembre 2016

For If loop to add rows of data with copied columns in R

I am trying to recreate an R process that a vendor did for my work. Below is the text "documentation" available:

For any polygon, the data generated by R script needs to be modified before it can be loaded into the database. Follow the following 3 steps to modify the data:

  1. Remove all records where Shape="line" (orange fill below)
  2. For each of the x,y coordinates where Path ID = 1.1 and Path Order = 1 to n (n=number of sides of polygon), do the following:
    a. Add 2 extra records with Path ID starting from 61 for each record and Path Order = 1 and 2
    b. For 1st record where Path ID = 61 and Path Order = 1, set (0,0) as the (x,y) coordinates. This sets the vertex lines to start from origin
    c. For Path ID = 61 and Path Order = 2, copy the x,y coordinates from Path ID = 1.1 and Path Order = 1. This sets the end-point of the vertex line at the radius greater than 1 d. Repeat this for the other points satisfying criteria defined in step 2. e. Once done for all points, delete all records where Shape = “triangle” and PathID = “1.1”

Img of pre and post rows of data with arrows showing originals in post table

  1. The rest of the records for that polygon remain as is.

I have tried to recreate step 2 via a for if loop, but am not able to get the records where 'Path Order'=2 to work:

## prep for loop
newrec61 = NULL
newrec62 = NULL
xcoor62 = NULL
ycoor62 = NULL

## use an for if loop
for (i in 1:10) {
        ## for decagon
if( (dfShapePoints$pathID = 1.1)&(dfShapePoints$pathOrder = i) ) {
    newrec61 <- matrix(ncol=5, nrow=i)
    newrec61[i,]  <- cbind("decagon", 60+i, 1, 0, 0)

   if( (dfShapePoints$pathID = 1.1)&(dfShapePoints$pathOrder = 1) ) {
    ##xcoor62[i,]   <-  dfShapePoints$xCoordinate
}
    if( (dfShapePoints$pathID = 1.1)&(dfShapePoints$pathOrder = 1) ) {
    ##ycoor62[i,]   <-  dfShapePoints$yCoordinate
    }
   ## newrec62 <- matrix(ncol=5, nrow=i)
   ## newrec62[i,]  <- cbind("decagon", 60+i, 2, xcoor62[i], ycoor62[i]))
  ##dfShapePoints <- rbind(dfShapePoints, newrec61, newrec62)
}
}

You can see where I commented out the rest of the work as it is a more complex copy of where I am failing at. I am cbinding 10 entirely null rows and one row of "decagon" "70" "1" "0" "0". Why is my loop not working?

Aucun commentaire:

Enregistrer un commentaire