-
Notifications
You must be signed in to change notification settings - Fork 1
/
Homework 4.Rmd
104 lines (96 loc) · 3.82 KB
/
Homework 4.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
---
title: "Homework 4"
author: "Aritra Samanta, Jorge Arauz Parada"
date: "2/5/2022"
output:
word_document: default
html_document: default
---
```{r}
library(tidyverse)
library(dplyr)
library(purrr)
```
1)
a) Use the R function nrow to confirm that the iris data frame has 150 rows. Then use and show R code that
features a map function to confirm that the iris data frame has 150 rows.
```{r}
nrow(iris)
map_dbl(iris, function(x) length((x))) #each row has 150 observations.
```
b) Each column of the iris data frame has a unique number of values or objects. For example, the column
Sepal.Length has 150 values but 35 of them are unique. Use and show R code that features a map function to
find the number of unique values or objects for each column of the iris data frame
```{r}
map_dbl(iris, function(x) length(unique(x)))
```
2) Use and show R code that features a nested loop that will produce the 5 by 3 matrix shown below.
```{r}
z <- matrix( nrow = 3, ncol = 5)
for (m in 1:3) {
for (n in 0:5) {
z[m, n] <- n-m
}
}
print(z)
```
3)Use and show R code that will produce a tibble that features 10 randomly generated values that are normally
distributed, with means of -10, 0, 10 and 100 respectfully. Run your code again, producing a second tibble,
that confirms random values, hence the second table will not have the same values.
```{r}
tibble(
mean10n = rnorm(10, -10),
mean0 = rnorm(10,0),
mean10p = rnorm(10,10),
mean100 = rnorm(10,100)
)
```
```{r}
tibble(
mean10n = rnorm(10, -10),
mean0 = rnorm(10,0),
mean10p = rnorm(10,10),
mean100 = rnorm(10,100)
)
#running again to show randomness
#assigning this to a variable will save the numbers also,losing the randomness.
```
```{r}
X <- list(12, 14, 15, 18, 19, 22,10,18,18)
Mean <- list(16, 16, 16, 16, 16,16,16,16,16)
sd <- list(2, 2, 2, 2, 2,2,2,2,2)
```
4a) In statistics, a z score indicates the standard deviation distance between the mean and a specific value of
the data set. What formula is used to find a z score? Use and show R coding that features a map function to
iteratively find z scores across the lists given above.
```{r}
pmap_dbl(list(X,Mean,sd), function(first, second, third) (first-second)/third)
```
b) The test statistic for a population mean is given by the formula ((X - mean)/s/sqrt(n)) Use and show R coding
that features a map function to iteratively find test statistics for population means across the lists given above.
```{r}
pmap_dbl(list(X,Mean,sd), function(first, second, third) (first-second)/(third/sqrt(9)))
```
```{r}
V = c(10,15,17,22,32,38,42)
```
5a) Another purr package function is the keep( ) function. Research, explore, and use the keep( ) function to
extract all number from the vector V given above that are less than 20
```{r}
V%>%
keep(V <20)
```
b)Another purr package function is the discard( ) function. Research, explore, and use the discard( ) function
to eliminate all numbers from the vector V given above that are less than 20
```{r}
V%>%
discard(V <20)
```
6)Another purr package function is the safely( ) function. Research, explore, and apply the safely( ) function to the given vector below as illustrated.
```{r}
U = list(10,15,"mary",22,32,"james",42)
map(U, safely(~ .x + 15))
```
In four or five sentences, explain the specific output for this problem and how the definition and the
application of the safely( ) function is used.
In the output for this problem,each item in the list is added to 15, and then the errors for every item are also shown if applicable. For exampls, you cannot add james by 15, therefore the result is shown as null and the error tells us about the non numeric argument to binary operator. The safely function allows us to run code where the errors will show in the output instead of as a side message where the code will not run. ie. we can still evaluate the code as we know there are errors in it.