Question 1

What are the values of:

7/0
## [1] Inf
0/0
## [1] NaN
0/5
## [1] 0
Y <- c(1,2,3,NA)
Y[4]
## [1] NA

Your Answer:

Inf
NaN
0
NA

Question 2

Suppose you want to create a new variable named myVariable with a value of 1299. Then you want to verify if you’ve correctly assigned the value to myVariable (using the comparison operator – this command should return a TRUE or FALSE value). Write two R commands for that, and explain how to assess whether you’ve correctly assigned the value from the second line of code.

Your Answer:

# Assign value of 1299 to myVariable

myVariable <- 1299

# To see if the value of 1299 has been added correctly use

myVariable == 1299
## [1] TRUE
# the output of myVariable == 1299 should be TRUE

# Another way to check if the value of myVariable is 1299

myVariable
## [1] 1299
# The output should be 1299

Question 3

What is the difference between lists and vectors?

If I’m trying to store data for a student studying in the IAF program at UNCG, which of the two should I use, and why?

Which data structure should I use if I want to store the student information about all the IAF students at UNCG?

Your Answer:

The difference between vectors and lists is that vectors contain elements all of the same type, whereas lists may contain values of different types.

Considering the different types of data that might be accumulated on the student, a List seems to be the more appropriate type rather than a vector.

An array matrix would be a good structure to hold all of the different students information.

Question 4

Suppose you have a dataframe a.df, as we had in the Module 1 content.

It has three variables—v1 and v2, which are numeric variables, and the third variable called logicals, which has boolean values.

a.df

v1 v2 logicals

1 35 10 TRUE

2 8 4 FALSE

Add a valid row and a valid column (with name abc of type “character”) to a.df and call that data frame b.df.

What happens if I use the command rbind(a.df, c(3,4,7))? Do you notice something different?

Your Answer:

# Creating the a.df from the example

a.mat = matrix(c(35,8,10,4), nrow=2)
colnames(a.mat) = c("v1","v2")

## View a.mat
a.mat
##      v1 v2
## [1,] 35 10
## [2,]  8  4
a.df = data.frame(a.mat,logicals=c(TRUE,FALSE))

## View a.df
a.df
##   v1 v2 logicals
## 1 35 10     TRUE
## 2  8  4    FALSE
## Adding a row to a.df, creating b.df, and adding a column to b.df of type character

b.df = data.frame(rbind(a.df,list(v1 = 2,v2 = 3,logicals = TRUE)), "character" = c("a", "b", "c"))

## View b.df
b.df
##   v1 v2 logicals character
## 1 35 10     TRUE         a
## 2  8  4    FALSE         b
## 3  2  3     TRUE         c

Using the rbind(a.df, c(3,4,7)) command changes the boolean values from logicals to numbers.

Question 5

To create a vector of 100 uniformly distributed random numbers, we can use x.vec = runif(100).

Your task is to:

Convert this vector into a vector of integers (these should also be stored as integers), and call it y.vec.These integers should not be all 0 and 1. Find the positions of all even numbers in y.vec, and then create a vector z.vec, such that every even number in y.vec is incremented by 10, and every odd number is left as is.

Your Answer:

## Create x.vec of 100 random numbers

x.vec = runif(100)

## View x.vec
x.vec
##   [1] 0.378458409 0.599617750 0.704626701 0.435206148 0.650600221 0.367510877
##   [7] 0.632416571 0.653702306 0.503882113 0.060785044 0.530449463 0.828296531
##  [13] 0.683491672 0.958641988 0.502207090 0.307080301 0.008844989 0.271210633
##  [19] 0.691742891 0.104086748 0.640645972 0.031132346 0.818882482 0.232462477
##  [25] 0.518843781 0.677869774 0.222600759 0.877102958 0.573762596 0.922602887
##  [31] 0.627503861 0.343414024 0.824379696 0.853408182 0.503092934 0.199303102
##  [37] 0.289185504 0.263083268 0.133221744 0.593266024 0.653388167 0.703245860
##  [43] 0.886852951 0.111037152 0.573014849 0.782310942 0.678069833 0.576089178
##  [49] 0.133450390 0.884456204 0.610354088 0.925017250 0.325857631 0.021555756
##  [55] 0.214591676 0.399749471 0.316781963 0.357396568 0.890390951 0.211260828
##  [61] 0.058451515 0.501864739 0.965857601 0.349461600 0.965142581 0.228979454
##  [67] 0.910062961 0.709997471 0.238899342 0.117169291 0.313834414 0.140383121
##  [73] 0.519550818 0.877364600 0.059551321 0.786853377 0.391872243 0.066778021
##  [79] 0.406304428 0.571605107 0.974018665 0.755741293 0.846871714 0.561772000
##  [85] 0.806562137 0.101253285 0.540600230 0.025119078 0.125920841 0.844475928
##  [91] 0.062033462 0.198212457 0.007817234 0.751355245 0.706543864 0.971384631
##  [97] 0.155669400 0.441309214 0.020690359 0.219086229
## multiply x.vec by 10 and round all elements to nearest int to create y.vec of all integers. this creates numbers between 0 and 10

