work with large numbers and decimals using R [duplicate] - r

This question already has answers here:
How to read data when some numbers contain commas as thousand separator?
(11 answers)
Closed 2 years ago.
I am currently importing a csv file that contains huge positive numbers separated by decimals in a column
tb <- read.csv("data.csv",dec = ";")
4,013,054,922
5,208,913,410
5,514,995,512
5,148,498,611
...
this data in R recognizes it as a character type and I cannot do operations
I have tried with
as.numeric(tb$large)
as.long(tb$large)
as.complex(tb$large)
but it returns rows with NA
and also try the gmp library too, to no avail
I appreciate your help

Is this what you are looking for?
d <- c('4,013,054,922','5,208,913,410',
'5,514,995,512',
'5,148,498,611')
class(d)
#> [1] "character"
library(stringr)
as.numeric(str_remove_all(d, ','))
#> [1] 4013054922 5208913410 5514995512 5148498611
Created on 2020-06-14 by the reprex package (v0.3.0)

Related

convert character to numeric values (with 2 types of numeric values) using r [duplicate]

This question already has answers here:
How to read data when some numbers contain commas as thousand separator?
(11 answers)
Closed 2 years ago.
I have a variable that should be numeric but is a character, this variable has two types of numeric values, when I convert them to numeric one is not recognized as a number:
num <-c("3,98E+03", "3,98E+03","0.003382932", "5,22E+02", "0.005464587")
as.numeric(num)
NAs introduced by coercion[1] NA NA 0.003382932 NA 0.005464587
I don't want to have NA introduced.
Thank you!
You can replace the , with . using sub:
as.numeric(sub(",", ".", num, fixed = TRUE))
#[1] 3.980000e+03 3.980000e+03 3.382932e-03 5.220000e+02 5.464587e-03
The readr package has helpful functions to parse numbers from a string which may be more generalisable. string_replace() also replaces the , with a . similar to answer by #GKi
library(stringr)
library(readr)
parse_number(str_replace(num, ",", "."))

How do I get the number of numbers in a text in R? [duplicate]

This question already has answers here:
count number of digits in a string in r
(2 answers)
Closed 3 years ago.
I want to know how many digits do I have in a text variable. For example, a function that in the text "ABC234" the answer would be 3.
I tried with this:
aa=gregexpr("[[:digit:]]+\\.*[[:digit:]]*","ABC234")
I almost have it, but honestly I still dont understand the lists, so I have no idea how to get it.
Any function? Or how to manage it with my almost-option?
Thanks
Match each digit and then take the length of the returned value:
lengths(gregexpr("\\d", "ABC234"))
## [1] 3
or replace each non-digit with a zero length string and take the length of what remains:
nchar(gsub("\\D", "", "ABC234"))
## [1] 3
As an option you can use stringi or stringr libraries as well:
stringi::stri_count('ABC234', regex = '\\d')
# [1] 3
stringr::str_count('ABC234', '\\d')
# [1] 3
You can use the dpylr and readr package as follows:
library(readr)
library(dplyr)
string = "ABC234"
parse_number(string) %>%
nchar()
[1] 3

Converting abbreviated "numbers" into numbers in R [duplicate]

This question already has answers here:
Converting 1M to 1000000 elegantly
(3 answers)
Closed 6 years ago.
I have a data frame in R that has monetary values such as $25,000 and $2,000,000 entered as 25K and 2M respectively. The data frame is massive, so is there any way I can, for example, change all of the 2M's to 2000000's?
Try gsub() on the letters:
df$variableName <- gsub("M", "000000", df$variableName)
df$variableName <- gsub("K", "000", df$variableName)
and so forth...
Maybe convert the class when you're done class(df$variable) <- "numeric".

as.numeric changes the last digits of large integers [duplicate]

This question already has answers here:
Why are these numbers not equal?
(6 answers)
Closed 5 years ago.
When I to convert large numbers formatted as character strings to numeric R changes the last digits. This also happens when I pass it the number itself.
For example:
> options(scipen = 999)
> as.numeric("3411190080123000215")
[1] 3411190080123000320
> as.numeric(3411190080123000215)
[1] 3411190080123000320
This also happens when I use other numeric functions:
> floor(3411190080123000215)
[1] 3411190080123000320
Could this be an issue with my settings?
Thank you!!
The issue is that you are not actually using integers, you are using floats.
x <- as.numeric("3411190080123000215")
is.integer(x)
However, your number is too large to be stored as an integer anyway. Check out the gmp R package for arbitrarily large integers.

Convert character input to vector of integers [r] [duplicate]

This question already has answers here:
Evaluate expression given as a string
(8 answers)
Closed 7 years ago.
How can I convert a character string such as "c(1:10)" into an actual vector of integers? My issue is that I need to read a bunch of files using read.xlsx from the package xlsx, but I need to read different rows for each file. I've got a separate file that I can read into the workspace as a data.frame (call it "MyFile") that lists in one column the names of the files and, in another column, which rows to read, but I don't know how to convert the character string into the numbers I need. I'd use regex to extract it, but that sounds like more work than I want since it's a mishmash of everything from "c(1:10)" to "c(2, 4, 6:8, 21)".
I've tried:
Files <- list()
for (i in 1:nrow(MyFiles)){
Files[[i]] <- read.xlsx(MyFiles$File[i],
sheetName = MyFiles$Tab[i],
rowIndex = MyFiles$Row[i])
}
but R doesn't know what to do with the row specification since it's currently reading it as a character string.
It's a shame there isn't a better way (as far as I know) than eval(parse(text= but that's what I use in these situations.
input <- "c(1:10)"
output <- eval(parse(text=input))
output
# [1] 1 2 3 4 5 6 7 8 9 10

Resources