Git Product home page Git Product logo

hopr's People

Contributors

adam-gruer avatar blairj09 avatar chuliangxiao avatar cwickham avatar dallinwebb avatar fjtkfm avatar garrettgman avatar nmoorenz avatar psychometrician avatar strboul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hopr's Issues

Slot Machine

The slot machine project does not consider the diamonds as wild symbols, so that means that the only way to "duplicate" the prize is if you have 3 diamonds. I don't know if this was intentional.

8.2 Wording Change "heartache" to "future headaches"

Notice that assign works similar to <-, If an object already exists with the given name in the given environment, assign will overwrite it without asking for permission. This makes assign useful for updating objects but creates the potential for

heartache.

Notice that assign works similar to <-, If an object already exists with the given name in the given environment, assign will overwrite it without asking for permission. This makes assign useful for updating objects but creates the potential for

future headaches.

#36

Exercise 5.4 does not reinforce the section it's in

It is customary that exercises within a section reinforce something about that section.

Exercise 5.4's placement, at the end of section 5.5.2, means it should reinforce 5.5.2 (factors) or section 5.5 (classes). However, this exercise illustrates nothing about factors or classes. Rather, it is rather an introduction to section 5.6, which is about coercion.

Options for fixing:

  • Change it into a demonstration in section 5.6.
  • (less preferred because it's unclean) Add a disclaimer to the top of exercise 5.4 to explain why it isn't about sections 5.5 or 5.5.2.

Minor grammar mistake in 9.4

"In this case, the prize’s aren’t identified by a symbol name..."

The use of "prize" here is not a possessive, so it shouldn't have an apostrophe.

a typo in prize for "BB"

  1. Exercise 9.5 (Test for All Bars)

if (same) {
symbol <- symbols[1]
if (symbol == "DD") {
prize <- 800
} else if (symbol == "7") {
prize <- 80
} else if (symbol == "BBB") {
prize <- 40
} else if (symbol == "BB") {
prize <- 5 ## should be “25”
} else if (symbol == "B") {
prize <- 10
} else if (symbol == "C") {
prize <- 10
} else if (symbol == "0") {
prize <- 0
}
}

numbering in Chapter 7 is off

The sub-part numbering in Chapter 7 isn't consistent with the rest of the book. Currently, it's numbered 7.0.1, 7.0.2, etc. instead of 7.1, 7.2, etc.

Minor edit

In the last sentence of the Summary of Section 2.7, change "writted" to "written"

Problem with link R-bloggers

In Appendix B R Packages > B.2 Loading Packages > What’s the best way to learn about R packages? the link of R-bloggers doesn´t work.

imagen

Little mistake 9.4 Lookup Tables

In section 9.4 Lookup Tables in "As in Case 1, you could write an if tree that handles each combination of cherries, but just like in Case 1, this would be an inefficient solution" there is a problem in:

if (cherries == 2) {
  prize <- 5
} else if (cherries == 1) {
  prize <- 2
} else {}
  prize <- 0
}

I think the correct code is:

if (cherries == 2) {
    prize <- 5
} else if (cherries == 1) {
    prize <- 2
} else {
    prize <- 0
}

8.6 does not work for me

Hi, the approach of:

setup <- function(deck) {
  DECK <- deck

  DEAL <- function() {
    card <- deck[1, ]
    assign("deck", deck[-1, ], envir = globalenv())
    card
  }

  SHUFFLE <- function(){
    random <- sample(1:52, size = 52)
    assign("deck", DECK[random, ], envir = globalenv())
 }

 list(deal = DEAL, shuffle = SHUFFLE)
}

cards <- setup(deck)

gives at my end an ogoing deal() output of 1 king spades 13.
I do not understand why this happens (on R4.3.3 on Slackware64 linux with RStudio 2023.12.1 Build 402).

My unsuccessful run:

