Basically I have sumpwver that is a list of a lot of projects and the costs of those projects. I then have negpw which is only the negative projects in sumpwver. With the loop I'm trying to determine how many positive values in sumpwver match the negative values in negpw. when I run the loop there are no errors, but it only creates one row in matches and I know there are more instances. For example, you can see from the head of both sets that the first two rows of sumpwver should be listed as a match.
Also, the really weird thing is that when I try and find the row in the sumpwver set that corresponds with the dollar value pulled into matches, it won't recognize it. But, if I search sumpwver by the project number, it will.
Any help you could offer would be greatly appreciated.
(dput(droplevels(head(pw))))
structure(list(Disaster.Number = c(1310L, 1310L, 1310L, 1310L,
1310L, 1310L), PW.Number = c(1L, 2L, 3L, 4L, 4L, 5L), Version.Number = c(0L,
0L, 0L, 0L, 1L, 0L), Total.Obligated = c(316180, -316180, 2659.69,
345794.29, -28929.91, 40344.08)), .Names = c("Disaster.Number",
"PW.Number", "Version.Number", "Total.Obligated"), .internal.selfref = <pointer: (nil)>, row.names = c(NA,
6L), class = c("data.table", "data.frame"))
Disaster.Number PW.Number Version.Number Total.Obligated
1 1310 1 0 316180.00
2 1310 2 0 -316180.00
3 1310 3 0 2659.69
4 1310 4 0 345794.29
5 1310 4 1 -28929.91
6 1310 5 0 40344.08
sumpwver <- setDT(pw)[,.(Total.Obligated=sum(Total.Obligated)),
by = .(dr.pw=paste(Disaster.Number, PW.Number, sep="pw"))]`
(dput(droplevels(head(sumpwver))))
structure(list(dr.pw = c("1310pw1", "1310pw2", "1310pw3", "1310pw4",
"1310pw5", "1310pw6"), Total.Obligated = c(316180, -316180, 2659.69,
316864.38, 40401.82, 162751.59)), .Names = c("dr.pw", "Total.Obligated"
), .internal.selfref = <pointer: (nil)>, row.names = c(NA, 6L
), class = c("data.table", "data.frame"))
dr.pw Total.Obligated
1 1310pw1 316180.00
2 1310pw2 -316180.00
3 1310pw3 2659.69
4 1310pw4 316864.38
5 1310pw5 40401.82
6 1310pw6 162751.59
negpw <- sumpwver[sumpwver$Total.Obligated < 0,]
(dput(droplevels(head(negpw))))
structure(list(dr.pw = c("1310pw2", "1310pw10", "1310pw37", "1310pw268",
"1310pw270", "1311pw10"), Total.Obligated = c(-316180, -1742.78,
-0.01, -8679.84, -76.34, -3835294.19)), .Names = c("dr.pw", "Total.Obligated"
), .internal.selfref = <pointer: (nil)>, row.names = c(NA, 6L
), class = c("data.table", "data.frame"))
dr.pw Total.Obligated
1 1310pw2 -316180.00
2 1310pw10 -1742.78
3 1310pw37 -0.01
4 1310pw268 -8679.84
5 1310pw270 -76.34
6 1311pw10 -3835294.19
matches <- data.frame(dr.pw=character(), Total.Obligated=numeric(), stringsAsFactors=FALSE)`
for (i in nrow(negpw)) {
if (any(sumpwver$Total.Obligated == abs(negpw$Total.Obligated[i]))) {
matches[nrow(matches)+1,"Total.Obligated"] <- negpw$Total.Obligated[i]
matches[nrow(matches),"dr.pw"] <- negpw$dr.pw[i]
}
}
(dput(droplevels(head(matches))))
structure(list(dr.pw = "4211pw133", Total.Obligated = -7.27595761418343e-12), .Names = c("dr.pw",
"Total.Obligated"), row.names = 1L, class = "data.frame")
dr.pw Total.Obligated
1 4211pw133 -7.275958e-12
sumpwver[sumpwver$Total.Obligated==-7.275958e-12,]
[1] dr.pw Total.Obligated
<0 rows> (or 0-length row.names)
sumpwver[sumpwver$Total.Obligated=="-7.275958e-12",]
[1] dr.pw Total.Obligated
<0 rows> (or 0-length row.names)
sumpwver[sumpwver$Total.Obligated==-7.275958*10^-12,]
Empty data.table (0 rows) of 2 cols: dr.pw,Total.Obligated
sumpwver[sumpwver$dr.pw=="4211pw133",]
dr.pw Total.Obligated
626721 4211pw133 -7.275958e-12