importing list of 10^6 digits

hi
i have a pi_million.txt file containing 10^6 digits of pi
presented on 10.000 lines of 100 digits each
the lines are separated by breakline, the digits are not separated
my aim is to get a unique vector with the 10^6 digits
i have tried things but didnt get it
any help welcome

truc<-read.csv("pi_million.txt",sep="",dec=".",header = FALSE)
truc$V1
truc$V1->truc
liste<-strsplit((truc[]),"")
liste[-1]->liste
liste[-1]->liste
#as.numeric(liste[1]) doesnt work

If you are reading the digits into a single column of a data frame with each row holding 100 digits, I think all you need is the paste() function and its collapse argument.

DF <- data.frame(Digits = c(paste(rep(1:3, 10),collapse = ""),
                             paste(rep(2:4, 10),collapse = ""),
                             paste(rep(3:5, 10),collapse = ""),
                             paste(rep(4:6, 10),collapse = "")))
DF
                          Digits
1 123123123123123123123123123123
2 234234234234234234234234234234
3 345345345345345345345345345345
4 456456456456456456456456456456
AllDigits <- paste(DF$Digits, collapse = "")
AllDigits
[1] "123123123123123123123123123123234234234234234234234234234234345345345345345345345345345345456456456456456456456456456456"

I would use directly readLines() (since you don't need a data.frame), then strsplit() to slit each line into its components, and unlist() if you want the final format as a vector (FJCC's paste() being the way to go if you want the final data as a string).

# FJCC's fake data
original <- c(paste(rep(1:3, 10),collapse = ""),
              paste(rep(2:4, 10),collapse = ""),
              paste(rep(3:5, 10),collapse = ""),
              paste(rep(4:6, 10),collapse = ""))

original
#> [1] "123123123123123123123123123123" "234234234234234234234234234234"
#> [3] "345345345345345345345345345345" "456456456456456456456456456456"
writeLines(original, "saved_pi.txt")


readLines("saved_pi.txt") |>
  strsplit(split = "") |>
  unlist() |>
  as.integer()
#>   [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 2 3 4 2 3 4 2
#>  [38] 3 4 2 3 4 2 3 4 2 3 4 2 3 4 2 3 4 2 3 4 2 3 4 3 4 5 3 4 5 3 4 5 3 4 5 3 4
#>  [75] 5 3 4 5 3 4 5 3 4 5 3 4 5 3 4 5 4 5 6 4 5 6 4 5 6 4 5 6 4 5 6 4 5 6 4 5 6
#> [112] 4 5 6 4 5 6 4 5 6

Created on 2022-03-13 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.