From 58d5dad0321d33b1135045f9a51b1a70d70eb936 Mon Sep 17 00:00:00 2001 From: Wei Wei Date: Thu, 22 Sep 2016 12:39:23 -0400 Subject: [PATCH] I finished lessons of probablities. --- .gitignore | 4 ++++ Class 4 - Swirl.Rproj | 13 +++++++++++++ cards.R | 23 +++++++++++++++++++++++ lesson1.csv | 28 ++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 .gitignore create mode 100644 Class 4 - Swirl.Rproj create mode 100644 cards.R create mode 100644 lesson1.csv diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/Class 4 - Swirl.Rproj b/Class 4 - Swirl.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/Class 4 - Swirl.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/cards.R b/cards.R new file mode 100644 index 0000000..a1a773b --- /dev/null +++ b/cards.R @@ -0,0 +1,23 @@ +values <- c("A", as.character(2:10), "J", "Q", "K") +suits <- c("spades", "hearts", "diamonds", "clubs") + +# Create a deck as a 13x4 matrix which is easy to verify by eye. +deck <- sapply(suits, function(suit)paste(values, suit, sep=":")) +# Verify. +deck +# Note the value of a card is its row index (if Aces are low.) + +# Select n cards from a deck at random without replacement. +hand <- function(n, deck)sample(deck, n, replace=FALSE) + +# Deal k hands of n cards each as a kxn matrix. +deal <- function(k, n, deck){ + # Select kxn cards at random without replacement. + temp <- hand(k*n, deck) + # Reshape selections into a kxn matrix. Since R + # fills by column, this is like dealing the cards + # out in circular order around the table. (Not that + # it makes any difference since the selection process + # is a random permutation of the deck anyway.) + matrix(temp, k, n) +} \ No newline at end of file diff --git a/lesson1.csv b/lesson1.csv new file mode 100644 index 0000000..7668670 --- /dev/null +++ b/lesson1.csv @@ -0,0 +1,28 @@ +"time","answer","id" +1474557960867,"install.packages(swirl)","YOUR NAME" +1474558061782,"library(swirl)","YOUR NAME" +1474558106237,"swirl()","YOUR NAME" +1474559387829,"11/12","YOUR NAME" +1474559439020,"deck","YOUR NAME" +1474559450620,"52","YOUR NAME" +1474559464553,"4/52","YOUR NAME" +1474559484601,"0","YOUR NAME" +1474559512863,"12/52","YOUR NAME" +1474559599848,"5/25","YOUR NAME" +1474559683724,"2/52","YOUR NAME" +1474559702359,"2/51","YOUR NAME" +1474559844628,"H <-read.table(~/.rstudio-desktop/history_database, sep=:, fill=T, stringsAsFactors=F)","YOUR NAME" +1474559844672,"names(H) <- c(time, answer)","YOUR NAME" +1474559844679,"H$id <- YOUR NAME","YOUR NAME" +1474559844690,"write.csv(H, file = lesson1.csv, row.names = FALSE)","YOUR NAME" +1474559858493,"swirl()","YOUR NAME" +1474559988869,"H <-read.table(~/.rstudio-desktop/history_database, sep=:, fill=T, stringsAsFactors=F)","YOUR NAME" +1474559988903,"names(H) <- c(time, answer)","YOUR NAME" +1474559988924,"H$id <- YOUR NAME","YOUR NAME" +1474559988939,"write.csv(H, file = lesson1.csv, row.names = FALSE)","YOUR NAME" +1474560044995,"swirl()","YOUR NAME" +1474560722478,"1.28","YOUR NAME" +1474560741861,"0.64","YOUR NAME" +1474560770116,"0.64","YOUR NAME" +1474560967720,"bye()","YOUR NAME" +1474561079682,"H <-read.table(~/.rstudio-desktop/history_database, sep=:, fill=T, stringsAsFactors=F)","YOUR NAME"