Teach girls bravery, not perfection

Hi Folks,

I heard this very interesting bit on the Ted Radio hour on NPR this weekend by Reshma Saujani, who founded “Girls who code”. I found it to have very interesting lessons for self-reflection and understanding. Please listen, even if you’re a boy :). Do you think there is something to it?

https://www.npr.org/2016/06/24/483131204/can-coding-help-girls-take-risks

Marguerite

Matching quantitive values

3) Some of you have emailed me regarding matching on numeric values. In general, it is not a good idea to try to use == on quantitative numeric (non-integer) values.

This is because numeric values are stored as double precision. Although it looks like 5.1, it may actually be 5.1000001 which will cause no match when you compare it to 5.1. It is actually stored to eight decimal places (+/- 10^-8). Itʻs perfectly fine to use == for discrete values (like index numbers which are integers, or factors or characters), but do not use them for quantitive values or you may be surprised.

Here is an example of where this might come up.

x <- seq( from=0, to=50, by=0.1)
which(x==5.5) # works
which(x==5.1) # doesnʻt work
which(x==5.3) # doesnʻt work

Instead, use:

which(x>5.0 & x<5.2 ) # Better way

Of course it may return more than one index. You can just take the mean index:
which( x>4.9 & x<5.3 )
[1] 51 52 53

ii <- which( x>4.9 & x<5.3 )
mean(ii)
[1] 52

Of course rather than using actual fixed numbers, you may want to have a guess for x called P_x and test in a small neighborhood around that point. So if your delta is .1, you could code it as:

> ii <- which((x>P_x-delta) & (x<P_x+delta))

mean(ii)

If you are concerned about getting an even number (and resulting in a mean with a .5 ending, just round down:

> ii <- floor(mean(ii))

Then the coordinates of for example your guess would be:

> P_x <- x[ii]

P_fx <- fx[ii]

If you create a vector of derivatives, you can also use ii to index that as well:

> dx <- derivative[ii] # if you store your vector of derivatives in “derivative”

Marguerite

help pages for control elements

A few questions came up in class – how to find these help pages:
while:  All of the control structures weʻve been talking about are on one help page, which you can see by typing
 
> ?Control
 (I donʻt know why, the person who wrote the code just chose to do it that way). Itʻs not a well-written help page unfortunately, short on details. But there you have it.
%in%:  This is a very very helpful function. You should read this and test the examples.
> ?”%in%”
Note: any function that is written in operator form has to be enclosed in quotes. For example, “+”“*” etc.
All of the histories and class videos are updated daily, on the page for each day (or you can look on the histories tab for histories).