R Markdown

#1. Create three vectors of numbers. For each, print the vector, the mean, the median, the variance, and the standard deviation.

A.Vector 1 should have the mean and median equal.

B. Vector 2 should have the mean greater than the median.

C. Vector 3 should have the mean less than the median.

# Vector 1
vec1 <- c(1, 2, 3, 4, 5)
mean1 <- mean(vec1)
median1 <- median(vec1)
var1 <- var(vec1)
sd1 <- sd(vec1)

print("Vector 1:")
## [1] "Vector 1:"
print(vec1)
## [1] 1 2 3 4 5
print("Mean:")
## [1] "Mean:"
print(mean1)
## [1] 3
print("Median:")
## [1] "Median:"
print(median1)
## [1] 3
print("Variance:")
## [1] "Variance:"
print(var1)
## [1] 2.5
print("Standard Deviation:")
## [1] "Standard Deviation:"
print(sd1)
## [1] 1.581139
# Vector 2
vec2 <- c(1, 2, 3, 4, 6)
mean2 <- mean(vec2)
median2 <- median(vec2)
var2 <- var(vec2)
sd2 <- sd(vec2)

print("Vector 2:")
## [1] "Vector 2:"
print(vec2)
## [1] 1 2 3 4 6
print("Mean:")
## [1] "Mean:"
print(mean2)
## [1] 3.2
print("Median:")
## [1] "Median:"
print(median2)
## [1] 3
print("Variance:")
## [1] "Variance:"
print(var2)
## [1] 3.7
print("Standard Deviation:")
## [1] "Standard Deviation:"
print(sd2)
## [1] 1.923538
# Vector 3
vec3 <- c(1, 2, 4, 4, 6)
mean3 <- mean(vec3)
median3 <- median(vec3)
var3 <- var(vec3)
sd3 <- sd(vec3)

print("Vector 3:")
## [1] "Vector 3:"
print(vec3)
## [1] 1 2 4 4 6
print("Mean:")
## [1] "Mean:"
print(mean3)
## [1] 3.4
print("Median:")
## [1] "Median:"
print(median3)
## [1] 4
print("Variance:")
## [1] "Variance:"
print(var3)
## [1] 3.8
print("Standard Deviation:")
## [1] "Standard Deviation:"
print(sd3)
## [1] 1.949359

#2.Use R to solve each of the following problems.

A. The head of quality control stops by a factory to inspect the next 20 widgets produced. Over the past six months, 98% of widgets have passed inspection. What is the probability that less than 10 widgets pass inspection?

B.The average test score in a large college statistics course with multiple sections is 85 and the standard deviation is five points. If you choose a student at random, what is the probability that their score is between 80 and 85?

C. Over the past three years at the farm you manage, the watermelons average about nine pounds each with a standard deviation of two pounds. What is the probability that a watermelon randomly selected from your harvest will weigh more than 10 pounds?

D. You manage a help desk. 90% of your tickets are resolved with the first reply. If you randomly select 10 tickets for review, what is the probability that all 10 were resolved with the first reply?

# Problem A
test_size <- 20
test_prob <- 0.98

pbinom(10, size = test_size, prob = test_prob)
## [1] 1.574945e-12
# Problem B
mean <- 85
sd <- 5
z <- (80-mean)/sd
Probabilty <- pnorm(abs(z))
print(Probabilty)
## [1] 0.8413447
# Problem C
mean <- 9
sd <- 2
prob3 <- pnorm(10, mean = mean, sd = sd, lower.tail = FALSE)
print(prob3)
## [1] 0.3085375
# Problem D
test_size <- 10
test_prob <- 0.90

dbinom(10 , size = test_size , prob = test_prob)
## [1] 0.3486784

3. From the MASS package, import the dataset “immer.”

A. Read the documentation and describe the dataset in your own words.

B. Is this data tidy? If not, tidy it and print the tidy dataset. Save your tidy dataset in a variable and work with it through the rest of the problem set.

C. Use any tools that you like in R to answer the following questions.

