Find the elements that sums closest to zero value

df <- structure(list(key_var = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = "XYZ_1234", class = "factor"),
Amount = c(8200304.5, 8160830.25, -8035850.35, -7843855.06,
-7638726.82, 7635197.95, 6947059.96, -6779376.16, -6659630.59,
-6538178.03, 3890858.28, 3727088.57, 3440399.02, 2612664.47,
2147241.37, -1381553.09, -1307455.22, 1253244.05, 1020172.5,
-1018263.84, 893138.6, -892595.1, -676137.21, 565106.18,
-451752.19, -444984.92, -333922.62, -333922.39, -235644.35,
-159120.68, 145970.31, 124236.96, 124160.23, -98276.99, -88602.23,
-66468.98, 61162.81, 24832.04, 13469.54, -3788.55, 995.66,
-543.5, 511.52, 0.96, 0.87, 0.86, 1.66, -1077622.65, 1035065.78,
281748.19, 36316.05, 21011.73, -13143.08, -11365.96, 5528.03,
3822.78, -1809.79, -18.22, -1.81)), class = "data.frame", row.names = c(NA,
-59L), .Names = c("key_var", "Amount"))

I have a data frame it contains key_var and Amount field as shown below:

Overall Sum of the Amount for the particular key_var should come between 0 to 1 (0-0.99) after removing the particular records/elements, Now I need to identify those removable records and create a 'FLAG' against them (by creating a new var as FLAG). There can be multiple combinations generated but I need only one combination of set to be generated using R.

FYI, If we remove the last 12 records/elements from the below data set then sum matches to 0.25. This is doing manually now, I need to generate R code to automate the same.

For Ex. Let's say one unique key has 60 records and overall sum of the amount say 20k. Now my target is to make overall sum of the amount fall between 0 to 1. So I need to identify the records which can be dropped so that it makes to reach my target ( 0 -1). Just need a flag against removable records out of 60 records. There can be multiple combinations to make it out, but need at least one combination of set that can identify the flags for removable records so that the overall sum of the amount matches between 0 to 1. Now currently people are identifying this manually using trial and error basis. Need to automate the R Code, Need your help.

Welcome to community!

Would you mind converting your code into a reproducible example (reprex)?

I am a bit confused what you input is, what you want to do, what you hope to get as output. Currently your code delivers an error related to certain objects nothing having been set up (specifically, numbers).

@EconomiCurtis

I have modified the question, can you please help me out.?

Thanks,
Vinayak

Find all the possible orders of the vector, and create a cumsum for each one, then find the one with lowest abs value.

Hi Peter_Griffin,

Could you please provide the sample code, if you have any?

Thanks,
Vinayak