> decki <- deck
> decki$value
 [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6
[22]  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11
[43] 10  9  8  7  6  5  4  3  2  1
> setup <- function(decki) {
+   DECK <- decki
+ 
+   DEAL <- function() {
+     card <- decki[1, ]
+     assign("decki", decki[-1, ], envir = globalenv())
+     card
+   }
+ 
+   SHUFFLE <- function(){
+     random <- sample(1:52, size = 52)
+     assign("decki", DECK[random, ], envir = globalenv())
+   }
+ 
+   list(deal = DEAL, shuffle = SHUFFLE)
+ }
> decki$value
 [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6
[22]  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11
[43] 10  9  8  7  6  5  4  3  2  1
> cards <- setup(decki)
> deal <- cards$deal
> shuffle <- cards$shuffle
> decki$value
 [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6
[22]  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11
[43] 10  9  8  7  6  5  4  3  2  1
> shuffle()
> decki$value
 [1]  8  9 11  6  4  5 13  4  3  6  1  5  7 13  2 10  6  7  7  6  1
[22]  8  3 10 11 13 12  7  4  9 11  9 12 10 11  2  3 12  1  9 13  5
[43]  8  2  4  5  2  1 12  8  3 10
> deal()
  face   suit value
1 king spades    13
> deal()
  face   suit value
1 king spades    13
> deal()
  face   suit value
1 king spades    13
> shuffle()
> decki$value
 [1]  8  8  1  4  7  9  6  2 11  6 13  7 10  9 11  1  7  6  2 12  5
[22] 13 12 11 10  6 13  2  4  8  9 13  2  4  3 12  3  1  5 12  3  9
[43] 10  4  1  8  5 10  5 11  7  3
> deal()
  face   suit value
1 king spades    13
> deal()
  face   suit value
1 king spades    13
> deal()
  face   suit value
1 king spades    13
> deal()
  face   suit value
1 king spades    13
> globalenv()
<environment: R_GlobalEnv>
> environment()
<environment: R_GlobalEnv>
> environment(setup)
<environment: R_GlobalEnv>
> environment(deal)
<environment: 0x5620e4976028>
> environment(shuffle)
<environment: 0x5620e4976028>
> environment(deck)

I only got this to work with changing all calls to refer to the setup-environment; not the global one. With an extra function STOCK that returns the current state of the used deck one can see what happens after SHUFFLE and DEAL...

#> deckj <- deck
> setup2 <- function(deckj){
+   DECK <- deckj
+ 
+   DEAL <- function(){
+     card <- DECK[1, ]
+     assign("DECK", DECK[-1, ], envir = parent.env(environment()))
+     card
+   }
+ 
+   SHUFFLE <- function(){
+     random <- sample(1:52, size = 52)
+     assign("DECK", deckj[random, ], envir = parent.env(environment()))
+   }
+ 
+   STOCK <- function(){
+     DECK
+   }
+ 
+   list(deal2 = DEAL, shuffle2 = SHUFFLE, stock2 = STOCK)
+ }
> cards2 <- setup2(deck)
> cards2
$deal2
function(){
    card <- DECK[1, ]
    assign("DECK", DECK[-1, ], envir = parent.env(environment()))
    card
  }
<bytecode: 0x5620e5b7b8b8>
<environment: 0x5620e719c620>

$shuffle2
function(){
    random <- sample(1:52, size = 52)
    assign("DECK", deckj[random, ], envir = parent.env(environment()))
  }
<bytecode: 0x5620e4b486f8>
<environment: 0x5620e719c620>

$stock2
function(){
    DECK
  }
<bytecode: 0x5620e4a23750>
<environment: 0x5620e719c620>

> deal2 <- cards2$deal2
> shuffle2 <- cards2$shuffle2
> stock2 <- cards2$stock2
> stock2()$value
 [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6
[22]  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11
[43] 10  9  8  7  6  5  4  3  2  1
> shuffle2()
> stock2()$value
 [1]  8  1  6  5  1  3  6  3  9 10 11  7  5 13 12  9  4  5 12 11  6
[22] 12  7 12  5  8  8  7 10 10  4  8  2 13  3 11 11  1 13  6  3  9
[43]  7  9  2 13  1 10  4  2  4  2
> deal2()
    face  suit value
19 eight clubs     8
> deal2()
   face  suit value
26  ace clubs     1
> deal2()
  face   suit value
8  six spades     6
> deal2()
   face     suit value
35 five diamonds     5
> stock2()$value
 [1]  1  3  6  3  9 10 11  7  5 13 12  9  4  5 12 11  6 12  7 12  5
[22]  8  8  7 10 10  4  8  2 13  3 11 11  1 13  6  3  9  7  9  2 13
[43]  1 10  4  2  4  2
> deal()
Error in deal() : could not find function "deal"
> deal2()
   face     suit value
39  ace diamonds     1

Trying the original setup again:

> decki$value
 [1] 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6  5
[22]  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10
[43]  9  8  7  6  5  4  3  2  1
> rm(decki)
> rm(deal)
> rm(shuffle)
> decki <- deck
> setup3 <- function(decki) {
+   DECK <- decki
+ 
+   DEAL <- function() {
+     card <- decki[1, ]
+     assign("decki", decki[-1, ], envir = globalenv())
+     card
+   }
+ 
+   SHUFFLE <- function(){
+     random <- sample(1:52, size = 52)
+     assign("decki", DECK[random, ], envir = globalenv())
+   }
+ 
+   list(deal3 = DEAL, shuffle3 = SHUFFLE)
+ }
> decki$value
 [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6
[22]  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11
[43] 10  9  8  7  6  5  4  3  2  1
> cards3 <- setup3(decki)
> deal3 <- cards3$deal3
> shuffle3 <- cards3$shuffle3
> shuffle3()
> decki$value
 [1] 11  6  9  2  9  1  2 12  5 12  4  3  3  6  3  7  4  9  8 10  1
[22]  7  4  1  6  9  4  5 10 11 11 13  2 10  6 12 13  8  8  7  8  7
[43]  5 13  3 13 12 11 10  1  5  2
> deal3()
  face   suit value
1 king spades    13
> deal3()
  face   suit value
1 king spades    13
> deal3()
  face   suit value
1 king spades    13
> decki$value
 [1] 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6  5
[22]  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10
[43]  9  8  7  6  5  4  3  2  1
> 

The original decki is returned after deal(), not the shuffled one...

Not about the book content but use of the book as a lecture material

Hi,

I wonder if the book titled "Hands-On Programming with R" can be used without any charge or for free as a lecture material in a class at a university. Because it is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 4.0, I am not sure whether I could let the students use as a textbook in a class and use the contents of the book to make some slides for lecture notes. Now I have realized that making slides from some of the book might violate the license becaused of "NoDerivs." But, still, I wonder how I could use the book is a class at a university as a lecture material.

Thank you,
SangChul

Figure 2.4 incorrect math

The math in Figure 2.4 of the "Hands-On Programming with R" book is incorrect. Either the *s should be changed to +s, or the 3rd, 4th, 5th, and 6th products need correcting. For example,
3 * 1 ≠ 4, and 4 * 2 ≠ 6, etc.

figure2 4

It's unclear if the authors intended to demonstrate addition or multiplication here.

Update link in 10.7 S4 and R5

In section 10.7 S4 and R5 you can update the link with 2nd edition of “Advanced R” and maybe talk about R6 instead of R5.

Off course, it is only a suggestion. I am only one of the readers of the book.

Insignificant typo

In 2.7, the last line about packages reads

which are collections of functions writted by R’s talented community

written instead of writted

T and F are risky

Please warn new R users in 5.1.4 that T can be redefined but TRUE cannot, or don't mention T and F at all until later if you think that is too much detail. RStudio is quite helpful with tab completion... so it really isn't that hard to develop good habits early.

Minor issues/typos

Thank you so much for this wonderful resource! I’d love to pay forward and share the few minor issues/typos that I spotted while working through Hands-On Programming with R.

  • 5.7 choose “subelement” or “sub-element” for consistency
  • 7.0.2 “indivuidually compare” --> typo in “individually”
  • 8.8 “the function’s origin enviornment” --> typo in “environment”
  • 9.1 “If a subtask seems complicated, try to divide it again into even subtasks that are even more simple.” --> I’m assuming delete the first “even” so that it reads “[…] try to divide it again into subtasks that are even more simple.”
  • 9.3 length(unique(symbols) == 1) shouldn’t this be length(unique(symbols)) == 1 ?
  • 9.3 Some of the brackets {} are highlighted in red but should probably just be black.
  • Very end of 9.5: play() ## "BB" "BB" "B" ## 25 --> shouldn’t that be 5 (any combination of bars)?
  • Exercise 11.7 (Calculate the Expected Value): “Solution.” header missing
  • 12.1/2 function specified as “abs-sets” in the code but referred to as “abs-set” (“s” missing) in the main body of text
  • 12.3 “abs_loop and change_many illustrate a characteristic of vectorized code: […]” --> change “change_many” to “change_symbols” in two instances, I think.

Thank you very much once again. I hope this is helpful :-)

Missing construction of the roll() function?

(Sorry, I spoke too soon before reading on. Closed/completed.)

Perhaps I'm missing something, but it sounds like there should be an example of how to write the roll() function here.

Something like:

roll <- function(x = die) {sum(sample(x, size = 2, replace = TRUE))}

image

A little Problem in section 5.5.2 Factors

5.5.2 Factors :
"R uses the levels attribute when it displays the factor, as you will see. R will display each 1 as female, the first label in the levels vector, and each 2 as male, the second label. If the factor included 3s, they would be displayed as the third label, and so on: …"

It's wrong! Because if we add an object that start with a letter that's alphabetically before 'f' , for example starts with 'a' in 'another' , the labels of factor class will change. It will be :
'another' ---> 1
'female' ---> 2
'male' ---> 3
To be more obvious, I write this part of code:

gender <- factor(c("male", "female", "another", "female", "another", "male"))
unclass(gender)
## [1] 3 2 1 2 1 3
## attr(,"levels")
## [1] "another" "female"  "male"

You saw that the label of "another" is 1. That means the numbering of the factor is alphabetical.

sorry for my bad English. :)

Typo in 12.2

change_vec2 <- function(vec){
tb <- c("DD" = "joker", "C" = "ace", "7" = "king", "B" = "queen",
"BB" = "jack", "BBB" = "ten", "0" = "nine")
unname(tb[vec])
}

system.time(change_vec(many))

write code should be
system.time(change_vec2(many))

deal <- function(cards) in 6.2 Deal a Card

I am a beginner and 'am very impressed with the book's method of teaching. I think it would help if you explained why you used function(cards), as "cards" itself was not defined as a variable before this use
deal <- function(cards) {

?

}
and followed by...
deal <- function(cards) {
cards[1, ]
}
For e.g. if we run > deal(), gives the following error...
Error in deal() : argument "cards" is missing, with no default

Faster and simpler vectorised prize calculation

I took the advice in the book to implement a faster vectorised score-calculation function using rowSums and vectorised selective updates. The method of decomposing the problem differs from that in the book. Namely, wild diamonds are handled by adding the diamond count to each check, as, per the problem description, the diamonds can be thought of as any other symbol (an exception is made for the cherry cases).

score.count.fast <- function(symbols) {
    # counts of symbols in each sample
    dd <- rowSums(symbols == "DD")
    x7 <- rowSums(symbols == "7")
    b3 <- rowSums(symbols == "BBB")
    b2 <- rowSums(symbols == "BB")
    b1 <- rowSums(symbols == "B")
    cc <- rowSums(symbols == "C")
    # calculate prize with selective updating (higher prizes later to override lower ones)
    prize = integer(nrow(symbols)) # defaults to a number of 0s
    prize[cc == 1]                  <-   2
    prize[cc > 0 & cc + dd == 2]    <-   5
    prize[b3 + b2 + b1 + dd == 3]   <-   5
    prize[cc + dd == 3]             <-  10
    prize[b1 + dd == 3]             <-  10
    prize[b2 + dd == 3]             <-  25
    prize[b3 + dd == 3]             <-  40
    prize[x7 + dd == 3]             <-  80
    prize[dd == 3]                  <- 100
    # apply diamonds doubling effect
    prize * (2 ^ dd)
}

The resulting function is about 3x shorter and, on my machine, about 3x faster than the book's example solution. In my opinion, it's also significantly simpler in the way it handles wild symbols, and I think the method could also easily be generalised to new similar sets of rules without significant alteration. A branch-based, non-vectorised version is also possible, which is of similar complexity and speed to the book's example (but it doesn't serve as a good demonstration of vector lookups).

score.count <- function(symbols) {
   # count symbols
   dd <- sum(symbols == "DD")
   x7 <- sum(symbols == "7")
   b3 <- sum(symbols == "BBB")
   b2 <- sum(symbols == "BB")
   b1 <- sum(symbols == "B")
   cc <- sum(symbols == "C")
   # calculate prize (higher prizes detected before lower ones)
   if (dd == 3) {
       prize <- 100
   } else if (x7 + dd == 3) {
       prize <- 80
   } else if (b3 + dd == 3) {
       prize <- 40
   } else if (b2 + dd == 3) {
       prize <- 25
   } else if (b1 + dd == 3) {
       prize <- 10
   } else if (cc + dd == 3) {
       prize <- 10
   } else if (b3 + b2 + b1 + dd == 3) {
       prize <- 5
   } else if (cc > 0 && cc + dd == 2) {
       prize <- 5
   } else if (cc == 1) {
       prize <- 2
   } else {
       prize <- 0
   }
   prize * (2 ^ dd)

I tested both versions and they give identical output to the book's code for all combinations of symbols. An rmarkdown script for testing and for profiling with microbenchmark is attached:

This issue serves three purposes

  1. Post the alternative method here for others to learn from if interested
  2. Contribute the simpler method in case the author would like to adapt it for use in future versions of the book
  3. Invite others to further improve the efficiency of the method and share their approaches and results

My thanks to the author and all contributors for their work on making an excellent open source book that helped me efficiently learn the fundamentals of R.

Histogram info

The following text is in section "3.1.2 Library" of Hands-On Programming with R:

"You can use a histogram to display visually how common different values of x are. Numbers covered by a tall bar are no more common than numbers covered by a short bar."

Should the text read, "Numbers covered by a tall bar are more common..."?

Typo in last part of 10.3 Generic Functions

In section 10.3 Generic Functions in "You may imagine that print looks up the class attribute of its input and then uses an +if+ tree to pick which output to display" maybe the "+if+" is "if" surrounder by `` and not by ++

Typo in 2.1, sequence 100:130

The third line begins with the 27th value, not the 25th.
The code box should also be corrected from [25] in the beggining of the third line to [27].

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.