This question already has answers here:
Round up from .5
(7 answers)
Closed 3 years ago.
I just got to know that R or Python have different ways of defining the round function compared to Excel.
Statistical argument aside, my business users are primarily Excel users. And this has led to some confusion as the numbers generated by R/Python scripts can differ due to this rounding convention.
It seems to me that Excel always 'round up' when it needs to calculate numbers like 1.5, 2.5, 3.5, etc while R/Python will round to the nearest even number instead.
Potentially there're other assumptions I'm not aware of which differentiate both Excel and R/Python.
Is there any way to implement the exact round function from Excel in R?
In Excel
In R
Based on the observed behaviour that round in Excel rounds away from zero for numbers ending in 0.5, it could be replicated in R with:
> x <- c(-1.5, -0.5, 0.5, 1.5, 2.5, 3.5)
> round(x+2*sign(x)*.Machine$double.eps)
# [1] -2 -1 1 2 3 4
Related
This question already has answers here:
Sample from vector of varying length (including 1)
(4 answers)
Closed 1 year ago.
I do not understand how the sample function is working if I have a vector as argument with only one element to sample from (and I just want to sample one element).
I am doing a simulation and me input vector x is going to be of different size every time my loop runs one step. But when the vector is containing only one element, for example x = c(3), it should be sample 3, but I doesn't it starts to sample 2 and 1 all kind of numbers. I have tried to use set.seed and changed the replace argument, but something is wrong. Does anyone know what's going on here?
From ?sample:
If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x. Note that this convenience feature may lead to undesired behaviour when x is of varying length in calls such as sample(x).
So this behavior is expected if undesirable. A possible solution is in the answer linked by user20650.
This question already has answers here:
Sample from vector of varying length (including 1)
(4 answers)
Closed 6 months ago.
I appreciate that sampling from a list of length 1 has little practical use yet all the same I tried the following:
When I run the r snippet sample(c(1,2,3),1) then I obtain a random single value from the list c(1,2,3).
When I run the r snippet sample(c(3),1) then I would expect the number 3 to always be output but I don't, I seem to obtain the same behaviour as above.
Why is this? How can I sample from a list of length 1?
I found that sample(c(3,3),1) does indeed output the intended, but feels not what I had in mind.
See documentation for sample:
If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x.
You can use resample() from the gdata package. This saves you having to redefine resample in each new script. Just call
gdata::resample(c(3), 1)
https://www.rdocumentation.org/packages/gdata/versions/2.18.0/topics/resample
This question already has answers here:
Missing last sequence in seq() in R
(6 answers)
Closed 4 years ago.
How do you generate a sequence of numbers and also includes the last number in it?
Check this out to understand what I am saying:
I want to generate the sequence of numbers that run from 0 to 1 with an interval of 0.3.
The formal I know runs like:
`seq(0, 1, by=0.3)`
The answer to this will be
`0,0.3, 0.6, 0.9`
However, I want a situation where either of the following is possible:
`0,0.3, 0.6, 0.9, 1` OR `0, 0.3, 0.6,1`
So pretty much in both instances the first and last number are included in the sequence. Is such possible in r?
Thanks.
You can create your customized function:
seq(0, 1, by=0.3)
lastnum_seq<-function(w,x,y){
y<-seq(w,x,by=y)
return(c(y,x))
}
lastnum_seq(0,1,0.3)
This question already has answers here:
Determining minimum values in a vector in R
(4 answers)
Closed 6 years ago.
So my task is to "extract and display the soil types of the two lowest altitude plots using a single line of R code", and I'm not sure of the best way of doing this.
At the moment my command looks like this:
conservation$soil[conservation$alt < 8]
but that's because I know the two lowest altitude plots are below 8, so I'm kind of cheating.
Surely there's a simple way to just define the two lowest entries here instead?
You can order the lines of conservation$alt with order(conservation$alt) in a descending fashion. Or order(-conservation$alt) for the lines in ascending order.
tail(df$a, 2) will return the last two entries of df$a. Alternatively, head(df$a, 2) will give the first two entries.
Combining these functions will give you:
conservation$soil[tail(order(-conservation$alt), 2)]
or
conservation$soil[head(order(conservation$alt), 2)]
This question already has answers here:
How to remove leading "0." in a numeric R variable
(5 answers)
Closed 9 years ago.
Writing an article with R + Sweave, I wish to hide the zero before decimal point for correlation results. For example, for a score of 0.85, I wish to show it as .85. Or if the score is negative, e.g. -0.85, it should be shown as -.85.
Right now I am doing it in a very ugly way, using substr(0.85, 2, 4) for positive values and paste("-", substr(-0.85, 3, 5), sep="") for negative values. I did some homework, checking functions such as format() and formatC in the base package, but none of them seemed to meet my needs.
So I wonder whether there is an easier way to hide the zero before decimal point in R? Any help is appreciated.
I'd use a regular expression:
sub('^(-)?0[.]', '\\1.', c(0.85, -0.85))
## [1] ".85" "-.85"