HW 3: R Introduction For our third HW, we will take our knowledge of logic into the field of data analysis. We will use a data analysis programming language called R. Pre-HW learning references Please...

1 answer below »
HW 3: R Introduction For our third HW, we will take our knowledge of logic into the field of data analysis. We will use a data analysis programming language called R. Pre-HW learning references Please go back to Codecademy’s R page and complete the Lesson(s) inside the “Learn R: Fundamentals of Data Visualization with ggplot2” module. You do not need to submit anything for Codecademy. Setup If you were not in class, please download this file. Follow the lecture recording to set up your workspace. If you were in class, please use the same RStudio Cloud project with your existing code. Tasks Complete the remaining questions in the R file that we worked on in class. Assumptions, clarifications, and hints 1. You will need the penguins data set for the last section on visualization. This data set is built into R. After you run the library(palmerpenguins) line, access the penguins data set by using penguins, ex: penguins %>% 2. Use alpha of 0.2 in the last question. How to submit your R output Please watch the end of the lecture recording to see how to knit your output file. If you have questions, let me know on Campuswire. Submit your file as a PDF. The output file will only be generated if there are no errors in your code. If you cannot get a question, just leave it blank. You can add the code after we discuss the answers.



MGT 205 R - in class.Rmd --- title: "MGT 205 R - in class" author: "YOUR NAME HERE" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` # Basic Functionality of R and Variables ## Q1. Data Types ### a) R in its most basic form can be used as a calculator. Write some coded math expressions using at least three different operators (including at least one trigonometric function). Note: R follows the standard order of operations, but this can be controlled by wrapping any expression in (). ```{r} ``` ### b) Name and give an example of the five base data types in R: ```{r} ``` ### c) Construct a vector in R. A vector is a data structure in R that contains multiple entries of the same type. ```{r} ``` ## Q2. Variables Variables are a reference to data in R and can be assigned any valid data structure. Variables can be used anywhere the data that they are referencing could be and serve as the building blocks for working with data in R as they can be composed. ### a) Create numeric variables x and y independently, then add x and y to a new variable z. ```{r} ``` ## Q3. Functions * Built in functions R's built in functions known as 'base R' form an incredibly powerful statistical package that can be used in a range of ways to extract information from data. Here we cover the basic usage of very common R functions. ### a) #### i) Create a sequence from 1 up to 10 ```{r} ``` #### ii) What is the sum of all numbers from 1 to 12345? ```{r} ``` ### b) Generate 100 normally distributed random numbers ```{r} ``` ### c) #### i) Sample 30 random whole numbers between 1 and 500 ```{r} ``` #### ii) Pick 10 of the normally distributed numbers generated in b) at random. ```{r} ``` ### d) Simulate flipping a coin 100 times with with 1s and 0s standing for heads and tails respectively. Count the number of heads. ```{r} ``` #### R Packages and functions 'R packages' are collections of useful functions created by academics and industry professionals that have been published openly for use by anyone. One of the most widely used collection of packages is the tidyverse, which provides a consistent way of working with data from the beginning of an analysis to the end. #### User defined functions Functions can be defined as needed to simplify operations or reduce repetition in code. The function() keyword is used to do this. 'Arguments' are passed to functions when they are called with parentheses and are separated by commas. These arguments can be accessed inside the function. ### e) Define a function called add_one that adds 1 to its input. ```{r} ``` ### f) Define a function that takes 2 arguments and randomly returns one. ```{r} ``` # Reading Data and Working with Data Frames From here on out we will be using the tidyverse. The tidyverse provides a consistent way to work with data, and extends the functionality of base R. In particular, we will be using dplyr for data processing, along with ggplot2 for visualization and readr for data reading. ## Q1. Reading Data Run the code block below. ```{r} library(tidyverse) ``` ### a) Load in the starwars data. ```{r} ``` ### b) List the first 6 rows of starwars. ```{r} ``` ### c) Get the number of rows and number of columns in starwars. ```{r} ``` ## Q2. Data Pipelines ( %>% ) Often multiple processing steps are required for data frames, but the intermediate results are often not that important. There are a number of ways to apply functions in succession, but the preferred standard is to use the forward pipe operator %>%. This operator takes whatever expression is on its left hand side and uses this expression as the first argument of the function on its right hand side. i.e. x %>% f %>% g %>% h is equivalent to h(g(f(x))) https://r4ds.had.co.nz/pipes.html https://magrittr.tidyverse.org ### a) Take the head of starwars and find how many rows are in it. ```{r} ``` ## Q3. Working with Data Frames ### a) #### i) How many males are in the data set? ```{r} ``` #### ii) How many humans with blue eyes are in the data set? ```{r} ``` ### b) List the tallest 3 humans, showing only name and height columns. ```{r} ``` ### c) Show the mean height and mass of humans. #### i) ```{r} ``` #### ii) Show the mean height and mass of humans grouped by sex. ```{r} ``` ### d) Get the 2 shortest masculine and feminine characters, displaying only name, height and gender columns (you must remove missing values for gender). ```{r} ``` # Data Visualization Base R has its own plotting utilities, but they are rarely for creating presentable visualizations as they are difficult to configure and do not have consistent behavior for different plot types. We will be using the ggplot2 package, which is the industry standard for data visualization as it uses a consistent 'grammar' to construct all types of visualizations, produces high quality outputs by default and is easy to configure. We will also be using the palmerpenguins educational data package for the data underlying our plots. Run the code block below. ```{r} library(palmerpenguins) ``` ## Q1) ### a) Create a scatter plot with bill depth on the x axis and body mass on the y axis. ```{r} ``` ### b) Color this scatter plot by species. ```{r} ``` ### c) Add an appropriate human-readable title, along with axis and legend labels. ```{r} ``` ## Q2 Construct a filled bar chart of species, filled by island. ```{r} ``` ## Q3 ### a) Construct a histogram of flipper length. ```{r} ``` ### b) Facet this histogram by species ```{r} ``` ### c) Compare the flipper length distributions between different species using density geom with an alpha. ```{r} ``` starwars.csv name,height,mass,hair_color,skin_color,eye_color,birth_year,sex,gender,homeworld,species Luke Skywalker,172,77,blond,fair,blue,19,male,masculine,Tatooine,Human C-3PO,167,75,NA,gold,yellow,112,none,masculine,Tatooine,Droid R2-D2,96,32,NA,"white, blue",red,33,none,masculine,Naboo,Droid Darth Vader,202,136,none,white,yellow,41.9,male,masculine,Tatooine,Human Leia Organa,150,49,brown,light,brown,19,female,feminine,Alderaan,Human Owen Lars,178,120,"brown, grey",light,blue,52,male,masculine,Tatooine,Human Beru Whitesun lars,165,75,brown,light,blue,47,female,feminine,Tatooine,Human R5-D4,97,32,NA,"white, red",red,NA,none,masculine,Tatooine,Droid Biggs Darklighter,183,84,black,light,brown,24,male,masculine,Tatooine,Human Obi-Wan Kenobi,182,77,"auburn, white",fair,blue-gray,57,male,masculine,Stewjon,Human Anakin Skywalker,188,84,blond,fair,blue,41.9,male,masculine,Tatooine,Human Wilhuff Tarkin,180,NA,"auburn, grey",fair,blue,64,male,masculine,Eriadu,Human Chewbacca,228,112,brown,unknown,blue,200,male,masculine,Kashyyyk,Wookiee Han Solo,180,80,brown,fair,brown,29,male,masculine,Corellia,Human Greedo,173,74,NA,green,black,44,male,masculine,Rodia,Rodian Jabba Desilijic Tiure,175,1358,NA,"green-tan, brown",orange,600,hermaphroditic,masculine,Nal Hutta,Hutt Wedge Antilles,170,77,brown,fair,hazel,21,male,masculine,Corellia,Human Jek Tono Porkins,180,110,brown,fair,blue,NA,male,masculine,Bestine IV,Human Yoda,66,17,white,green,brown,896,male,masculine,NA,Yoda's species Palpatine,170,75,grey,pale,yellow,82,male,masculine,Naboo,Human Boba Fett,183,78.2,black,fair,brown,31.5,male,masculine,Kamino,Human IG-88,200,140,none,metal,red,15,none,masculine,NA,Droid Bossk,190,113,none,green,red,53,male,masculine,Trandosha,Trandoshan Lando Calrissian,177,79,black,dark,brown,31,male,masculine,Socorro,Human Lobot,175,79,none,light,blue,37,male,masculine,Bespin,Human Ackbar,180,83,none,brown mottle,orange,41,male,masculine,Mon Cala,Mon Calamari Mon Mothma,150,NA,auburn,fair,blue,48,female,feminine,Chandrila,Human Arvel Crynyd,NA,NA,brown,fair,brown,NA,male,masculine,NA,Human Wicket Systri Warrick,88,20,brown,brown,brown,8,male,masculine,Endor,Ewok Nien Nunb,160,68,none,grey,black,NA,male,masculine,Sullust,Sullustan Qui-Gon Jinn,193,89,brown,fair,blue,92,male,masculine,NA,Human Nute Gunray,191,90,none,mottled green,red,NA,male,masculine,Cato Neimoidia,Neimodian Finis Valorum,170,NA,blond,fair,blue,91,male,masculine,Coruscant,Human Jar Jar Binks,196,66,none,orange,orange,52,male,masculine,Naboo,Gungan Roos Tarpals,224,82,none,grey,orange,NA,male,masculine,Naboo,Gungan Rugor Nass,206,NA,none,green,orange,NA,male,masculine,Naboo,Gungan Ric Olié,183,NA,brown,fair,blue,NA,NA,NA,Naboo,NA Watto,137,NA,black,"blue, grey",yellow,NA,male,masculine,Toydaria,Toydarian Sebulba,112,40,none,"grey, red",orange,NA,male,masculine,Malastare,Dug Quarsh Panaka,183,NA,black,dark,brown,62,NA,NA,Naboo,NA Shmi Skywalker,163,NA,black,fair,brown,72,female,feminine,Tatooine,Human Darth Maul,175,80,none,red,yellow,54,male,masculine,Dathomir,Zabrak Bib Fortuna,180,NA,none,pale,pink,NA,male,masculine,Ryloth,Twi'lek Ayla Secura,178,55,none,blue,hazel,48,female,feminine,Ryloth,Twi'lek Dud Bolt,94,45,none,"blue, grey",yellow,NA,male,masculine,Vulpter,Vulptereen Gasgano,122,NA,none,"white, blue",black,NA,male,masculine,Troiken,Xexto Ben Quadinaros,163,65,none,"grey, green, yellow",orange,NA,male,masculine,Tund,Toong Mace Windu,188,84,none,dark,brown,72,male,masculine,Haruun Kal,Human Ki-Adi-Mundi,198,82,white,pale,yellow,92,male,masculine,Cerea,Cerean Kit Fisto,196,87,none,green,black,NA,male,masculine,Glee Anselm,Nautolan Eeth Koth,171,NA,black,brown,brown,NA,male,masculine,Iridonia,Zabrak Adi Gallia,184,50,none,dark,blue,NA,female,feminine,Coruscant,Tholothian Saesee Tiin,188,NA,none,pale,orange,NA,male,masculine,Iktotch,Iktotchi Yarael Poof,264,NA,none,white,yellow,NA,male,masculine,Quermia,Quermian Plo Koon,188,80,none,orange,black,22,male,masculine,Dorin,Kel Dor Mas Amedda,196,NA,none,blue,blue,NA,male,masculine,Champala,Chagrian Gregar Typho,185,85,black,dark,brown,NA,male,masculine,Naboo,Human Cordé,157,NA,brown,light,brown,NA,female,feminine,Naboo,Human Cliegg Lars,183,NA,brown,fair,blue,82,male,masculine,Tatooine,Human Poggle the Lesser,183,80,none,green,yellow,NA,male,masculine,Geonosis,Geonosian Luminara Unduli,170,56.2,black,yellow,blue,58,female,feminine,Mirial,Mirialan Barriss Offee,166,50,black,yellow,blue,40,female,feminine,Mirial,Mirialan Dormé,165,NA,brown,light,brown,NA,female,feminine,Naboo,Human Dooku,193,80,white,fair,brown,102,male,masculine,Serenno,Human Bail Prestor Organa,191,NA,black,tan,brown,67,male,masculine,Alderaan,Human Jango Fett,183,79,black,tan,brown,66,male,masculine,Concord Dawn,Human Zam Wesell,168,55,blonde,"fair, green, yellow",yellow,NA,female,feminine,Zolan,Clawdite Dexter Jettster,198,102,none,brown,yellow,NA,male,masculine,Ojom,Besalisk Lama Su,229,88,none,grey,black,NA,male,masculine,Kamino,Kaminoan Taun We,213,NA,none,grey,black,NA,female,feminine,Kamino,Kaminoan Jocasta Nu,167,NA,white,fair,blue,NA,female,feminine,Coruscant,Human Ratts Tyerell,79,15,none,"grey, blue",unknown,NA,male,masculine,Aleen Minor,Aleena R4-P17,96,NA,none,"silver, red","red
Answered Same DayNov 02, 2021

Answer To: HW 3: R Introduction For our third HW, we will take our knowledge of logic into the field of data...

Suraj answered on Nov 02 2021
120 Votes
---
title: "MGT 205 R - in class"
author: "YOUR NAME HERE"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Basic Functionality of R and Variables
## Q1. Data Typ
es
### a)
R in its most basic form can be used as a calculator. Write some coded math expressions using at least three different operators (including at least one trigonometric function).
Note: R follows the standard order of operations, but this can be controlled by wrapping any expression in ().
```{r}
#sum
2+5
#multiply
10*2
#division
100%/%2
```
### b)
Name and give an example of the five base data types in R:
```{r}
#logical
l<-as.logical(0,1)
#integer
int<-as.integer(4)
#numeric
num<-as.numeric(235.4)
#list
ls<-list(2,3,5)
#character
chr<-as.character("r","x","t")
```
### c)
Construct a vector in R.
A vector is a data structure in R that contains multiple entries of the same type.
```{r}
x<-c(1,2,5,8,7,6,9,5)
```
## Q2. Variables
Variables are a reference to data in R and can be assigned any valid data structure. Variables can be used anywhere the data that they are referencing could be and serve as the building blocks for working with data in R as they can be composed.
### a)
Create numeric variables x and y independently, then add x and y to a new variable z.
```{r}
x<-10
y<-15
z<-x+y
```
## Q3. Functions
* Built in functions
R's built in functions known as 'base R' form an incredibly powerful statistical package that can be used in a range of ways to extract information from data. Here we cover the basic usage of very common R functions.
### a)
#### i)
Create a sequence from 1 up to 10
```{r}
seq(1,10)
```
#### ii)
What is the sum of all numbers from 1 to 12345?
```{r}
sum(seq(1,12345))
```
### b)
Generate 100 normally distributed random numbers
```{r}
rnorm(100)
```
### c)
#### i)
Sample 30 random whole numbers between 1 and 500
```{r}
x<-1:500
sample(x,30)
```
#### ii)
Pick 10 of the normally distributed numbers generated in b) at random.
```{r}
sample(rnorm(100),10)
```
### d)
Simulate flipping a coin 100 times with with 1s and 0s standing...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here