Subsetting for proportional representation in R
I can't wrap my tiny brain around this one. One dataframe contains observations, each with a gender and an age bracket. I'm trying to write a function that returns a subset of the rows of this dataframe where each agegender combination appears in a proportion roughly equal to the value in the "props" dataframe. Ideally, the function will trim as few observations as possible. The results can be approximate (By approximate/roughly equal, I mean that each group's representation in the output should be at least within 5% of the desired proportion, and generally as low as possible).
ages < c("1829", "3039", "4049", "5059","60+")
genders < c("M","F")
set.seed(101)
df < data.frame("id" = paste0("p",c(1:500)),
"gender" = sample(genders, replace=TRUE, size=500),
"age" = sample(ages, replace=T, size=500))
props < data.frame("age" = c(ages, ages),
"gender" = genders,
"pcts" = c(.0835, .1145, .1145, .1145, .073, .0835, .1145,
.1145, .1145, .073))
select_max < function(df, props) {
....
return(subset)
}
I experimented with solutions using least common multiples and greatest common divisors, but these fell apart when the proportions didn't work nicely together. I'm considering a solution which adds and subtracts rows one at a time until it gets close enough to the desired proportions, but I feel there must be some more elegant solution. All help is appreciated. This is a fun one, for sure.
See also questions close to this topic

How to write for loop in R
Hello i need to calculate the following loop in R:
for (i in seq(0.004, 0.009, by=0.001)) { M[i] < mvdc(copula=tCopula(i,dim=2,df=df), margins=c("norm","norm"),paramMargins=list(list(mean=apple_mu, sd=apple_sd),list(mean=microsoft_mu, sd=microsoft_sd))) simulation [i]< rMvdc(3009,M[i]) error[i]<apple.returnssimulation[i]
}
I have the following error :
Error in M[i] < mvdc(copula = tCopula(i, dim = 2, df = df), margins = c("norm", : incompatible types (from S4 to logical) in subassignment type fix
Any ideas what i am doing wrong?
Thank you

unexpected symbol in copying a cURL  R
I am copying a cURL from the google chrome console, I paste it in my R script between "" and when I am trying to save it to an object, R says there is an unexpected symbol in it.
Can someone explain why this is and how to solve it?
httpbinrhcurl < "curl 'http://www.domainia.nl/quarantaine/2018/12/15' H 'Connection: keepalive' H 'CacheControl: maxage=0' H 'UpgradeInsecureRequests: 1' H 'UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36' H 'Origin: http://www.domainia.nl' H 'ContentType: application/xwwwformurlencoded' H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' H 'Referer: http://www.domainia.nl/quarantaine/2018/12/15' H 'AcceptEncoding: gzip, deflate' H 'AcceptLanguage: enUS,en;q=0.9,nl;q=0.8' H 'Cookie: ASP.NET_SessionId=1rq1dcm2rxrxhejcv2apj1nz; _ga=GA1.2.1720697664.1544465383; cookieconsent_dismissed=yes; _gid=GA1.2.1174161929.1544888026; _gat=1' data '__EVENTTARGET=ctl00%24ContentPlaceHolder1%24gvDomain&__EVENTARGUMENT=Page%246&__VIEWSTATE=%2BfCpnCxB3CBeL3p0AJRxX709sVZd46FhL5m2WythvucpdaUpCnCyUxSa%2BEidpSc9wEzRF6wNxg8LpEQV8%2BzUZpzAPL8nX4hXXd282D77F%2BphaIBhktpf4j8Wj31S3LIC5QRM2V6lHEWuJEbAJbuk68wwOBwa2zOVseYwNs%2FQywJT2H5wWBPzxALrQo5Dui76GArI3RRCSyKUgrq97euwYy4zq5b1Y3NgFWi8nOyu4VWuih%2BRANxczyqirApMmInEpCVFHqbNYulM8iKpi5Ay1jU3k2fC8I87LhC8oFWOPglZXhMuMSxR66FM%2BMOk9T%2BUbSrESAFA9AIywFhk7gRy%2FGMTgmDA%2BdyCbmnnIzJbGy6sr02V0I08l8UxzxfiaRCTx1paJqN3Co7tgq4D%2FqPmkvzlU8w1sMEqpdDWrXnX2cDswYbcEmg01w3PfgD66f4yezNJb4PQdjURDLwYsb6BkPum9iwXzRMi9OHQoULpN%2BUGFZh2BqgJMcI6R9OLbTZXs%2B%2FiiIVwyO8zeYjw8gMIjLZeu2TTdduhgiVBLUbBRZOQ%2BZwAIL6rZNoKF7Vop0BeSXTuA%2F6bCf1Z5g9Yss4WC%2Bre81YbVKImtrnUoqBsRQLvt6EtDUYxxJ1NDfdWYC8HivFw1FHvzwf0EiH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iOgLwnIYyBgRGoIOFsBhcauh0UO8dIMTNi6LRXiFpIK8VXEa4kG8enfIoJWlj2Yp06FL8mxpQXpQJkZrajseuQ5gtxk6vh3ZanVn3XDg5BrBxB2zUZNDmcVlSWOrw%2Fza6g9mm52q%2Ff%2FtdxrS4xYrTcJcavDkt%2Bhh4Y7Brhf3x0oDtyzk6WeLj5l13ZNjEcnqmhRlluY2Y1VCr9fQzrJFH8NZBiKa4pNVv2lfTKXqje1AogYX9LqUC4JoVfkXT1ip9%2BATWAApQpW7Z%2FjxXcUzN53xZopDP3UtpWQ8uPhBwaz2FRlNpQV7cv1QyoawOZDqIck88J9yEUiDKYDHczSkgK5AAeTENbJZSsxtMUJxIA97XyRWunk5WqmixcAQW8GV5QFT8c3yS35TafJ0bNW5U5vN6BByZiGAJg96r8sJrwZ6RkkFXHgPDGlivdxc594qqEDQTAaBo%2Fj2AJjk%2Frs793XMP61%2B6ahQIF9iAqoA8Iq6RaJSgs%2FOP%2BVrsONlNv%2BXCtndmE97M602%2F%2BVJmrQle%2F8ySQLlrmBdafdJGBSZBNrDeqhS4%2Bs9dbTarV7AvUwVjZUgTIhJ1JTgGGu09kgVOe5FDd89KJ6D9xFNUzAMJiyK7H%2BbX3Mf5KdZgmVnyehE%3D&ctl00%24ddlState=quarantaine&ctl00%24tbSearch=' compressed"

Interpreting uplift curve for treatment
I obtained the attached uplift plot for my data (using uplift R package) Basically i am looking at finding certain % of population for which the treatment is effective and will not have any chronic disorders after treatment. But i do not know how to interpret this plot? Does it mean i can select 40% of population for effective treatment without much risk when compared to including entire population?

Function with arrays as a parameter
how can I write a rubyfunction, that can calculate the average of an array? It should return the result. If the array hasn't got any elements the result should be 0. I should use a loop for the implementation! I started like this, but I'm not quite sure how to use the loop...
a = [1, 2, 3, 4, 5, 6] def average(a) sum = 0.0 result = 0.0 if array.length > 0 then array.each do item sum += item end result = sum / array.length end return result.to_f end

jQuery Form won't hide and function return undefined
I'm having a problem with one the buttons. It doens't work properly.
When I click "Add Run" button and then "Home" button almost nothing work. The form that is dynamically created doesn't hide and search input doesn't show, and function return undefined.$("#home").on("click", function () { $("#search").show(); $("#addForm").hide(); $("#noPrevious").hide(); complete() console.log(complete()) });
But on the other hand, some things work, they hide or show. For example, after refresh, when I click "All my Runs" it displays text, which will hide after clicking on "Home" button, and on top of that, search input field shows up. I don't understand this. Does anybody know what's wrong?
Thanks in advance.
Here is CodePen and GitHub. The problem is at the top of .js file. https://codepen.io/ovy1448/pen/gZrKOX
https://github.com/ovy1448/MyRun/blob/master/js/main.js 
Union between three arrays
I need to find the union of three arrays that get passed to the function
union
.It took me about 50lines to code to get the expected result. Apparently, the following code works but now I wonder what are the best ways (either in a functional and in a nonfunctional fashion) to do the same job.
function union(...arrays) { var array1 = arguments[0]; var array2 = arguments[1]; var array3 = arguments[2]; var unique = []; var intersaction = []; // find the unique values for(let i = 0; i < array1.length; i++) { if( (array2.includes(array1[i]) == false) && (array3.includes(array1[i])) == false ) { unique.push(array1[i]); } } for(let i = 0; i < array2.length; i++) { if( (array1.includes(array2[i]) == false) && (array3.includes(array2[i])) == false ) { unique.push(array2[i]); } } for(let i = 0; i < array3.length; i++) { if( (array1.includes(array3[i]) == false) && (array2.includes(array3[i])) == false ) { unique.push(array3[i]); } } // find the intersection for(let j = 0; j < array1.length; j++) { if(array2.includes(array1[j])  array3.includes(array1[j]) ) { if (intersaction.indexOf(array1[j]) == 1) { intersaction.push(array1[j]); } } } for(let j = 0; j < array2.length; j++) { if(array1.includes(array2[j])  array3.includes(array2[j]) ) { if (intersaction.indexOf(array2[j]) == 1) { intersaction.push(array2[j]); } } } for(let j = 0; j < array3.length; j++) { if(array1.includes(array3[j])  array2.includes(array3[j]) ) { if (intersaction.indexOf(array3[j]) == 1) { intersaction.push(array3[j]); } } } return union = [...intersaction, ...unique]; } console.log(union([5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5])); // should log: [5, 10, 15, 88, 1, 7, 100]

How do solve delay calculations, namely propagation delay and transmition delay (Clarify the formula)
I have a big exam coming up in January and a small portion of it is on Delay calculations. My tutors are not very helpful when it comes to this so I was wondering if I could get some help on understadning the maths (I do not have a strong mathematical background).
Here is the question w/ answers
I would really appreciate an explanation in layman's terms, I would really appreciate the help.
Thank you for your time.

Calculate Next Pay Day in Swift
Trying to do some math calculations in Swift and getting a bit stuck.
Essentially we get paid every 4th Friday, and I want to do a calculation that based on todays date, works out how many days until our next Pay Day.
I've got a reference day of "28/03/2008"
Hope someone can help, sure this is really simple and I'm over calculating it.

Calculate possible combinations of banknotes in Java
I have this question: how can I calculate all the possible combinations of banknotes in Java given a number of banknotes? The output should be like this:
You can only use bills of 20€, 50€ and 100€.
200€ = 10 * 20€
200€ = 5 * 20€ + 2 * 50€
200€ = 2 * 100€
...
I cannot think about a solution for this. Thanks.

Select items from dataframe based function applied individually to subsets
I would like to select items (rows) from a dataframe that fall within n'th quantile as calculated per subsets. Suppose i have for example observations for 10 different variables. I want to have a subset of observations that consists of picking for each 10 variables those observations that fall within the n'th quantile of the observations of that variable.

Subsetting DataFrames Using a List in Julia
I was wondering if you could subset a dataframe like the one below based on the values of one of the columns (such as ids), you could use the equals operator like in df2 however, if you want to subset based on a list like ids I cannot find an operator to subset the dataframe based on a list as the .in operator does not seem to work with dataframes is there another operator I could use?
df = DataFrame(ids = [1, 1000, 10000, 100000,1,2,3,4], B = [1,2,3,4,123,6,2,7], D = ["N", "M", "I", "J","hi","CE", "M", "S"]) df2= df[df[:pmid] .== 1000, :] ids = [2,3, 10000] df3= df[df[:pmid] .in ids,:]
As of right now df3 gives me a bounds error.
Also I am running this on Julia 0.6.4

data.table and shifts of varying length
I have a
data.table
with times and time intervalslibrary(data.table) dat < data.table(time=seq(from=as.POSIXct("20180101 01:00:00"),to=as.POSIXct("20180101 01:00:10"), by="secs"), int=c(2,3,3,1,1,10,10,10,10,10,10), x=2*1:11) > dat time int x 1: 20180101 01:00:00 2 2 2: 20180101 01:00:01 3 4 3: 20180101 01:00:02 3 6 4: 20180101 01:00:03 1 8 5: 20180101 01:00:04 1 10 6: 20180101 01:00:05 10 12 7: 20180101 01:00:06 10 14 8: 20180101 01:00:07 10 16 9: 20180101 01:00:08 10 18 10: 20180101 01:00:09 10 20 11: 20180101 01:00:10 10 22
as well as some variable
x
. I would like to create a chain where only those rows are shown for which (in loopexpressions)time[i]=time[i1]+int[i1]
for a fixed number of timesN
. In the sample data onlyN=3
is possible and the result would look like> res time int x 1: 20180101 01:00:00 2 2 2: 20180101 01:00:02 3 6 3: 20180101 01:00:05 1 12
I am certain this is much faster in data.table than in a loop, but I cannot get it. I thought about a solution with
shift()
but can`t handle the different intervals.