0

I would like for R to read in the first 10,000 digits of Pi and group every 10 digits together

e.g., I want R to read in a sequence

pi <- 3.14159265358979323846264338327950288419716939937510582097...

and would like R to give me a table where each row contains 10 digit:

3141592653

5897932384

6264338327 ...

I am new to R and really don't know where to start so any help would be much appreciated!

Thank you in advance

  • Does this answer your question? https://stackoverflow.com/questions/11619616/how-to-split-a-string-into-substrings-of-a-given-length – Ali Jul 23 '20 at 19:01

3 Answers3

1

Here's one way to do it. It's fully reproducible, so just cut and paste it into your R console. The vector result is the first 10,000 digits of pi, split into 1000 strings of 10 digits.

For this many digits, I have used an online source for the precalculated value of pi. This is read in using readChar and the decimal point is stripped out with gsub. The resulting string is split into individual characters and put in a 1000 * 10 matrix (filled row-wise). The rows are then pasted into strings, giving the result. I have displayed only the first 100 entries of result for clarity of presentation.

pi_url  <- "https://www.pi2e.ch/blog/wp-content/uploads/2017/03/pi_dec_1m.txt"
pi_char <- gsub("\\.", "", readChar(url, 1e4 + 1))
pi_mat  <- matrix(strsplit(pi_char, "")[[1]], byrow = TRUE, ncol = 10)
result  <- apply(pi_mat, 1, paste0, collapse = "")

head(result, 100)
#>   [1] "3141592653" "5897932384" "6264338327" "9502884197" "1693993751"
#>   [6] "0582097494" "4592307816" "4062862089" "9862803482" "5342117067"
#>  [11] "9821480865" "1328230664" "7093844609" "5505822317" "2535940812"
#>  [16] "8481117450" "2841027019" "3852110555" "9644622948" "9549303819"
#>  [21] "6442881097" "5665933446" "1284756482" "3378678316" "5271201909"
#>  [26] "1456485669" "2346034861" "0454326648" "2133936072" "6024914127"
#>  [31] "3724587006" "6063155881" "7488152092" "0962829254" "0917153643"
#>  [36] "6789259036" "0011330530" "5488204665" "2138414695" "1941511609"
#>  [41] "4330572703" "6575959195" "3092186117" "3819326117" "9310511854"
#>  [46] "8074462379" "9627495673" "5188575272" "4891227938" "1830119491"
#>  [51] "2983367336" "2440656643" "0860213949" "4639522473" "7190702179"
#>  [56] "8609437027" "7053921717" "6293176752" "3846748184" "6766940513"
#>  [61] "2000568127" "1452635608" "2778577134" "2757789609" "1736371787"
#>  [66] "2146844090" "1224953430" "1465495853" "7105079227" "9689258923"
#>  [71] "5420199561" "1212902196" "0864034418" "1598136297" "7477130996"
#>  [76] "0518707211" "3499999983" "7297804995" "1059731732" "8160963185"
#>  [81] "9502445945" "5346908302" "6425223082" "5334468503" "5261931188"
#>  [86] "1710100031" "3783875288" "6587533208" "3814206171" "7766914730"
#>  [91] "3598253490" "4287554687" "3115956286" "3882353787" "5937519577"
#>  [96] "8185778053" "2171226806" "6130019278" "7661119590" "9216420198"

Created on 2020-07-23 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
1

https://rextester.com/OQRM27791

p <- strsplit("314159265358979323846264338327950288419716939937510582097", "")
digits <- p[[1]]

split(digits, ceiling((1:length(digits)) / 10));
  • 1
    Nice answer. I think you're missing the final stage of pasting the digits back together into length-10 strings and unlisting the result, but that should be trivial. – Allan Cameron Jul 23 '20 at 19:32
1

We can use str_extract:

pi  <- readLines("https://www.pi2e.ch/blog/wp-content/uploads/2017/03/pi_dec_1m.txt")

library(stringr)
t <- unlist(str_extract_all(sub("\\.","", pi), "\\d{10}"))
t[1:100]
  [1] "3141592653" "5897932384" "6264338327" "9502884197" "1693993751" "0582097494" "4592307816" "4062862089"
  [9] "9862803482" "5342117067" "9821480865" "1328230664" "7093844609" "5505822317" "2535940812" "8481117450"
 [17] "2841027019" "3852110555" "9644622948" "9549303819" "6442881097" "5665933446" "1284756482" "3378678316"
 [25] "5271201909" "1456485669" "2346034861" "0454326648" "2133936072" "6024914127" "3724587006" "6063155881"
 [33] "7488152092" "0962829254" "0917153643" "6789259036" "0011330530" "5488204665" "2138414695" "1941511609"
 [41] "4330572703" "6575959195" "3092186117" "3819326117" "9310511854" "8074462379" "9627495673" "5188575272"
 [49] "4891227938" "1830119491" "2983367336" "2440656643" "0860213949" "4639522473" "7190702179" "8609437027"
 [57] "7053921717" "6293176752" "3846748184" "6766940513" "2000568127" "1452635608" "2778577134" "2757789609"
 [65] "1736371787" "2146844090" "1224953430" "1465495853" "7105079227" "9689258923" "5420199561" "1212902196"
 [73] "0864034418" "1598136297" "7477130996" "0518707211" "3499999983" "7297804995" "1059731732" "8160963185"
 [81] "9502445945" "5346908302" "6425223082" "5334468503" "5261931188" "1710100031" "3783875288" "6587533208"
 [89] "3814206171" "7766914730" "3598253490" "4287554687" "3115956286" "3882353787" "5937519577" "8185778053"
 [97] "2171226806" "6130019278" "7661119590" "9216420198"
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34