[R] Optimize code to read text-file with digits

Martin Møller Skarbiniks Pedersen traxplayer at gmail.com
Fri Sep 8 15:51:21 CEST 2017


On 8 September 2017 at 14:37, peter dalgaard <pdalgd at gmail.com> wrote:
>
>
> > On 8 Sep 2017, at 14:03 , peter dalgaard <pdalgd at gmail.com> wrote:
> >
> > x <- scan("~/Downloads/digits.txt")
> > x <- x[-seq(1,220000,11)]
>
> ...and, come to think of it, if you really want the 1000000 random digits:
>
> xx <- c(outer(x,10^(0:4), "%/%")) %% 10
>

Hi Peter,
  Thanks a lot for the answers. I can see that I need to read about outer().
  However I get a different result than expected.

R> x <- scan("digits.txt")
Read 220000 items

R> head(x)
[1]     0 10097 32533 76520 13586 34673

R> x <- x[-seq(1,220000,11)]
R> head(x)
[1] 10097 32533 76520 13586 34673 54876

R> head(c(outer(x,10^(0:4), "%/%")) %% 10, 10) #
[1] 7 3 0 6 3 6  9 7 2 5

Regards
Martin

	[[alternative HTML version deleted]]



More information about the R-help mailing list