Recently was depressed by the round function in R, some say that the rules of the round function are so defined:
Refer to Cos Moderator Xiaonan: This rule has a vulgar called "four homes six into five together", also called "Banker rounding", the full version is as follows: 1. If the number being repaired is equal to or less than 4 o'clock, the number is shed;
2. The number of the repaired is equal to or greater than 6 o'clock;
3. The number of the repair is equal to 5 o'clock, to see the number in front of 5, if the odd is rounded, if the even number will be 5 off, that is, after the end of the revision of the figure is an even number, if there is no "0" in the back of any numbers, then whether the front of the 5 is an odd or even,
For example, use the above rules to retain 3 valid digits for the following data:
9.8249=9.82, 9.82671=9.83
9.8350=9.84, 9.8351 =9.84
9.8250=9.82, 9.82501=9.83
It seems to illustrate the problem, but:
Round (9.9995,3) The result is 9.999, 9 is an odd exception.
Rounding up the decimals that you have implemented in R (mainly to follow the rules in mathematics: Rounding the absolute value of the number plus the sign)
There are a number of improvements to the code that are currently only available to meet the needs of the individual (rounding after holding n bits after a number of decimal digits):
Myround <-function (x, keep=4) {x <-as.character (x) if (Grepl ("[.]", X)) {xSplit <-unlist (strsplit (x, "[
.]")) XSPLITBEF <-xsplit[1] Minusyn <-substring (XSPLITBEF, 1, 1) # Minusyn <-Minusyn = = "-" Xsplitaf T <-xsplit[2] xsplitaft <-substring (xsplitaft, 1:nchar (Xsplitaft), 1:nchar (Xsplitaft)) if (Minusyn = = "-") {XSPLITBEF <-substring (xsplitbef, 2:nchar (XSPLITBEF), 2:nchar (XSPLITBEF)) lenbef <-Length (XSPLITB EF) Lenaft <-Length (Xsplitaft) if (Lenaft > Keep) {all <-C (XSPLITBEF, xsplitaft[1: (ke EP + 1)] if (As.integer (tail (all,1)) < 5) {res1 <-C (XSPLITBEF, Xsplitaft[1:keep]) res1
<-paste (c (RES1[1:LENBEF], ".", Res1[(lenbef+1): Length (res1)]), collapse= "") res1 <--as.numeric (res1) }else{res1 <-C (XSPLITBEF, Xsplitaft[1:keep]) res1 <-As.integer (res1) for (I in Length (res1): 1{if (i = = Length (res1)) {Res1[i] <-Res1[i] + 1} if (Res1[i]! = 10) {
Break}else{Res1[i-1] <-res1[i-1] + 1}} if (res1[1] = =) {Res1[-1] <-0 res1 <-C (1,0,res1[-1]) res1 <-
Paste (C (res1[1: (lenbef+1)], ".", Res1[(lenbef+2): Length (res1)]), collapse= "") res1 <--as.numeric (res1) }else{Res1[which (res1 = = Ten)] <-0 res1 <-paste (C (RES1[1:LENBEF], ".", res1[(Lenbe f+1): Length (res1)]), collapse= "") res1 <--as.numeric (res1)}}}else{Res 1 <-as.numeric (x)}}else{xsplitbef <-substring (xsplitbef, 1:nchar (XSPLITBEF), 1:nchar (XSPLITBEF) ) lenbef <-Length (xsplitbef) lenaft <-Length (Xsplitaft) if (Lenaft > Keep) {All <-C (XSPLITBEF, XSPLItaft[1: (keep + 1)]) if (As.integer (tail (all,1)) < 5) {res1 <-C (XSPLITBEF, Xsplitaft[1:keep]) Res1 <-Paste (C (RES1[1:LENBEF], ".", Res1[(lenbef+1): Length (res1)]), collapse= "") res1 <-as.numeric (RES1)} else{res1 <-C (XSPLITBEF, Xsplitaft[1:keep]) res1 <-As.integer (res1) for (i in length (RES1): 1) {if (i = = Length (res1)) {Res1[i] <-Res1[i] + 1} if (Res1[i]
! = Ten) {break}else{res1[i-1] <-res1[i-1] + 1}} if (res1[1] = =) {Res1[-1] <-0 res1 <-C (1,0,res1[-1]) re S1 <-paste (C (res1[1: (lenbef+1)], ".", Res1[(lenbef+2): Length (res1)]), collapse= "") res1 <-As.numeric (r ES1)}else{Res1[which (res1 = = Ten)] <-0 res1 <-paste (C (RES1[1:LENBEF], ".", Res1 [(LENBEF+1): Length (res1)]), collapse= "") res1 <-as.numeric (res1)}}}else{Res1
<-as.numeric (x)}}}else{res1 <-as.numeric (x)} return (RES1)} myround ( -0.374832,3)
This function is not suitable for vector calculations, x must be a single value
You can use multiple values:
(X1 <-seq (-.2,. 4, by =.))
Sapply (x1, Myround, Keep=1)