numbers

How to fill in the preceding numbers whenever there is a 0 in R?

≯℡__Kan透↙ 提交于 2019-12-21 03:37:40
问题 I have a string of numbers: n1 = c(1, 1, 0, 6, 0, 0, 10, 10, 11, 12, 0, 0, 19, 23, 0, 0) I need to replace 0 with the corresponding number right in front of it to get: n2 = c(1, 1, 1, 6, 6, 6, 10, 10, 11, 12, 12, 12, 19, 23, 23, 23) How can I get from n1 to n2? Thanks in advance! 回答1: n2 <- n1[cummax(seq_along(n1) * (n1 != 0))] 回答2: Try na.locf() from the package zoo : library(zoo) n1 <- c(1, 1, 0, 6, 0, 0, 10, 10, 11, 12, 0, 0, 19, 23, 0, 0) n1[n1 == 0] <- NA na.locf(n1) ## [1] 1 1 1 6 6 6

How to get the numbers after the decimal point? (java) [duplicate]

青春壹個敷衍的年華 提交于 2019-12-21 03:15:05
问题 This question already has answers here : How do I get whole and fractional parts from double in JSP/Java? (17 answers) Closed 6 years ago . double d = 4.321562; Is there an easy way to extract the 0.321562 on it's own from d? I tried looking in the math class but no luck. If this can be done without converting to string or casting to anything else, even better. 回答1: Well, you can use: double x = d - Math.floor(d); Note that due to the way that binary floating point works, that won't give you

Mapping Array in Javascript with sequential numbers

限于喜欢 提交于 2019-12-21 02:22:14
问题 The following code: let myArray = Array.apply(null, {length: 10}).map(Number.call, Number); Creates the following Array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] I just don't understand why. I can't find anything on the internet that explains this behavior. Does anyone know why this works the way it does? Perhaps a link to some documentation? 回答1: Array.apply(null, {length: 10}) creates an array of length 10 with all elements being undefined . .map(Number.call, Number) will invoke Number.call for each

Find unique numbers in array

南楼画角 提交于 2019-12-20 14:39:33
问题 Well, I have to find how many different numbers are in an array. For example if array is: 1 9 4 5 8 3 1 3 5 The output should be 6, because 1,9,4,5,8,3 are unique and 1,3,5 are repeating (not unique). So, here is my code so far..... not working properly thought. #include <iostream> using namespace std; int main() { int r = 0, a[50], n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int j = 0; j < n; j++) { for (int k = 0; k < j; k++) { if (a[k] != a[j]) r++; } } cout << r << endl

Random number between negative and positive value [duplicate]

隐身守侯 提交于 2019-12-20 12:28:35
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Generating random numbers in Javascript in a specific range? How can i get a random value between, for example, from -99 to 99, excluding 0? 回答1: var num = Math.floor(Math.random()*99) + 1; // this will get a number between 1 and 99; num *= Math.floor(Math.random()*2) == 1 ? 1 : -1; // this will add minus sign in 50% of cases 回答2: This returns what you want function getNonZeroRandomNumber(){ var random = Math

TCP Sequence Number

谁说我不能喝 提交于 2019-12-20 11:06:36
问题 I'm trying to understand how the sequence numbers of the TCP header are generated. In some places I read that it is the "index of the first byte in the packet" (link here), on some other sites it is a random 32bit generated number that is then incremented. I don't really know which is which, so here are some questions: How is the initial sequence number generated? (Please provide an RFC number if there is one) How is it incremented? How is the secret key generated? I read some of the RFCs

Algorithm for digit summing?

夙愿已清 提交于 2019-12-20 10:22:29
问题 I'm searching for an algorithm for Digit summing. Let me outline the basic principle: Say you have a number: 18268 . 1 + 8 + 2 + 6 + 8 = 25 2 + 5 = 7 And 7 is our final number. It's basically adding each number of the whole number until we get down to a single (also known as a 'core') digit. It's often used by numerologists. I'm searching for an algorithm (doesn't have to be language in-specific) for this. I have searched Google for the last hour with terms such as digit sum algorithm and

Algorithm for digit summing?

女生的网名这么多〃 提交于 2019-12-20 10:20:54
问题 I'm searching for an algorithm for Digit summing. Let me outline the basic principle: Say you have a number: 18268 . 1 + 8 + 2 + 6 + 8 = 25 2 + 5 = 7 And 7 is our final number. It's basically adding each number of the whole number until we get down to a single (also known as a 'core') digit. It's often used by numerologists. I'm searching for an algorithm (doesn't have to be language in-specific) for this. I have searched Google for the last hour with terms such as digit sum algorithm and

JavaScript: Get the second digit from a number?

南楼画角 提交于 2019-12-20 10:17:22
问题 I have a number assigned to a variable, like that: var myVar = 1234; Now I want to get the second digit (2 in this case) from that number without converting it to a string first. Is that possible? 回答1: So you want to get the second digit from the decimal writing of a number. The simplest and most logical solution is to convert it to a string : var digit = (''+myVar)[1]; or var digit = myVar.toString()[1]; If you don't want to do it the easy way, or if you want a more efficient solution, you

Sorting Scientific Number With Unix Sort

纵然是瞬间 提交于 2019-12-20 09:53:31
问题 I tried to sort these number with Unix sort, but it doesn't seem to work: 2e-13 1e-91 2e-13 1e-104 3e-19 9e-99 This is my command: sort -nr file.txt What's the right way to do it? 回答1: Use -g (long form --general-numeric-sort ) instead of -n . The -g option passes the numbers through strtod to obtain their value, and it will recognize this format. I'm not sure if this is available on all implementations of sort , or just the GNU one. 回答2: if your sort doesn't have -g , another way. $ printf "