flobrr
flobrr

Reputation: 307

R concate 2 numeric columns with big numbers

I want to concate 2 columns with numbers and get as result a number. Example: First column: 123456 Second column: 78910 Desired Result: 12345678910

test<-matrix(
  c(328897771052600448,4124523780886268),
  nrow=1,
  ncol=2
)

test<-data.frame(test)

str(test)

Both columns are numeric

colnames(test)<-c("post_visid_high","post_visid_low")

test_2<-transform(test,visit_id=as.numeric(paste0(post_visid_high,post_visid_low)))

Problem: My concated result gives: 3.288977710526004289528e+33 I dont understand why I get this (incorrect??) number.

When I exlcude "as.numeric" I get the right result:

    test_2<-transform(test,visit_id=paste0(post_visid_high,post_visid_low))
test_2

But its converted into "factor":

str(test_2)

Upvotes: 0

Views: 40

Answers (1)

Ralf Stubner
Ralf Stubner

Reputation: 26823

These numbers are to large to be stored exactly as numeric. You can either store them as string by specifying stringsAsFactors = FALSE:

test_2<-transform(test,visit_id=paste0(post_visid_high,post_visid_low), stringsAsFactors = FALSE)
test_2
#>   post_visid_high post_visid_low                           visit_id
#> 1    3.288978e+17   4.124524e+15 3288977710526004484124523780886268
str(test_2)
#> 'data.frame':    1 obs. of  3 variables:
#>  $ post_visid_high: num 3.29e+17
#>  $ post_visid_low : num 4.12e+15
#>  $ visit_id       : chr "3288977710526004484124523780886268"

Or you use something like gmp to process arbitrary sized integers:

library(gmp)
test_3 <- test
test_3$visit_id <- as.bigz(paste0(test_3$post_visid_high, test_3$post_visid_low))
test_3
#>   post_visid_high post_visid_low                           visit_id
#> 1    3.288978e+17   4.124524e+15 3288977710526004484124523780886268
str(test_3)
#> 'data.frame':    1 obs. of  3 variables:
#>  $ post_visid_high: num 3.29e+17
#>  $ post_visid_low : num 4.12e+15
#>  $ visit_id       : 'bigz' raw 3288977710526004484124523780886268

Upvotes: 3

Related Questions