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:
Img of pre and post rows of data with arrows showing originals in post table
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?
Here is how I would do all the process:
Let's create an initial data frame:
shape = c("square","square","square","square","square","line","line","line","triangle","triangle","triangle","triangle")
PathID = c(1.1,1.1,1.1,1.1,1.1,1,1,1,1.1,1.1,1.1,1.1)
PathOrder = c(1,2,3,4,5,1,2,3,1,2,3,4)
xCoord = c(1,1,2,2,1,5,4,7,2,3,3,2)
yCoord = c(3,4,4,3,3,5,4,7,1,-1,1,1)
df = data.frame(shape,PathID,PathOrder,xCoord,yCoord)
Initial data:
> df
shape PathID PathOrder xCoord yCoord
1 square 1.1 1 1 3
2 square 1.1 2 1 4
3 square 1.1 3 2 4
4 square 1.1 4 2 3
5 square 1.1 5 1 3
6 line 1.0 1 5 5
7 line 1.0 2 4 4
8 line 1.0 3 7 7
9 triangle 1.1 1 2 1
10 triangle 1.1 2 3 -1
11 triangle 1.1 3 3 1
12 triangle 1.1 4 2 1
Process:
# Keep only PathId == 1.1
df = df[df$PathID == 1.1, ]
# Create a new ID to start at 61 for each polygon
df$NewPathID = c(61, seq(nrow(df)-1) + 61 + cumsum(diff(df$PathOrder)-1))
# Remove the last point for each polygon
df = df[!c(diff(df$NewPathID) < 0, TRUE),]
# Set the new path order to 2
df$NewPathOrder = 2
# Create a new id for each row (will be needed to reorder later)
df$ID = seq(nrow(df))
# create a copy in order to set the path order to 1 and the coordinates to 0
df2 = df
df2$NewPathOrder = 1
df2$xCoord = 0
df2$yCoord = 0
# add df2 as new rows and reorder
df = rbind(df2,df)
df = df[order(df$ID),]
# replace columns and remove temporary ones
df$PathID = df$NewPathID
df$PathOrder = df$NewPathOrder
df$NewPathID = NULL
df$NewPathOrder = NULL
df$ID = NULL
Result:
> df
shape PathID PathOrder xCoord yCoord
1 square 61 1 0 0
12 square 61 2 1 3
2 square 62 1 0 0
21 square 62 2 1 4
3 square 63 1 0 0
31 square 63 2 2 4
4 square 64 1 0 0
41 square 64 2 2 3
9 triangle 61 1 0 0
91 triangle 61 2 2 1
10 triangle 62 1 0 0
101 triangle 62 2 3 -1
11 triangle 63 1 0 0
111 triangle 63 2 3 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.