Assignment 1 (10%)

Amy Zhang Course Section: CIND 123, LEC 5752 Student #: 501344918

This assignment can be submitted using either Python or R, whichever you prefer. If using R, you must submit an RMD file with its knitted file (PDF or HTML). To learn more about knitting and R markdown, visit R Markdown.

If using Python, you must submit an IPYNB file and its exported PDF/HTML with clearly printed/shown answers.

Failing to submit both files ({RMD + knitted PDF/HTML} OR {IPYNB + PDF/HTML}) will be subject to a 30% mark deduction.

NOTE: IF YOU USE R STUDIO , YOU SHOULD NEVER HAVE install.packages IN YOUR CODE; OTHERWISE, THE Knit OPTION WILL RAISE AN ERROR. COMMENT OUT ALL PACKAGE INSTALLATIONS BUT KEEP library() CALLS.

NOTE: If you answer the questions in R, all your answers should be in R (ignore Python questions). If you answer the questions in Python, all your answers

Question 1 (40 Points)

Q. 1a (5 points)

Create a vector of all odd numbers from 5 to 50 Hint: Use seq() in R or range() in python

odd_seq <- seq(5,50, 2)
odd_seq
##  [1]  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

Q. 1b (5 points)

Create and print a vector x with all integers 1-100, and a vector y with even integer in the same range. Hint: use seq()function in R or range()in python. Calculate the difference in lengths of the vectors x and y. Hint: use length() in R or len() in python

x <- seq(1, 100, 2)
x
##  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
## [26] 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
y <- seq (2, 100, 2)
y
##  [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38
## [20]  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76
## [39]  78  80  82  84  86  88  90  92  94  96  98 100
length(x) - length(y)
## [1] 0

Q. 1c (10 points)

Create a new vector, “x_square”, with the square of elements at indices 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 from the variable “x”. (Hint: Use indexing rather than a for loop.)

Calculate the mean and median of the LAST 6 values from x_square.

odd_seq <- seq(5,50, 2)
x_square <- odd_seq <- x[odd_seq]
mean(x_square)
## [1] 53
median(x_square)
## [1] 53

Q. 1d (10 Points)

For a given factor variable factorVar <- factor(c(“2.5”, “7.1”, “6.3”, “4.8”)) in R or factorVar = [“2.5”, “7.1”, “6.3”, “4.8”] in python Please convert it into numeric data type and show proof that it is converted to numeric.

factorVar <- factor(c("2.5", "7.1", "6.3", "4.8"))
new_factorVar <- as.numeric(factorVar)
is.numeric(new_factorVar)
## [1] TRUE

Q. 1e (10 points)

A comma-separated values file dataset.csv consists of missing values represented by Not a Number (NaN) and question mark (?). How can you read this type of files in R?

setwd("C:/Users/16137/Desktop/cind123")
data <- read.csv("dataset.csv")
head(data)
##   X1 X2 X3 X4 X5 X6 X7   X8 X9 X10
## 1 11 12 13 14 15 16 17   18 19  20
## 2 21 22 23 24 25 26 27   28 29  30
## 3 31 32 33 34 35 36 37   38 39  40
## 4 41 42 43 44 45  ? 47   48 49  50
## 5 51 52 53  ? 55 56 57 null 59  60
## 6 61 62 63 64 65 66 67   68 69  70

Question 2 (60 Points)

Compute with using R or Python commands

Q. 2a (10 points)

\[\sum_{n=1}^{200}\frac{3^{n}}{(n+1)!}\] Hint: Use factorial(n) to compute \(n!\)

numbers <- 1:200
numerator <- 3^numbers
denominator <- factorial(numbers+1)
result <- sum(numerator/denominator)
result
## [1] 5.361846

Q. 2b (10 points)

\[\sum_{n=1}^{25}\left(\frac{2^{n}}{n^2} + \frac{n^{4}}{4^{n}}\right)\]

numbers <- 1:25
answer <- sum((2^numbers)/(numbers^2) + ((numbers^4)/(4^numbers)))
answer
## [1] 118044.3

Q. 2c (10 points)

\[\sum_{n=0}^{100} \frac{(n+1)5^n}{15^{n+1}}\]

numbers <- 0:100
answer <- sum(((numbers+1)*5^numbers)/15^(numbers+1))
answer
## [1] 0.15

Q. 2d (10 points)

\[\prod_{n=2}^{22} \left(3n + \frac{3}{\sqrt[2]{n}}\right)\]

numbers <- 2:22
answer <- prod(3*numbers + 3/sqrt(numbers))
answer
## [1] 3.549961e+31

Q. 2e (10 points)

\[\sum_{n=1}^{9}\left(\frac{3^{n}}{n^3} + \frac{n^{5}}{4^{5}}+\frac{n^{7}}{7^{n}}+\frac{n^{9}}{9^{n}}\right)\] (5 points)

numbers <- 1:9
answer <- sum((3^numbers/numbers^3)+(numbers^5/4^5)+(numbers^7/7^numbers)+(numbers^9/9^numbers))
answer
## [1] 338.3396

Q. 2f (10 points)

Describe the purpose of is.logical() , is.character() , is.numeric() , and is.na() functions in R.

In Python, describe the purpose of is.instance() , type() , and pd.isna() functions from the pandas library.

Hint: Please use x <- c(“a”, FALSE, “b”, NA, 2, TRUE) in R or x = [“a”, False, “b”, None, 2, True] in Python to explain your description.

x <- c(“a”, FALSE, “b”, NA, 2, TRUE)

In R, objects belong to specific classes, which have particular properties dictating how they can be operated on. The functions is.logical(), is.character(), is.numeric(), and is.na(). These functions assess the object and return whether they belong to that particular class. When presented with a list, the function determines whether the list type is of the same class (i.e. if all elements of the list belong to that class).

For example: is.numeric(x) returns FALSE because there are are logical (TRUE/FALSE) elements and character (“a”) elements.

is.numeric(x)

On the other hand, is.na() will return FALSE FALSE FALSE TRUE FALSE FALSE, since only the object that belongs to that class is at index [4].

is.na(x)

By using these functions, it allows us to select for certain data types, which is important because not all functions are capable of operating on every type.