Lesson 2 Complete

This commit is contained in:
Dusty.P 2018-04-17 21:21:19 -08:00
parent 6f865b5ff5
commit 8c07674904
6 changed files with 48 additions and 7 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata

View File

@ -1,9 +1,16 @@
Title Speed and Stopping Distances of Cars
======================================================== ========================================================
This is an R Markdown document or RMD. Markdown is a simple formatting syntax for authoring web pages (click the **Help** toolbar button for more details on using R Markdown). This is an R Markdown document or RMD. Markdown is a simple formatting syntax for authoring web pages (click the **Help** toolbar button for more details on using R Markdown).
```{r}
?cars
str(cars)
```
```{r fig.width=7, fig.height=6}
plot(cars)
```

View File

@ -55,7 +55,7 @@ numbers
udacious <- c("Chris Saden", "Lauren Castellano", udacious <- c("Chris Saden", "Lauren Castellano",
"Sarah Spikes","Dean Eckles", "Sarah Spikes","Dean Eckles",
"Andy Brown", "Moira Burke", "Andy Brown", "Moira Burke",
"Kunal Chawla", YOUR_NAME) "Kunal Chawla", "Dustin Pianalto")
# Notice how R updates 'udacious' in the workspace. # Notice how R updates 'udacious' in the workspace.
# It should now say something like 'chr[1:8]'. # It should now say something like 'chr[1:8]'.

View File

@ -24,13 +24,14 @@ Subset the data so that you create a new data frame that contains
cars that get 23 or more mpg (miles per gallon). Save it to a new data cars that get 23 or more mpg (miles per gallon). Save it to a new data
frame called efficient. frame called efficient.
```{r} ```{r}
efficient <- mtcars[mtcars$mpg >= 23,]
efficient
``` ```
3. How many cars get more than 23 mpg? Use one of the commands you 3. How many cars get more than 23 mpg? Use one of the commands you
learned in the demystifying.R to answer this question. learned in the demystifying.R to answer this question.
```{r} ```{r}
dim(efficient)
``` ```
4. We can also use logical operators to find out which car(s) get greater 4. We can also use logical operators to find out which car(s) get greater
@ -66,13 +67,15 @@ Now you try some.
6. Print the cars that have a 1/4 mile time (qsec) less than or equal to 6. Print the cars that have a 1/4 mile time (qsec) less than or equal to
16.90 seconds to the console. 16.90 seconds to the console.
```{r} ```{r}
mtcars[mtcars$qsec <= 16.90,]
``` ```
7. Save the subset of cars that weigh under 2000 pounds (weight is measured in lb/1000) 7. Save the subset of cars that weigh under 2000 pounds (weight is measured in lb/1000)
to a variable called lightCars. Print the numbers of cars and the subset to the console. to a variable called lightCars. Print the numbers of cars and the subset to the console.
```{r} ```{r}
lightCars <- mtcars[mtcars$wt <= 2.0,]
dim(lightCars)
lightCars
``` ```
8. You can also create new variables in a data frame. Let's say you wanted 8. You can also create new variables in a data frame. Let's say you wanted
@ -104,7 +107,7 @@ Open the table of values to see what values year takes on.
Drop the year variable from the data set. Drop the year variable from the data set.
```{r} ```{r}
mtcars <- subset(mtcars, select = -year)
``` ```
@ -164,6 +167,10 @@ answer a question to continue on in Lesson 2.
Which car(s) have an mpg (miles per gallon) greater than or equal to 30 Which car(s) have an mpg (miles per gallon) greater than or equal to 30
OR hp (horsepower) less than 60? Create an R chunk of code to answer the question. OR hp (horsepower) less than 60? Create an R chunk of code to answer the question.
```{r}
answer = mtcars[mtcars$mpg >= 30 | mtcars$hp < 60,]
answer
```
Once you have the answer, go the [Udacity website](https://www.udacity.com/course/viewer#!/c-ud651/l-729069797/e-804129319/m-811719066) to continue with Lesson 2. Once you have the answer, go the [Udacity website](https://www.udacity.com/course/viewer#!/c-ud651/l-729069797/e-804129319/m-811719066) to continue with Lesson 2.

13
lesson2/lesson2.Rproj Normal file
View File

@ -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

10
lesson2/reddit_graphs.R Normal file
View File

@ -0,0 +1,10 @@
reddit <- read.csv('reddit.csv')
table(reddit$employment.status)
summary(reddit)
library(ggplot2)
age.ordered <- ordered(reddit$age.range, levels = c('Under 18', '18-24', '25-34', '45-54', '55-64', '65 or Above', 'NA'))
qplot(data = reddit, x = age.ordered)
qplot(data=reddit, x=income.range)