#What values are present in the location variable? How many rows are there for each value? #What values are present in the variety variable? How many rows are there for each value? #How many years are represented in the dataset? How many rows are there for each year? #What is the mean yield? #What is the first quartile, median, and third quartile for yield? #What are the minimum and maximum values for yield? #What are the variance and standard deviation for yield? #Create two boxplots for yield. #The first boxplot should have bars that extend from the minimum to the maximum. #The second boxplot should have bars that extend from (Q1 - 1.5 x IQR) to (Q3 + 1.5 x IQR)

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6      ✔ purrr   1.0.1 
## ✔ tibble  3.1.7      ✔ dplyr   1.0.10
## ✔ tidyr   1.3.0      ✔ stringr 1.5.0 
## ✔ readr   2.1.2      ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
data(immer)

#A #The “immer” dataset is a data set from a study on the effects of a fertilizer treatment on the yield of a barley variety called “Immer” grown in four different locations. The data consists of a randomized block design with three replicates per location. The treatment is a factor with two levels: control and nitro. The response variable is the yield of barley in bushels per acre. It includes four factors, location, replicate, nitro and block and a response variable yield.

#B #This data is not in tidy format, as the location, replicate, nitro and block variables are spread across multiple columns. To make this data tidy, we can use the “gather” function from the tidyr package to gather these variables into a single column called “variable” and the corresponding values into a column called “value”.

#Tidying up the data

#Renaming collums 
names(immer)[3] <- 1931
names(immer)[4] <- 1932

# Load tidyverse package to use pivot
library(tidyverse)

# Pivot immer df from wide to longer and assign tidy df to newImmer
newImmer <- pivot_longer(
              data = immer,
              cols = colnames(immer[3:4]),
              names_to = "Year",
              values_to = "Yield"
            )

print(newImmer)
## # A tibble: 60 × 4
##    Loc   Var   Year  Yield
##    <fct> <fct> <chr> <dbl>
##  1 UF    M     1931   81  
##  2 UF    M     1932   80.7
##  3 UF    S     1931  105. 
##  4 UF    S     1932   82.3
##  5 UF    V     1931  120. 
##  6 UF    V     1932   80.4
##  7 UF    T     1931  110. 
##  8 UF    T     1932   87.2
##  9 UF    P     1931   98.3
## 10 UF    P     1932   84.2
## # … with 50 more rows

#C

unique(newImmer)
## # A tibble: 60 × 4
##    Loc   Var   Year  Yield
##    <fct> <fct> <chr> <dbl>
##  1 UF    M     1931   81  
##  2 UF    M     1932   80.7
##  3 UF    S     1931  105. 
##  4 UF    S     1932   82.3
##  5 UF    V     1931  120. 
##  6 UF    V     1932   80.4
##  7 UF    T     1931  110. 
##  8 UF    T     1932   87.2
##  9 UF    P     1931   98.3
## 10 UF    P     1932   84.2
## # … with 50 more rows

Location valuables: UF, W, M, C, GR, and D. Data set presents

10 rows for these values.

#Variety variables: M, S, V, T, and P. Data set presents 12 rows #for these values.

# Statistical summary of the newImmer df 
summary(newImmer)
##  Loc     Var        Year               Yield       
##  C :10   M:12   Length:60          Min.   : 49.90  
##  D :10   P:12   Class :character   1st Qu.: 80.62  
##  GR:10   S:12   Mode  :character   Median : 97.50  
##  M :10   T:12                      Mean   :101.09  
##  UF:10   V:12                      3rd Qu.:119.72  
##  W :10                             Max.   :191.50

#Mean yield - 101.09 #First quartile for yield - 80.62 #Median for yield - 97.50 #Third quartile for yield - 119.72 #Minimum value for yield - 49.90 #Maximum value for yield - 191.50

var(newImmer$Yield)
## [1] 758.1755
sd(newImmer$Yield)
## [1] 27.53499

#Variance for yield - 758.18 #Sd for yield - 27.53

boxplot(newImmer$Yield,  
        main = "Barley Yield",
        horizontal  =  TRUE,  
        range  =  0) 

#Boxplot #1

boxplot(newImmer$Yield,  
        main = "Barley Yield (Q1 - 1.5 x IQR) to (Q3 + 1.5 x IQR)",
        horizontal  =  TRUE,  
        range  =  1.5) 