y.vec = round(x.vec*10, digits = 0)

## View y.vec
y.vec
##   [1]  4  6  7  4  7  4  6  7  5  1  5  8  7 10  5  3  0  3  7  1  6  0  8  2  5
##  [26]  7  2  9  6  9  6  3  8  9  5  2  3  3  1  6  7  7  9  1  6  8  7  6  1  9
##  [51]  6  9  3  0  2  4  3  4  9  2  1  5 10  3 10  2  9  7  2  1  3  1  5  9  1
##  [76]  8  4  1  4  6 10  8  8  6  8  1  5  0  1  8  1  2  0  8  7 10  2  4  0  2
## Create a position vector where True fill position of even number and False fills position of odd number

pos.vec = y.vec %% 2 == 0

## View pos.vec
pos.vec
##   [1]  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE
##  [13] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
##  [25] FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE
##  [37] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE
##  [49] FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
##  [61] FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE
##  [73] FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [85]  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE
##  [97]  TRUE  TRUE  TRUE  TRUE
## Create a z.vec where the odd numbers in y.vec remain the same and even numbers are increased by 10

z.vec = ifelse(y.vec %% 2 == 0, y.vec + 10, y.vec)

## View z.vec
z.vec
##   [1] 14 16  7 14  7 14 16  7  5  1  5 18  7 20  5  3 10  3  7  1 16 10 18 12  5
##  [26]  7 12  9 16  9 16  3 18  9  5 12  3  3  1 16  7  7  9  1 16 18  7 16  1  9
##  [51] 16  9  3 10 12 14  3 14  9 12  1  5 20  3 20 12  9  7 12  1  3  1  5  9  1
##  [76] 18 14  1 14 16 20 18 18 16 18  1  5 10  1 18  1 12 10 18  7 20 12 14 10 12

Question 6

Two vectors x and y are such that x = [12 3 4 5 80 9 50] and y = [2 3]. In addition, a matrix

z =

[1 4 8 9

10 2 -3 4];

Write a code in R to create x, y, and z. Then, perform the operation x^(-log(y))+1 and explain (in words) what it does.

Also, create a new matrix newz so that the elements in the first row of matrix z are multiplied by the first element of vector y, and elements in the second row
of z are multiplied by the second element of y.

Your Answer:

x <- c(12, 3, 4, 5, 80, 9, 50)
y <- c(2,3)
x^(-log(y)) + 1
## Warning in x^(-log(y)): longer object length is not a multiple of shorter
## object length
## [1] 1.178636 1.299108 1.382546 1.170649 1.047960 1.089466 1.066430

When the operatrion x^(-log(y)) + 1 is performed it uses the elements for x and the first element for y which is 2. Then it displays a warning message that says “longer object length is not a multiple of shorter object length”.

z = matrix(data = c(1,10, 4, 2, 8, -3, 9, 4), nrow = 2, ncol = 4)
z
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    8    9
## [2,]   10    2   -3    4
# Create matrix newz
newz = z*y

# View matrix newz
newz
##      [,1] [,2] [,3] [,4]
## [1,]    2    8   16   18
## [2,]   30    6   -9   12

Question 7

Use the rep function to create a vector of length 10 so that its entries are repeated as False, True, False, True, etc.

Using the same function, create a vector of length 20 so that its first eight entries have repeated entries 1, 2, 1, 2, etc. and the final 12 entries are repeated entries with -1, -2, -1, -2, etc.

