From 7c7df78b38144baae6ef242870a2d15e769339da Mon Sep 17 00:00:00 2001 From: ubhetuwal Date: Tue, 15 Oct 2019 15:52:38 -0400 Subject: [PATCH 1/4] No.2 answered --- Week 06/pre-class-06.Rmd | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Week 06/pre-class-06.Rmd b/Week 06/pre-class-06.Rmd index 7b29159..a340dae 100644 --- a/Week 06/pre-class-06.Rmd +++ b/Week 06/pre-class-06.Rmd @@ -25,10 +25,12 @@ f3 <- function(x, y) { } ``` -2. Compare and contrast rnorm() and MASS::mvrnorm(). How could you make them more consistent? +## 2. Compare and contrast rnorm() and MASS::mvrnorm(). How could you make them more consistent? +rnorm() is used to generate random number for univariate normal distribution. While mvrnorm() is used to generate random number for bivariate or multivariate normal distribution.rnorm takes number,mean and sd.While mvrnorm takes n, mu, and Sigma.Sigma is calculated by finding determinant of variance and covariance of the variables. -3. Use `lapply()` and an anonymous function to find the coefficient of variation (the standard deviation divided by the mean) for all columns in the mtcars dataset. + +## 3. Use `lapply()` and an anonymous function to find the coefficient of variation (the standard deviation divided by the mean) for all columns in the mtcars dataset. 4. Use vapply() to: a. Compute the standard deviation of every column in a numeric data frame. From 8c598458988798afd5e24156c730d325c14278c2 Mon Sep 17 00:00:00 2001 From: ubhetuwal Date: Tue, 15 Oct 2019 16:41:58 -0400 Subject: [PATCH 2/4] solution to no.3 --- Week 06/pre-class-06.Rmd | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Week 06/pre-class-06.Rmd b/Week 06/pre-class-06.Rmd index a340dae..5617795 100644 --- a/Week 06/pre-class-06.Rmd +++ b/Week 06/pre-class-06.Rmd @@ -1,14 +1,4 @@ -# pre-class - - -Make sure you commit this often with meaningful messages. - - - -```{r setup, include=FALSE} -knitr::opts_chunk$set(echo = TRUE) -``` - +# pre-class-06 1. Read the source code for each of the following three functions, puzzle out what they do, and then brainstorm better names. @@ -20,6 +10,7 @@ f2 <- function(x) { if (length(x) <= 1) return(NULL) x[-length(x)] } + f3 <- function(x, y) { rep(y, length.out = length(x)) } @@ -32,6 +23,14 @@ rnorm() is used to generate random number for univariate normal distribution. Wh ## 3. Use `lapply()` and an anonymous function to find the coefficient of variation (the standard deviation divided by the mean) for all columns in the mtcars dataset. +anonym<-function(x){ + coeff_dev<- sd(x)/mean(x) + print(coeff_dev) +} +lapply(mtcars,anonym) + + + 4. Use vapply() to: a. Compute the standard deviation of every column in a numeric data frame. b. Compute the standard deviation of every numeric column in a mixed data frame. (Hint: you’ll need to use vapply() twice.) From 606b965f27caf5ad473a6cc7ee5219e6c1f67ea4 Mon Sep 17 00:00:00 2001 From: ubhetuwal Date: Tue, 15 Oct 2019 20:16:31 -0400 Subject: [PATCH 3/4] 4 answered --- Week 06/pre-class-06.Rmd | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Week 06/pre-class-06.Rmd b/Week 06/pre-class-06.Rmd index 5617795..4670d45 100644 --- a/Week 06/pre-class-06.Rmd +++ b/Week 06/pre-class-06.Rmd @@ -31,6 +31,21 @@ lapply(mtcars,anonym) -4. Use vapply() to: - a. Compute the standard deviation of every column in a numeric data frame. - b. Compute the standard deviation of every numeric column in a mixed data frame. (Hint: you’ll need to use vapply() twice.) +## 4. Use vapply() to: +## a. Compute the standard deviation of every column in a numeric data frame. + anom<-function(x){ + if(is.numeric(x)){ + sd(x) + } else { + print("Numeric dataset needed") + } +} +vapply(mtcars,anom,numeric(1)) + + ## b. Compute the standard deviation of every numeric column in a mixed data frame. (Hint: you’ll need to use vapply() twice.) + + vapply(gapminder[vapply(gapminder, is.numeric, logical(1))], anom, numeric(1)) + + + + From a516f6eb93f032c9cfc16e5afcab91b2c9322840 Mon Sep 17 00:00:00 2001 From: ubhetuwal Date: Tue, 15 Oct 2019 20:50:49 -0400 Subject: [PATCH 4/4] Answered all questions. --- Week 06/pre-class-06.Rmd | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Week 06/pre-class-06.Rmd b/Week 06/pre-class-06.Rmd index 4670d45..26be2eb 100644 --- a/Week 06/pre-class-06.Rmd +++ b/Week 06/pre-class-06.Rmd @@ -15,6 +15,11 @@ f3 <- function(x, y) { rep(y, length.out = length(x)) } ``` +For f1 if we assign any element to character vector,it checks if the if the elements matches with prefix. +For f2 The function drops the last element of vector. +For f3 The function makes vector y equal to vector x in terms of elements.If y has 3 elements and x has 4,the function adds first element of y as 4th one and makes both of them equal. + + ## 2. Compare and contrast rnorm() and MASS::mvrnorm(). How could you make them more consistent? @@ -42,7 +47,7 @@ lapply(mtcars,anonym) } vapply(mtcars,anom,numeric(1)) - ## b. Compute the standard deviation of every numeric column in a mixed data frame. (Hint: you’ll need to use vapply() twice.) + ## b. Compute the standard deviation of every numeric column in a mixed data frame. (Hint: you’ll need to use vapply() twice.) vapply(gapminder[vapply(gapminder, is.numeric, logical(1))], anom, numeric(1))