## Boxplot #2

#4 From the MASS package, import the dataset “Pima.tr.” #Read the documentation and describe the data in your own words. #Is the data tidy? If not, tidy it. #Write a loop that calculates and prints the mean of each numeric variable.

?Pima.tr

pimadataset <- Pima.tr

pimadataset <- as_tibble(pimadataset)
(pimadataset)
## # A tibble: 200 × 8
##    npreg   glu    bp  skin   bmi   ped   age type 
##    <int> <int> <int> <int> <dbl> <dbl> <int> <fct>
##  1     5    86    68    28  30.2 0.364    24 No   
##  2     7   195    70    33  25.1 0.163    55 Yes  
##  3     5    77    82    41  35.8 0.156    35 No   
##  4     0   165    76    43  47.9 0.259    26 No   
##  5     0   107    60    25  26.4 0.133    23 No   
##  6     5    97    76    27  35.6 0.378    52 Yes  
##  7     3    83    58    31  34.3 0.336    25 No   
##  8     1   193    50    16  25.9 0.655    24 No   
##  9     3   142    80    15  32.4 0.2      63 No   
## 10     2   128    78    37  43.3 1.22     31 Yes  
## # … with 190 more rows
data(Pima.tr)
unique(Pima.tr)
##     npreg glu  bp skin  bmi   ped age type
## 1       5  86  68   28 30.2 0.364  24   No
## 2       7 195  70   33 25.1 0.163  55  Yes
## 3       5  77  82   41 35.8 0.156  35   No
## 4       0 165  76   43 47.9 0.259  26   No
## 5       0 107  60   25 26.4 0.133  23   No
## 6       5  97  76   27 35.6 0.378  52  Yes
## 7       3  83  58   31 34.3 0.336  25   No
## 8       1 193  50   16 25.9 0.655  24   No
## 9       3 142  80   15 32.4 0.200  63   No
## 10      2 128  78   37 43.3 1.224  31  Yes
## 11      0 137  40   35 43.1 2.288  33  Yes
## 12      9 154  78   30 30.9 0.164  45   No
## 13      1 189  60   23 30.1 0.398  59  Yes
## 14     12  92  62    7 27.6 0.926  44  Yes
## 15      1  86  66   52 41.3 0.917  29   No
## 16      4  99  76   15 23.2 0.223  21   No
## 17      1 109  60    8 25.4 0.947  21   No
## 18     11 143  94   33 36.6 0.254  51  Yes
## 19      1 149  68   29 29.3 0.349  42  Yes
## 20      0 139  62   17 22.1 0.207  21   No
## 21      2  99  70   16 20.4 0.235  27   No
## 22      1 100  66   29 32.0 0.444  42   No
## 23      4  83  86   19 29.3 0.317  34   No
## 24      0 101  64   17 21.0 0.252  21   No
## 25      1  87  68   34 37.6 0.401  24   No
## 26      9 164  84   21 30.8 0.831  32  Yes
## 27      1  99  58   10 25.4 0.551  21   No
## 28      0 140  65   26 42.6 0.431  24  Yes
## 29      5 108  72   43 36.1 0.263  33   No
## 30      2 110  74   29 32.4 0.698  27   No
## 31      1  79  60   42 43.5 0.678  23   No
## 32      3 148  66   25 32.5 0.256  22   No
## 33      0 121  66   30 34.3 0.203  33  Yes
## 34      3 158  64   13 31.2 0.295  24   No
## 35      2 105  80   45 33.7 0.711  29  Yes
## 36     13 145  82   19 22.2 0.245  57   No
## 37      1  79  80   25 25.4 0.583  22   No
## 38      1  71  48   18 20.4 0.323  22   No
## 39      0 102  86   17 29.3 0.695  27   No
## 40      0 119  66   27 38.8 0.259  22   No
## 41      8 176  90   34 33.7 0.467  58  Yes
## 42      1  97  68   21 27.2 1.095  22   No
## 43      4 129  60   12 27.5 0.527  31   No
## 44      1  97  64   19 18.2 0.299  21   No
## 45      0  86  68   32 35.8 0.238  25   No
## 46      2 125  60   20 33.8 0.088  31   No
## 47      5 123  74   40 34.1 0.269  28   No
## 48      2  92  76   20 24.2 1.698  28   No
## 49      3 171  72   33 33.3 0.199  24  Yes
## 50      1 199  76   43 42.9 1.394  22  Yes
## 51      3 116  74   15 26.3 0.107  24   No
## 52      2  83  66   23 32.2 0.497  22   No
## 53      8 154  78   32 32.4 0.443  45  Yes
## 54      1 114  66   36 38.1 0.289  21   No
## 55      1 106  70   28 34.2 0.142  22   No
## 56      4 127  88   11 34.5 0.598  28   No
## 57      1 124  74   36 27.8 0.100  30   No
## 58      1 109  38   18 23.1 0.407  26   No
## 59      2 123  48   32 42.1 0.520  26   No
## 60      8 167 106   46 37.6 0.165  43  Yes
## 61      7 184  84   33 35.5 0.355  41  Yes
## 62      1  96  64   27 33.2 0.289  21   No
## 63     10 129  76   28 35.9 0.280  39   No
## 64      6  92  62   32 32.0 0.085  46   No
## 65      6 109  60   27 25.0 0.206  27   No
## 66      5 139  80   35 31.6 0.361  25  Yes
## 67      6 134  70   23 35.4 0.542  29  Yes
## 68      3 106  54   21 30.9 0.292  24   No
## 69      0 131  66   40 34.3 0.196  22  Yes
## 70      0 135  94   46 40.6 0.284  26   No
## 71      5 158  84   41 39.4 0.395  29  Yes
## 72      3 112  74   30 31.6 0.197  25  Yes
## 73      8 181  68   36 30.1 0.615  60  Yes
## 74      2 121  70   32 39.1 0.886  23   No
## 75      1 168  88   29 35.0 0.905  52  Yes
## 76      1 144  82   46 46.1 0.335  46  Yes
## 77      2 101  58   17 24.2 0.614  23   No
## 78      2  96  68   13 21.1 0.647  26   No
## 79      3 107  62   13 22.9 0.678  23  Yes
## 80     12 121  78   17 26.5 0.259  62   No
## 81      2 100  64   23 29.7 0.368  21   No
## 82      4 154  72   29 31.3 0.338  37   No
## 83      6 125  78   31 27.6 0.565  49  Yes
## 84     10 125  70   26 31.1 0.205  41  Yes
## 85      2 122  76   27 35.9 0.483  26   No
## 86      2 114  68   22 28.7 0.092  25   No
## 87      1 115  70   30 34.6 0.529  32  Yes
## 88      7 114  76   17 23.8 0.466  31   No
## 89      2 115  64   22 30.8 0.421  21   No
## 90      1 130  60   23 28.6 0.692  21   No
## 91      1  79  75   30 32.0 0.396  22   No
## 92      4 112  78   40 39.4 0.236  38   No
## 93      7 150  78   29 35.2 0.692  54  Yes
## 94      1  91  54   25 25.2 0.234  23   No
## 95      1 100  72   12 25.3 0.658  28   No
## 96     12 140  82   43 39.2 0.528  58  Yes
## 97      4 110  76   20 28.4 0.118  27   No
## 98      2  94  76   18 31.6 0.649  23   No
## 99      2  84  50   23 30.4 0.968  21   No
## 100    10 148  84   48 37.6 1.001  51  Yes
## 101     3  61  82   28 34.4 0.243  46   No
## 102     4 117  62   12 29.7 0.380  30  Yes
## 103     3  99  80   11 19.3 0.284  30   No
## 104     3  80  82   31 34.2 1.292  27  Yes
## 105     4 154  62   31 32.8 0.237  23   No
## 106     6 103  72   32 37.7 0.324  55   No
## 107     6 111  64   39 34.2 0.260  24   No
## 108     0 124  70   20 27.4 0.254  36  Yes
## 109     1 143  74   22 26.2 0.256  21   No
## 110     1  81  74   41 46.3 1.096  32   No
## 111     4 189 110   31 28.5 0.680  37   No
## 112     4 116  72   12 22.1 0.463  37   No
## 113     7 103  66   32 39.1 0.344  31  Yes
## 114     8 124  76   24 28.7 0.687  52  Yes
## 115     1  71  78   50 33.2 0.422  21   No
## 116     0 137  84   27 27.3 0.231  59   No
## 117     9 112  82   32 34.2 0.260  36  Yes
## 118     4 148  60   27 30.9 0.150  29  Yes
## 119     1 136  74   50 37.4 0.399  24   No
## 120     9 145  80   46 37.9 0.637  40  Yes
## 121     1  93  56   11 22.5 0.417  22   No
## 122     1 107  72   30 30.8 0.821  24   No
## 123    12 151  70   40 41.8 0.742  38  Yes
## 124     1  97  70   40 38.1 0.218  30   No
## 125     5 144  82   26 32.0 0.452  58  Yes
## 126     2 112  86   42 38.4 0.246  28   No
## 127     2  99  52   15 24.6 0.637  21   No
## 128     1 109  56   21 25.2 0.833  23   No
## 129     1 120  80   48 38.9 1.162  41   No
## 130     7 187  68   39 37.7 0.254  41  Yes
## 131     3 129  92   49 36.4 0.968  32  Yes
## 132     7 179  95   31 34.2 0.164  60   No
## 133     6  80  66   30 26.2 0.313  41   No
## 134     2 105  58   40 34.9 0.225  25   No
## 135     3 191  68   15 30.9 0.299  34   No
## 136     0  95  80   45 36.5 0.330  26   No
## 137     4  99  72   17 25.6 0.294  28   No
## 138     0 137  68   14 24.8 0.143  21   No
## 139     1  97  70   15 18.2 0.147  21   No
## 140     0 100  88   60 46.8 0.962  31   No
## 141     1 167  74   17 23.4 0.447  33  Yes
## 142     0 180  90   26 36.5 0.314  35  Yes
## 143     2 122  70   27 36.8 0.340  27   No
## 144     1  90  62   12 27.2 0.580  24   No
## 145     3 120  70   30 42.9 0.452  30   No
## 146     6 154  78   41 46.1 0.571  27   No
## 147     2  56  56   28 24.2 0.332  22   No
## 148     0 177  60   29 34.6 1.072  21  Yes
## 149     3 124  80   33 33.2 0.305  26   No
## 150     8  85  55   20 24.4 0.136  42   No
## 151    12  88  74   40 35.3 0.378  48   No
## 152     9 152  78   34 34.2 0.893  33  Yes
## 153     0 198  66   32 41.3 0.502  28  Yes
## 154     0 188  82   14 32.0 0.682  22  Yes
## 155     5 139  64   35 28.6 0.411  26   No
## 156     7 168  88   42 38.2 0.787  40  Yes
## 157     2 197  70   99 34.7 0.575  62  Yes
## 158     2 142  82   18 24.7 0.761  21   No
## 159     8 126  74   38 25.9 0.162  39   No
## 160     3 158  76   36 31.6 0.851  28  Yes
## 161     3 130  78   23 28.4 0.323  34  Yes
## 162     2 100  54   28 37.8 0.498  24   No
## 163     1 164  82   43 32.8 0.341  50   No
## 164     4  95  60   32 35.4 0.284  28   No
## 165     2 122  52   43 36.2 0.816  28   No
## 166     4  85  58   22 27.8 0.306  28   No
## 167     0 151  90   46 42.1 0.371  21  Yes
## 168     6 144  72   27 33.9 0.255  40   No
## 169     3 111  90   12 28.4 0.495  29   No
## 170     1 107  68   19 26.5 0.165  24   No
## 171     6 115  60   39 33.7 0.245  40  Yes
## 172     5 105  72   29 36.9 0.159  28   No
## 173     7 194  68   28 35.9 0.745  41  Yes
## 174     4 184  78   39 37.0 0.264  31  Yes
## 175     0  95  85   25 37.4 0.247  24  Yes
## 176     7 124  70   33 25.5 0.161  37   No
## 177     1 111  62   13 24.0 0.138  23   No
## 178     7 137  90   41 32.0 0.391  39   No
## 179     9  57  80   37 32.8 0.096  41   No
## 180     2 157  74   35 39.4 0.134  30   No
## 181     2  95  54   14 26.1 0.748  22   No
## 182    12 140  85   33 37.4 0.244  41   No
## 183     0 117  66   31 30.8 0.493  22   No
## 184     8 100  74   40 39.4 0.661  43  Yes
## 185     9 123  70   44 33.1 0.374  40   No
## 186     0 138  60   35 34.6 0.534  21  Yes
## 187    14 100  78   25 36.6 0.412  46  Yes
## 188    14 175  62   30 33.6 0.212  38  Yes
## 189     0  74  52   10 27.8 0.269  22   No
## 190     1 133 102   28 32.8 0.234  45  Yes
## 191     0 119  64   18 34.9 0.725  23   No
## 192     5 155  84   44 38.7 0.619  34   No
## 193     1 128  48   45 40.5 0.613  24  Yes
## 194     2 112  68   22 34.1 0.315  26   No
## 195     1 140  74   26 24.1 0.828  23   No
## 196     2 141  58   34 25.4 0.699  24   No
## 197     7 129  68   49 38.5 0.439  43  Yes
## 198     0 106  70   37 39.4 0.605  22   No
## 199     1 118  58   36 33.3 0.261  23   No
## 200     8 155  62   26 34.0 0.543  46  Yes
names(Pima.tr)
## [1] "npreg" "glu"   "bp"    "skin"  "bmi"   "ped"   "age"   "type"
duplicated(Pima.tr)
##   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [157] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [169] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [181] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [193] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
glimpse(Pima.tr)
## Rows: 200
## Columns: 8
## $ npreg <int> 5, 7, 5, 0, 0, 5, 3, 1, 3, 2, 0, 9, 1, 12, 1, 4, 1, 11, 1, 0, 2,…
## $ glu   <int> 86, 195, 77, 165, 107, 97, 83, 193, 142, 128, 137, 154, 189, 92,…
## $ bp    <int> 68, 70, 82, 76, 60, 76, 58, 50, 80, 78, 40, 78, 60, 62, 66, 76, …
## $ skin  <int> 28, 33, 41, 43, 25, 27, 31, 16, 15, 37, 35, 30, 23, 7, 52, 15, 8…
## $ bmi   <dbl> 30.2, 25.1, 35.8, 47.9, 26.4, 35.6, 34.3, 25.9, 32.4, 43.3, 43.1…
## $ ped   <dbl> 0.364, 0.163, 0.156, 0.259, 0.133, 0.378, 0.336, 0.655, 0.200, 1…
## $ age   <int> 24, 55, 35, 26, 23, 52, 25, 24, 63, 31, 33, 45, 59, 44, 29, 21, …
## $ type  <fct> No, Yes, No, No, No, Yes, No, No, No, Yes, Yes, No, Yes, Yes, No…
pimadataset[pimadataset$npreg == 1 & pimadataset$age == 22,]
## # A tibble: 7 × 8
##   npreg   glu    bp  skin   bmi   ped   age type 
##   <int> <int> <int> <int> <dbl> <dbl> <int> <fct>
## 1     1    79    80    25  25.4 0.583    22 No   
## 2     1    71    48    18  20.4 0.323    22 No   
## 3     1    97    68    21  27.2 1.10     22 No   
## 4     1   199    76    43  42.9 1.39     22 Yes  
## 5     1   106    70    28  34.2 0.142    22 No   
## 6     1    79    75    30  32   0.396    22 No   
## 7     1    93    56    11  22.5 0.417    22 No

#A The data consists of several medical predictor variables such as the number of pregnancies the patient has had, their BMI, insulin level, age and so on. The response variable is a binary variable indicating whether the patient has diabetes or not.

#The Pima.tr data set is in a tidy format as the columns are representing variables and the rows are representing observations.

#Write a loop that calculates and prints the mean of each numeric variable

for (i in c(2:5)) {
  print(mean(pimadataset[[i]]))
}
## [1] 123.97
## [1] 71.26
## [1] 29.215
## [1] 32.31