Your Answer:

# Create a vector (v10) of length 10 alternating False, True

v10 <- rep(c(FALSE, TRUE),length = 10)

## View v10
v10
##  [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
# To do this create 2 vectors a and b and combine them into a length 20 vector

a <- rep(c(1,2), length = 8)
b <- rep(c(-1,-2), length = 12)
v20 <- c(a,b)

# View v20
v20
##  [1]  1  2  1  2  1  2  1  2 -1 -2 -1 -2 -1 -2 -1 -2 -1 -2 -1 -2

Question 8

Use the runif function to create a matrix of size 10 x 10. Then make two submatrices of this matrix:

A. All entries which are greater than 0.5 are multiplied by 25. B. For the matrix created in A, display only the rows with row sums more than 50. C. Create a new matrix with rows as in B and odd-numbered columns (1, 3, 5, etc.).

Your Answer:

## Create 10 x 10 matrix with runif
s.mat <- matrix(runif(100), nrow = 10, ncol = 10)

## View s.mat
s.mat
##             [,1]       [,2]       [,3]       [,4]      [,5]       [,6]
##  [1,] 0.91226699 0.38750338 0.99447531 0.46430462 0.4129035 0.80869093
##  [2,] 0.19985062 0.04556681 0.33277917 0.18756223 0.4802270 0.28106832
##  [3,] 0.06670165 0.04643869 0.11189187 0.91081230 0.5500036 0.02902507
##  [4,] 0.49194346 0.54835733 0.34715287 0.71258815 0.5733559 0.55614493
##  [5,] 0.91190416 0.30346851 0.59835463 0.53471992 0.8931138 0.31052781
##  [6,] 0.70627920 0.09319102 0.82466901 0.08684899 0.5697386 0.10905915
##  [7,] 0.91125666 0.70173118 0.33183482 0.70024936 0.1793421 0.09585770
##  [8,] 0.66443592 0.26125951 0.03225427 0.43086025 0.9203201 0.09876655
##  [9,] 0.44047853 0.47130010 0.16402340 0.21908110 0.7712933 0.73826175
## [10,] 0.45347271 0.17159280 0.40351902 0.93287159 0.5021177 0.26875887
##             [,7]      [,8]       [,9]      [,10]
##  [1,] 0.08619511 0.8631220 0.15669537 0.47871767
##  [2,] 0.62796035 0.5022858 0.91387954 0.23272813
##  [3,] 0.71232706 0.1869197 0.56466593 0.33786780
##  [4,] 0.04912841 0.6499857 0.98360753 0.91140933
##  [5,] 0.95218907 0.0300812 0.95488867 0.46026728
##  [6,] 0.60664345 0.4215794 0.09320061 0.07451423
##  [7,] 0.93503809 0.7915347 0.42004110 0.27291551
##  [8,] 0.09831978 0.6529146 0.20995515 0.04192352
##  [9,] 0.10111175 0.3630048 0.34095865 0.26572541
## [10,] 0.74786620 0.8434831 0.03237296 0.42505688
## Create sub matrix where values > .5 are multiplied by 25
t.mat <- ifelse(s.mat[,] > .5, s.mat[,]*25, s.mat[,])

## Display sub matrix with only rows that sum > 50
rsum <- rowSums(t.mat)
t.mat[rsum > 50,]
##              [,1]        [,2]        [,3]        [,4]       [,5]        [,6]
##  [1,] 22.80667479  0.38750338 24.86188263  0.46430462  0.4129035 20.21727329
##  [2,]  0.19985062  0.04556681  0.33277917  0.18756223  0.4802270  0.28106832
##  [3,]  0.06670165  0.04643869  0.11189187 22.77030745 13.7500897  0.02902507
##  [4,]  0.49194346 13.70893325  0.34715287 17.81470387 14.3338987 13.90362314
##  [5,] 22.79760394  0.30346851 14.95886581 13.36799805 22.3278454  0.31052781
##  [6,] 17.65697993  0.09319102 20.61672533  0.08684899 14.2434639  0.10905915
##  [7,] 22.78141660 17.54327951  0.33183482 17.50623388  0.1793421  0.09585770
##  [8,] 16.61089797  0.26125951  0.03225427  0.43086025 23.0080021  0.09876655
##  [9,]  0.45347271  0.17159280  0.40351902 23.32178972 12.5529434  0.26875887
##              [,7]       [,8]        [,9]       [,10]
##  [1,]  0.08619511 21.5780494  0.15669537  0.47871767
##  [2,] 15.69900865 12.5571445 22.84698848  0.23272813
##  [3,] 17.80817646  0.1869197 14.11664817  0.33786780
##  [4,]  0.04912841 16.2496429 24.59018833 22.78523333
##  [5,] 23.80472681  0.0300812 23.87221687  0.46026728
##  [6,] 15.16608630  0.4215794  0.09320061  0.07451423
##  [7,] 23.37595234 19.7883681  0.42004110  0.27291551
##  [8,]  0.09831978 16.3228654  0.20995515  0.04192352
##  [9,] 18.69665496 21.0870780  0.03237296  0.42505688
## Create new matrix from matrix above with only the odd columns
n = c(1:10)
u.mat <- t.mat[rsum > 50, n %% 2 != 0]

