0% found this document useful (0 votes)
2 views6 pages

Mod1Homework2 5

The document describes a tutorial session in the R programming environment using the swirl package, focusing on handling missing values (NA) in data analysis. It guides the user through various exercises, including creating vectors with NA values, identifying their locations, and counting them. Additionally, it introduces the concept of NaN (not a number) and demonstrates operations that result in NaN, such as dividing zero by zero.

Uploaded by

salmanwnl4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Mod1Homework2 5

The document describes a tutorial session in the R programming environment using the swirl package, focusing on handling missing values (NA) in data analysis. It guides the user through various exercises, including creating vectors with NA values, identifying their locations, and counting them. Additionally, it introduces the concept of NaN (not a number) and demonstrates operations that result in NaN, such as dividing zero by zero.

Uploaded by

salmanwnl4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

> library(swirl)

> swirl()

| Welcome to swirl! Please sign in. If you've been here before, use
| the same name as you did then. If you are new, call yourself
| something unique.

What shall I call you? Sunny

| Please choose a course, or type 0 to exit swirl.

1: R Programming
2: Take me to the swirl course repository!

Selection: 1

| Please choose a lesson, or type 0 to return to course menu.

1: Basic Building Blocks 2: Workspace and Files


3: Sequences of Numbers 4: Vectors
5: Missing Values 6: Subsetting Vectors
7: Matrices and Data Frames 8: Logic
9: Functions 10: lapply and sapply
11: vapply and tapply 12: Looking at Data
13: Simulation 14: Dates and Times
15: Base Graphics

Selection: 5

| | 0%

| Missing values play an important role in statistics and data


| analysis. Often, missing values must not be ignored, but rather
| they should be carefully studied to see if there's an underlying
| pattern or cause for their missingness.

...

|=== | 5%
| In R, NA is used to represent any value that is 'not available'
| or 'missing' (in the statistical sense). In this lesson, we'll
| explore missing values further.

...

|====== | 10%
| Any operation involving NA generally yields NA as the result. To
| illustrate, let's create a vector c(44, NA, 5, NA) and assign it
| to a variable x.

> x<-c(44, NA, 5, NA)

| Excellent work!

|========= | 15%
| Now, let's multiply x by 3.

> x*3
[1] 132 NA 15 NA

| All that hard work is paying off!

|============ | 20%
| Notice that the elements of the resulting vector that correspond
| with the NA values in x are also NA.

...

|============== | 25%
| To make things a little more interesting, lets create a vector
| containing 1000 draws from a standard normal distribution with y
| <- rnorm(1000).

> y<- rnorm(1000)

| That's correct!

|================= | 30%
| Next, let's create a vector containing 1000 NAs with z <- rep(NA,
| 1000).

> z<- rep(NA, 1000)

| Keep up the great work!

|==================== | 35%
| Finally, let's select 100 elements at random from these 2000
| values (combining y and z) such that we don't know how many NAs
| we'll wind up with or what positions they'll occupy in our final
| vector -- my_data <- sample(c(y, z), 100).
my_na <- [Link](my_data)
> my_data<- sample(c(y, z), 100)

| Keep working like that and you'll get there!

|======================= | 40%
| Let's first ask the question of where our NAs are located in our
| data. The [Link]() function tells us whether each element of a
| vector is NA. Call [Link]() on my_data and assign the result to
| my_na.

> my_na<- [Link](my_data)

| Your dedication is inspiring!

|========================== | 45%
| Now, print my_na to see what you came up with.
> my_na
[1] TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE
[11] FALSE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
[21] TRUE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE
[31] FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE
[41] FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE
[51] FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE
[61] FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE
[71] TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
[81] FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
[91] FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE

| Great job!

|============================= | 50%
| Everywhere you see a TRUE, you know the corresponding element of
| my_data is NA. Likewise, everywhere you see a FALSE, you know the
| corresponding element of my_data is one of our random draws from
| the standard normal distribution.

...

|================================ | 55%
| In our previous discussion of logical operators, we introduced
| the `==` operator as a method of testing for equality between two
| objects. So, you might think the expression my_data == NA yields
| the same results as [Link](). Give it a try.

> my_data == NA
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[22] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[43] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[64] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[85] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

| Excellent job!

|=================================== | 60%
| The reason you got a vector of all NAs is that NA is not really a
| value, but just a placeholder for a quantity that is not
| available. Therefore the logical expression is incomplete and R
| has no choice but to return a vector of the same length as
| my_data that contains all NAs.

