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"
Related
This question already has an answer here:
Extract digit from numeric in r
(1 answer)
Closed 1 year ago.
For example, if I had the number 7.12935239484 and wanted just the 10th decimal place digit (in this example the answer would be 8), how would I go about displaying that using R?
Multiple by 1e10, convert to an integer, and then perform mod 10 to retrieve the number.
floor(7.12935239484* 1e10) %%10
The easiest way is probably by string manipulation.
Use format() with enough digits to make sure that you include the digits you want.
I have written the digit position as 10+2 to emphasize that you are skipping over the first two digits (7.) and taking the 10th digit after the decimal point.
x <- 7.12935239484
substr(format(x,digits=20), start = 10+2, stop = 10+2)
It might be more principled (and robust) to use numerical manipulation
floor((x*1e10) %% 10)
This shifts the decimal point 10 places and then calculates the reminder modulo 10 (the parentheses around x*1e10 are needed to get the right order of operations). This would still work if there were more digits to the left of the decimal point (unlike the string-based solution).
Extract digit from numeric in r is almost a duplicate ...
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
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:
How to format a number as percentage in R?
(10 answers)
Represent numeric value with typical dollar amount format
(4 answers)
Closed 5 years ago.
I want to know if it's possible to display values on a table as percentages but still be able to do arithmetic operations with them.
I have tried using percent() from scales package, but it seems like it transforms the values from numeric to character. I have tried to convert them back using as.numeric(), but it will not work either.
Any solutions? Thank you.
One very hacky way to do this would be as follows
x <- runif(10)
class(x) <- "percent"
print.percent <- function(x) print(scales::percent(as.numeric(x)))
This is probably useful for quick analyses or short scripts but I wouldn't put this into any kind of package or shared code.
This question already has answers here:
How can I disable scientific notation?
(4 answers)
Closed 6 years ago.
My plot is showing values on the y-axis in the form of e notation. Which command should I use to get the values in the numeric form. The values in the file used are in the numeric form?
Thanks
To set the use of scientific notation in your entire R session, you can use the scipen option. From the documentation (?options):
‘scipen’: integer. A penalty to be applied when deciding to print
numeric values in fixed or exponential notation. Positive
values bias towards fixed and negative towards scientific
notation: fixed notation will be preferred unless it is more
than ‘scipen’ digits wider.
So in essence this value determines how likely it is that scientific notation will be triggered. So to prevent scientific notation, simply use a large positive value like 999:
options(scipen=999)
Try format function:
> xx = 100000000000
> xx
[1] 1e+11
> format(xx, scientific=F)
[1] "100000000000"