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

peter dalgaard
2017-09-08T14:12:21Z

> On 8 Sep 2017, at 15:51 , Martin M?ller Skarbiniks Pedersen <traxplayer
at gmail.com> wrote:
>
> 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
>

Ah, right. You do get all the digits, but in the order of the last digit of each
5 digit number, then all the penultimate digits, etc. To get digits in the right
order, try

> xx <- c(t(outer(x,10^(4:0), "%/%"))) %% 10
> head(xx, 100)
  [1] 1 0 0 9 7 3 2 5 3 3 7 6 5 2 0 1 3 5 8 6 3 4 6 7 3 5 4 8 7 6 8 0 9 5
 [35] 9 0 9 1 1 7 3 9 2 9 2 7 4 9 4 5 3 7 5 4 2 0 4 8 0 5 6 4 8 9 4 7 4 2
 [69] 9 6 2 4 8 0 5 2 4 0 3 7 2 0 6 3 6 1 0 4 0 2 0 0 8 2 2 9 1 6 6 5

I.e., reverse the order of digit generation and transpose the matrix that
outer() creates (because matrices are column-major).



> Regards
> Martin
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com