...

|====================================== | 65%
| Don't worry if that's a little confusing. The key takeaway is to
| be cautious when using logical expressions anytime NAs might
| creep in, since a single NA value can derail the entire thing.

...

|========================================= | 70%
| So, back to the task at hand. Now that we have a vector, my_na,
| that has a TRUE for every NA and FALSE for every numeric value,
| we can compute the total number of NAs in our data.

...

|============================================ | 75%
| The trick is to recognize that underneath the surface, R
| represents TRUE as the number 1 and FALSE as the number 0.
| Therefore, if we take the sum of a bunch of TRUEs and FALSEs, we
| get the total number of TRUEs.

...

|============================================== | 80%
| Let's give that a try here. Call the sum() function on my_na to
| count the total number of TRUEs in my_na, and thus the total
| number of NAs in my_data. Don't assign the result to a new
| variable.

> sum(my_na)
[1] 56

| Keep working like that and you'll get there!

|================================================= |
85%
| Pretty cool, huh? Finally, let's take a look at the data to
| convince ourselves that everything 'adds up'. Print my_data to
| the console.

> my_data
[1] NA 0.09991003 NA NA NA
[6] 0.54815264 NA NA NA NA
[11] -0.38364278 NA NA NA 1.14510638
[16] 1.49418580 NA -1.37078358 0.11341008 -0.83151224
[21] NA NA 0.92943695 NA NA
[26] -1.05816550 -0.85658415 NA -0.67952730 NA
[31] 1.17329727 NA NA NA NA
[36] -1.82209303 0.46309931 NA NA -0.24250720
[41] -0.24489511 NA NA NA NA
[46] NA NA 0.24755878 0.66450031 NA
[51] 0.87713660 -1.47830243 1.82390411 -1.17932704 NA
[56] -0.34706573 NA NA -0.44802933 NA
[61] 0.16592066 -1.04873611 0.05222233 NA 2.27602617
[66] NA -1.26510863 0.41095603 NA NA
[71] NA NA NA -1.20239039 NA
[76] -0.97398231 NA 1.30119522 NA -0.60525027
[81] 0.53337932 NA -0.51724463 NA NA
[86] NA NA NA NA -0.61905373
[91] -0.24973837 -0.05094109 NA NA -0.15984119
[96] -1.59239704 NA 0.70853522 NA 0.21023885

| Your dedication is inspiring!


|==================================================== |
90%
| Now that we've got NAs down pat, let's look at a second type of
| missing value -- NaN, which stands for 'not a number'. To
| generate NaN, try dividing (using a forward slash) 0 by 0 now.

> my_data
[1] NA 0.09991003 NA NA NA
[6] 0.54815264 NA NA NA NA
[11] -0.38364278 NA NA NA 1.14510638
[16] 1.49418580 NA -1.37078358 0.11341008 -0.83151224
[21] NA NA 0.92943695 NA NA
[26] -1.05816550 -0.85658415 NA -0.67952730 NA
[31] 1.17329727 NA NA NA NA
[36] -1.82209303 0.46309931 NA NA -0.24250720
[41] -0.24489511 NA NA NA NA
[46] NA NA 0.24755878 0.66450031 NA
[51] 0.87713660 -1.47830243 1.82390411 -1.17932704 NA
[56] -0.34706573 NA NA -0.44802933 NA
[61] 0.16592066 -1.04873611 0.05222233 NA 2.27602617
[66] NA -1.26510863 0.41095603 NA NA
[71] NA NA NA -1.20239039 NA
[76] -0.97398231 NA 1.30119522 NA -0.60525027
[81] 0.53337932 NA -0.51724463 NA NA
[86] NA NA NA NA -0.61905373
[91] -0.24973837 -0.05094109 NA NA -0.15984119
[96] -1.59239704 NA 0.70853522 NA 0.21023885

| That's not exactly what I'm looking for. Try again. Or, type
| info() for more options.

| Try 0/0.

>0/0
[1] NaN

| You are quite good my friend!

|
=======================================================
| 95%
| Let's do one more, just for fun. In R, Inf stands for infinity.
| What happens if you subtract Inf from Inf?

>0/0
[1] NaN

| You almost had it, but not quite. Try again. Or, type info() for
| more options.

| Type Inf - Inf. Can you guess the result?

> Inf - Inf


[1] NaN

| That's correct!

|
=======================================================
===| 100%

You might also like