IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 103 forks source link

Add a Pascal key to the probability or special keyboard #8036

Closed rdstern closed 1 year ago

rdstern commented 1 year ago

This is not to be done immediately. And I need help, because I can't yet do it as I would like.

The murderous maths book on probability declares that Pascal's triangle is one of the most gorgeous things in maths.

I would like to generate them as a list and it seems surprisingly easy. It uses the choose function for which there is already a key.

I discuss this in #8034 . I then found that the simple code:

lapply(x1,choose,0:100)is almost what I want:

image

But the list variable always has 101 elements. Nice if it had, 2, 3, etc for each row. Having 0:x1 gives just the first 2 values. I assume I need to define and run a function?

Maybe this is nice for the propose Special keyboard I am planning to support the teaching of programming.

anastasia-mbithe commented 1 year ago

@rdstern, My guess is that the pascal key be implemented with lapply(x1,choose,0:100), we can modify it to lapply(x1, choose,0:nrow(data)) this will control the number of 0's for smaller sets of data (It seems that the 0's are inevitable for now)

I have been looking at other possible ways and found numbers::pascal_triangle(n) function also produces it, though n<=50. and the output produced looks like below; (its limiting factor is n<=50, and since the length of the data set must be divisible by the length of the output produced, then for 20 rows; n=19) image

rdstern commented 1 year ago

@anastasia-mbithe I think I may have a solution - based on using a defined function. Your solution above is interesting, but I was pretty happy with one that puts the result in a single list column, rather than a set of columns.

Consider: pascalTriangle <- function(x) {lapply(0:x, function(i) choose(i, 0:i))}

I think as a calculator key we could have something like: lapply(x1, function(x) {lapply(0:x, function(i) {choose(i, 0:i)})}

But I can't get it to work. Don't spend too long on this as it is a luxury for now - but fun!

anastasia-mbithe commented 1 year ago

Its working, the code lacks a closing bracket. The output looks like this; image

I've also noticed an issue with overlapping on the grid, from row 14 downwards.

rdstern commented 1 year ago

Well done. I have now got it work:

lapply(x1, function(x) {lapply(x, function(i) {choose(i, 0:i)})})

It needs your closing bracket and it also needs to have x and not 0:x as the function.

Here it is in the R viewer:

image

This has a problem of going into scientific mode for large R, but that can be worried about later.

There is then a problem on how we display it in reogrid, but that's a different question.

Perhaps you can mention that question to @N-thony for #8015 or he can look at this, because he is working on the display of lists and I suggest he may need to check his code on this with someone else, because he has had many attempts at resolving this.

It would be good if his display can match the display in the R viewer.