min

With min() in R return NA instead of Inf

你。 提交于 2020-08-27 20:56:05
问题 Please consider the following: I recently 'discovered' the awesome plyr and dplyr packages and use those for analysing patient data that is available to me in a data frame. Such a data frame could look like this: df <- data.frame(id = c(1, 1, 1, 2, 2), # patient ID diag = c(rep("dia1", 3), rep("dia2", 2)), # diagnosis age = c(7.8, NA, 7.9, NA, NA)) # patient age I would like to summarise the minimum patient age of all patients with a median and mean. I did the following: min.age <- df %>%

Constrain lower limit of the result of a subtraction

吃可爱长大的小学妹 提交于 2020-08-08 06:59:04
问题 I want to subtract the values in a vector from a scalar. However, if the result is lower than zero I want to set the result to zero. I have tried using max , but it doesn't give me the expected result s # [1] 750.0 975.0 1125.0 1237.5 1312.5 1400.0 max(1050 - s, 0) # [1] 300 I expect result to be c(300, 150, 0, 0, 0, 0) 回答1: I suggest pmax : pmax(1050 - s, 0) # [1] 300 75 0 0 0 0 来源: https://stackoverflow.com/questions/3438049/constrain-lower-limit-of-the-result-of-a-subtraction

Find minimum and maximum values of a function

岁酱吖の 提交于 2020-07-02 09:43:47
问题 I have a function and I would like to find its maximum and minimum values. My function is this: def function(x, y): exp = (math.pow(x, 2) + math.pow(y, 2)) * -1 return math.exp(exp) * math.cos(x * y) * math.sin(x * y) I have an interval for x [-1, 1] and y [-1, 1]. I would like to find a way, limited to this interval, to discover the max and min values of this function. 回答1: Using, for instance, scipy 's fmin (which contains an implementation of the Nelder-Mead algorithm), you can try this:

Printing Min1 and Min2 using Python

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-30 08:05:20
问题 What am I missing in this code here so that it sets min1 and min2 to the two smallest numbers? def test() : # do not change this line! list = [4, 5, 1, 9, -2, 0, 3, -5] # do not change this line! min1 = list[0] min2 = list[1] #missing code here print(min1, min2) return (min1, min2) # do not change this line! # do not write any code below here test() # do not change this line! # do not remove this line! 回答1: List does not sort elements in it implicitly. You have sort the list with sort()

Java Streams .max() and .min() lag in performance?

拈花ヽ惹草 提交于 2020-05-25 08:47:14
问题 Consider Below 2 examples. 1 With Streams myList.stream().map(this::getInt).max(Integer::compareTo); 2 Old way int max = Integer.MIN_VALUE; for (MyItem item : myList) { max = Math.max(max, getInt(item)); } Above getInt method accepts a MyItem argument and returns an int result. Here, #2 gives me a much lower latency compared to #1. Does anyone have an idea why or anything going wrong for me? 回答1: myList.stream().mapToInt(this::getInt).max() Try mapping to an IntStream. An IntStream works with

Java Streams .max() and .min() lag in performance?

蓝咒 提交于 2020-05-25 08:46:19
问题 Consider Below 2 examples. 1 With Streams myList.stream().map(this::getInt).max(Integer::compareTo); 2 Old way int max = Integer.MIN_VALUE; for (MyItem item : myList) { max = Math.max(max, getInt(item)); } Above getInt method accepts a MyItem argument and returns an int result. Here, #2 gives me a much lower latency compared to #1. Does anyone have an idea why or anything going wrong for me? 回答1: myList.stream().mapToInt(this::getInt).max() Try mapping to an IntStream. An IntStream works with

How do I sort the possibilities vector in 0/1 knapsack problem using the template min_max function I have written?

大城市里の小女人 提交于 2020-04-16 05:47:23
问题 SO already has multiple questions related to this topic. My question ain't 'bout the algorithm. It's 'bout sorting the final possibilities vector as per profit I wrote the following piece of code to solve the 0/1 knapsack problem: #include "algorithms.h" struct Item { int index = 1; int profit = 1; int weight = 1; Item() = delete; explicit Item(int i, int _profit, int w) { index = i; profit = _profit; weight = w; } bool operator<(const Item& item) { return this->profit < item.profit; } bool

How do I return a list of the 3 lowest values in another list

 ̄綄美尐妖づ 提交于 2020-04-06 09:10:29
问题 How do I return a list of the 3 lowest values in another list. For example I want to get the 3 lowest values of this list: in_list = [1, 2, 3, 4, 5, 6] input: function(in_list, 3) output: [1, 2, 3] 回答1: You can use heapq.nsmallest: >>> from heapq import nsmallest >>> in_list = [1, 2, 3, 4, 5, 6] >>> nsmallest(3, in_list) [1, 2, 3] >>> 回答2: If you could sort,you can get frst 3 elements as below: alist=[6, 4, 3, 2, 5, 1] sorted(alist)[:3] Output: [1,2,3] 回答3: even simpler without modules

How do I return a list of the 3 lowest values in another list

寵の児 提交于 2020-04-06 09:09:12
问题 How do I return a list of the 3 lowest values in another list. For example I want to get the 3 lowest values of this list: in_list = [1, 2, 3, 4, 5, 6] input: function(in_list, 3) output: [1, 2, 3] 回答1: You can use heapq.nsmallest: >>> from heapq import nsmallest >>> in_list = [1, 2, 3, 4, 5, 6] >>> nsmallest(3, in_list) [1, 2, 3] >>> 回答2: If you could sort,you can get frst 3 elements as below: alist=[6, 4, 3, 2, 5, 1] sorted(alist)[:3] Output: [1,2,3] 回答3: even simpler without modules