Finding specific decimal point digit [duplicate] - r

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 ...

Related

Customized Replacing in R Studio [duplicate]

This question already has answers here:
Converting unit abbreviations to numbers
(4 answers)
Closed 1 year ago.
I have a homework to analyze data of Bloomberg Billionaires Index in R Studio, but I am facing a problem with the periods.. There are three forms of values:
185B (No periods)
18.5B (one digit after the period)
1.85B (two digits after the period)
I want to delete the dots and add nine zeros in place of the billion symbol (B) but that means the three values will be the same.
Is there a way to add:
Nine zeros for the first formula (where there are no points)
Eight zeros for the second formula (where there is one digit after
the period)
Seven zeros for the third formula (where there are two digits after
the period)
Thank you!!
x <- c('185B', '18.5B', '1.85B')
as.numeric(sub('B', '', x, fixed = TRUE)) * 10^9
If use of packages are allowed you can use readr::parse_number to get the number directly.
readr::parse_number(x) * 10^9
We can use
library(stringr)
as.numeric(str_remove(x, 'B')) * 10 ^9
#[1] 1.85e+11 1.85e+10 1.85e+09
data
x <- c('185B', '18.5B', '1.85B')

How to convert char to int in R retaining leading zeroes

I am converting a column that has characters such as 000024, 000120 etc to integers.
My code is as below
df$colname <- as.integer(df$colname)
But this removes the leading zeroes and I see result as 24, 120. Is there any way I can prevent it?
Integers don't have a fixed number of leading zeros (or I guess you could say they have infinitely many leading zeros) so computers don't track those if the values as numeric. It's only when displaying them, or turning them into a string that you add a certain number of zeros. When you need them to be pretty, you can add zeros with functions like sprintf("%06d", c(12, 120)) but those return strings in the end (and they assume all values will use the same number of digits).

How to create a sequence of text IDs in r? [duplicate]

This question already has answers here:
How to add leading zeros?
(8 answers)
Closed 6 years ago.
My problem is to create a sequence of IDs in a vector. The vector will contain 001 to 020 then 030 to 100.
I can generate numbers by
x <- c(1:20,30:100)
but this is not in the format I am interested.
x <- c(paste("00", 1:9, sep=""),paste("0", 10:99, sep=""),100)
As suggested by the Frank... Use sprinf for formatted output. I like the %f formatter to format numbers. It is designed to format floating point numbers. %f will be replaced by the number. You can add 0 in front of the f to get leading numbers. Or you can also define how many digits you want to have overall (in your case 3) and how many should be decimal (0 after the .). Play a little with it. It is great for formatted output, filename etc.
sprintf('%03.0f', c(1:20,30:100))

Change decimal digits for data frame column in R

Questions about displaying of certain numbers of digits have been posted, however, just for single values or vectors, so I hope someone can help me with this.
I have a data frame with several columns and want to display all values in one column with two decimal digits (this column only). I have tried round() and format() and options(digits) but none worked on a column (numerical). I wonder if there is a method to do this without going the extra way of converting the column to a vector and gluing all together again.
Thanks a lot!
Here's an example of how to do this with the cars data.frame that comes installed with R.
First I'll add some variability so that we have numbers with decimal places:
data=cars+runif(nrow(cars))
Then to round just a single column (in this case the dist column to 2 decimal places):
data[,'dist']=round(data[,'dist'],2)
If your data contain whole numbers then you can guarantee that all values will have 2 decimal places by using:
cars[,'dist']=format(round(cars[,'dist'],2),nsmall=2)

Hide zero before decimal point [duplicate]

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"

Resources