sum with n decimal places R [duplicate] - r

This question already has answers here:
Controlling number of decimal digits in print output in R
(4 answers)
Closed 9 years ago.
I am trying to have sum of two values in R with 6 decimal places but it only returns with 5.
85.85+0.01302778
# [1] 85.86303
I tried
round(85.85+0.01302778,6)
but it does not work.
Actually sum(85.85,0.01302778) gives only 5 decimals and I did not find any scope of decimal places in ?sum.
Any suggestions

Try this to get 6 digits:
> options(digits=8)
> 85.85+0.01302778
[1] 85.863028

Related

What is the R function and code for summing a list of characters in R? [duplicate]

This question already has answers here:
Count number of distinct values in a vector
(6 answers)
Closed 1 year ago.
I am stuck on a question in R, i need to sum a list of characters from the starwars dataset, which is the starwars eye color. The question asks how many different eye colours the character have. The answer is 15, which i derived from a table (table(starwars$eyecolor), but i cannot figure the code to get to 15.
Try any of below
> length(levels(factor(starwars$eye_color)))
[1] 15
> length(unique(starwars$eye_color))
[1] 15
> sum(!duplicated(starwars$eye_color))
[1] 15
length(unique(starwars$eye_color))

Need to extract only those digits which are having 5 to 6 digit length [duplicate]

This question already has answers here:
How to use grep()/gsub() to find exact match
(2 answers)
Closed 4 years ago.
I need to extract only 5 or 6 digit from a string. For example "hi 23456678 is number, also there is a number 92844 and 741653 "
I need to extract only the 5 or 6 digit number from string , i tried \d{5,6} but it is giving me result as (23456, 92844, 741653) but my desired outcome should be only 92844 & 741653 , how can i get that.
I am using R, please suggest.
You can try this
^[0-9]{5,6}$
{5,6} = between 5 and 6 characters

R - substring by number [duplicate]

This question already has answers here:
Extracting numbers from vectors of strings
(12 answers)
Closed 3 years ago.
what is the most easiest way how to get number from string? I have huge list of links like this, I need to get that number 98548 from it.
https://address.com/admin/customers/98548/contacts
Note that number cant have different count of numbers and can start from 0 to 9
This is the most easiest that I know :
str <- "https://address.com/admin/customers/98548/contacts"
str_extract_all(str, "\\d+")[[1]]
Using stringr:
no="https://address.com/admin/customers/98548/contacts"
unlist(stringr::str_extract_all(no,"\\d{1,}"))
[1] "98548"

Cutting value in vector by determine positions [duplicate]

This question already has answers here:
Trying to return a specified number of characters from a gene sequence in R
(3 answers)
Extracting the last n characters from a string in R
(15 answers)
Closed 5 years ago.
Is there a function in R that I can cut a value in vector.
for example i got this vec:
40754831597
64278107602
64212163451
and each vale in the vec i want to cut so from the number pos 3 to 6 for example and get a new vector look like this
7548
2781
2121
and so on
I don't really get why you would like to do this, but here you go:
# assuming it's a character vector
substring(vec,3,6)
# if it's numeric
substring(as.character(vec),3,6)
#output
#[1] "7548" "2781" "2121"
We can use sub
sub(".{2}(.{4}).*", "\\1", v1)
#[1] "7548" "2781" "2121"
data
v1 <- c(40754831597, 64278107602, 64212163451)

change numbers into the sum of their figures [duplicate]

This question already has answers here:
Digit sum function in R
(4 answers)
Closed 7 years ago.
I've got this simple question: how can I change a vector consisting of 10 numbers into a vector consisting of ten numbers which are the sum of the figures of the first numbers? So 11 in the first vector becomes 2, 234 becomes 9.
We can use str_extract_all from stringr to get the individual numbers, convert them to numeric and get the sum.
library(stringr)
sapply(str_extract_all(c(11, 234), '\\d'), function(x) sum(as.numeric(x)))

Resources