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))
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:
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')
This question already has answers here:
Extract the first (or last) n characters of a string
(5 answers)
Closed 2 years ago.
Well, I got a variable that shows numbers like this one: 13015064000992062, and I need to extract the four first numbers and put it in another column. In this case, it will be 1301.
A friend told me that it was using stringr library, but he can't remember the code.
I am working with a data frame. So I need to get a new variable called "canton", that are the four first digits from 'Identificador'
So it looks like this:
We can use substr
permMan19$newcol <- substr(permMan19$identificador, 1, 4)
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 an answer here:
R floating point number precision being lost on coversion from character
(1 answer)
Closed 7 years ago.
How do I retain the full 16 digit precision when coercing a text to numeric in R?
My attempt below does not appear to do this...
x<-"0.501288104715059"
x<-as.double(x)
x
[1] 0.5012881
[Note this is similar to a previously asked question using as.numeric to convert a character to number but his question refers to the case of using as.double to convert a character to a number]
The code in fact does work - I just needed to set the number of digits to be displayed
x<-"0.501288104715059"
x<-as.double(x)
options(digits=16)
x
[1] 0.501288104715059
Might be useful to somebody else