## View new matrix
u.mat
##              [,1]        [,2]       [,3]        [,4]        [,5]
##  [1,] 22.80667479 24.86188263  0.4129035  0.08619511  0.15669537
##  [2,]  0.19985062  0.33277917  0.4802270 15.69900865 22.84698848
##  [3,]  0.06670165  0.11189187 13.7500897 17.80817646 14.11664817
##  [4,]  0.49194346  0.34715287 14.3338987  0.04912841 24.59018833
##  [5,] 22.79760394 14.95886581 22.3278454 23.80472681 23.87221687
##  [6,] 17.65697993 20.61672533 14.2434639 15.16608630  0.09320061
##  [7,] 22.78141660  0.33183482  0.1793421 23.37595234  0.42004110
##  [8,] 16.61089797  0.03225427 23.0080021  0.09831978  0.20995515
##  [9,]  0.45347271  0.40351902 12.5529434 18.69665496  0.03237296

Question 9

State why R encounters errors in the following statements. Provide an alternate solution to fix these errors (assuming that I still want to use a standard boolean operator &:

(0 > 0) & all(matrix(0,2,2) == matrix(0,3,3))

(0 > 0) & (ThisVariableIsNotDefined == 0)

Your Answer:

Using & looks at all elements within the statement. If there is a problem with any element within the statement it will return an error message. Using && will terminate when it reads the first false element within the statement and can be used here to recognize the statement to be false. The statement (0 > 0) is a false statement, and we could use && to return FALSE. However, because the matrices in the second part of the statement are non-compatable, if the first part were (0 == 0) then the && would still return an error. For us to use just the & operator the 2 matrices would need to be the same size to not create an error message. Just like in A the first half of the statement (0 > 0) is false so using the && operator would return a value of FALSE because it would terminate after reading that statement. However, the only reason this statement doesn’t work is because ‘ThisVariableIsNotDefined’ has no numerical value assigned to it. So we could give it a value for the & operator to return a logical value.

Question 10

Consider the list x.list created below. Complete the following tasks, each with a single line of code:

A. Extract all but the second element of x.list—you’re looking for a list as the final answer.

B. Extract the first and third elements of x.list, then extract the second element of the resulting list—you’re looking for a vector as the final answer.

C. Extract the second element of x.list as a vector, and then extract the first 10 elements of this vector—you’re looking for a vector as the final answer.

Note: pay close attention to what’s asked, and use either single brackets [ ] or double brackets [[ ]] as appropriate.

x.list = list(rnorm(6), letters, sample(c(TRUE,FALSE),size=4,replace=TRUE))
x.list
## [[1]]
## [1]  0.6084204  1.2342516  0.6113058 -0.1233730 -0.6505558 -0.4318676
## 
## [[2]]
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
## [20] "t" "u" "v" "w" "x" "y" "z"
## 
## [[3]]
## [1]  TRUE FALSE FALSE FALSE

Your Answer:

## Part A

x.list[-2]
## [[1]]
## [1]  0.6084204  1.2342516  0.6113058 -0.1233730 -0.6505558 -0.4318676
## 
## [[2]]
## [1]  TRUE FALSE FALSE FALSE
## Part B

x.list[-2][[2]]
## [1]  TRUE FALSE FALSE FALSE
## Part C

x.list[[2]][1:10]
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"