Replies: 4 comments
-
The functions here are for the sums and ssq of digits: There are 3 possible functions here: Then we have to wrap sapply in this so it copes with a vector. Can we use piping in the function? The code is taken from here In the pre-programmed key I propose to use a simpler version. The code above will also cope with negative numbers and with decimals, where it only considers the numbers before the decimal points. The simpler code here is used for the key itself, (I think it gives NA if any number has a minus or decimal point, which is still ok. These are as follows: sumd: sapply( ,function(n){a<-as.integer(c(strsplit(as.character(n),split="")[[1]])); sum(a)}) So I am thinking that these would be able to execute the pre-programmed versions, while the others (again) only work after the function code has been run! These would then be on successive rows of a keyboard, and I am then also looking for more functions. Or there might be alternative coding of these examples. The keys I am happiest with are then the ssqd and ssqd1 keys. They give the calculations needed for the happy numbers. Ideally they could then be programmed to repeat this k times into successive variables. With a number this is as follows - take 7 > 49 > 897 > 130 > 10 > 1 > 1 > 1 - so it is happy! |
Beta Was this translation helpful? Give feedback.
-
It is possible that factorization could provide new functions. At least I have now found the function I would like from the conf.design package. This is interesting, because, although not updated since 2013it discusses the value of factorization in experimental design! So I suggest we add this package to R-Instat and also add factorization1 to the integer keyboard. It gives a list column, which then also is a good example data for the mode - also a list column. Then perhaps there is something of interest here - particularly being able to factorize large numbers. This might add to our new proposed Special keyboard. |
Beta Was this translation helpful? Give feedback.
-
I was reading the probability book in the murderous maths series and it tells us to mark the page specially that shows Pascal's triangle - this is from 1 up to 11. They seem very simple as functions, and store the results in an obvious way, so in a list! So that's a great example for us: One solution is here:
And - perfect for us - here is the next version - showing how explicit loops can often be avoided:
And an alternative given later, takes us back to our use of sapply:
What a great example that is useful in probability and excellent to discuss programming! This is the code that works as a calculator key and is discussed in #8036 . This is now in the probability keyboard. It could still be in the special keyboard and perhaps the pascal function could be run anyway, so the probability keyboard command looks simpler.
This is where the integer variable is called x1 - and is probably 1, 2, 3, The resulting list looks fine up to 32. Then it goes into scientific notation - not a real problem, because the coefficients have become too large. Another example that is a great use of a function is the fractions - and their reverse. This is on the transform keyboard and the function there could perhaps use the decimals function that we pre-program. Then the code is repeated on the special - or functions - keyboard? |
Beta Was this translation helpful? Give feedback.
-
@lilyclements and @lloyddewit I have been driven back to this topic following a simple question from Talu in INNODEMS, that is (I think) in work for Emily (SAMI) and that came to me from Beryl. This was "I was helping Talu do some work with the calculator and there was a question that needed us to generate a column that categorizes the numbers generated as even or odd, but we could not see a key to use so I asked. " When I looked it is a good example that is so easy that proper R people don't see the need for a function, see here for example. We could have the result as 1, or 0, or True/False, or I'm thinking that the keyboard gives 2 options for each task. Here there would be a key that is called The whole keyboard is like that. Those functions are (deliberately) not there initially. But the proposed R package will have a script that runs them all. Then the simple key version springs into life! |
Beta Was this translation helpful? Give feedback.
-
I, at last, begin to see how we might structure an R course, for R-Instat users.
Some may avoid this course as much as they can - this may depend on their assessment. Others may skim through it, because they already think they know R.
For those who have ambitions to become an R whizz, they may then soon wish to graduate from R-Instat to using RStudio. That's fine. We like RStudio. Our aim is to support good data science and with it good statistical analyses of the data. R-Instat is designed to help this process, particularly for those who might be daunted by the steep learning curve, if you start with R in its neat form.
We also suggest that many who graduate to using RStudio may then have 2 different types of work. There are tasks they do themselves, and there are tasks where they work jointly with those who prefer the more visual approach with R-Instat. Then, if you drop back to R-Instat for your joint work, they can understand and contribute more. (If your aim is to impress them with your new skills, then stick with RStudio.)
Start by switching on the commands.
Then take the opportunity to read as well as to write.
Then commands and scripts and functions and packages in turn
Here I am now interested in the section on functions. I make the case that commands and scripts are for your work - and even for most of your sharing work. Functions are really easy in R, and they are particularly useful when you also consider sharing, so that they may be used by others, when you are not there. And a package is for sharing. So here we don't show much about writing a package. But we do show how you can load a new package into R-Instat and use it.
Functions
This is to teach users about writing and then using functions.
a) In the summary keyboard there is no ssq (sum of squares function/key. One reason is because (if you know it), the sum function, on the keyboard, can easily be used. The sum key gives the sum, e.g. sum(x1) for a variable x1 and sum(x!*x!) or sum(x1^2) gives the sum of squares! What could be simpler!
Well simpler, for those for whom the R-Instat calculator is stretching them, is to have a ssq key. And we have put it on the Special keyboard. @lloyddewit I am thinking of adding this keyboard and also having a whole set of functions in the library that could be run to add these functions. The first key might be ssq and the first function in your list might be that function.
The key
ssq
could be the codesum( ^2,na.rm=TRUE)
- this is given.The next key is ssq1 is
ssq1( )
.This will say function not found unless they run the library example(s)
Then we have the key
css
, which issum( - mean( ))^2
.That's a bit more awkward, because you have to insert the column twice. So nice to have a function, see below, which is the key css1, see below.
This is, an easy function, as follows:
Similarly the corrected sum of squares is described here.
This suggests 2 ways:
Another entry also prefers the var(x) approach, because var(x) is coded in C and is therefore faster.
I would prefer the first, for example:
x1 <- c(2,3,4,5)
css1 <- function(x) {
sum( (x - mean(x) )^2 ,na.rm=TRUE)
}
css1(x1)
However, I now have a problem and I am so feeble in R that I can't solve it. In the examples above, and unlike the stack-overflow examples, I have added the na.rm=TRUE. The stack-overflow solutions don't mention the possibility of missing values. I have "hard-wired" na.rm=TRUE in the code above - not in any of the stack overflow solutions. So their assumption gives NA when there are any missing values.
The proper sum function has the additional na.rm argument, and we should be able to add it to our functions. I tried, but failed. I suggest we might have an additional css2, which adds that option. And we discuss the idea that these functions might be of use by others, and even for yourself, you might want to make the options/limitations more visible.
My next set is possibly the sum of the digits and the sum of squares of the digits.
Beta Was this translation helpful? Give feedback.
All reactions