Assignment

In this assignment, we’ll review R programming fundamentals. Remember to place all code inside a code chunk.

Problem 1.

Create an empty numeric vector of length 3 called a Then assign the elements of a the values 5, 6, and 6, respectively.

a <- c(5,6,6)
length(a)
## [1] 3
"This is the code for vector a, I used the length function to print it below"
## [1] "This is the code for vector a, I used the length function to print it below"

Problem 2.

Create a new numeric vector called b which is the result of squaring each element of a. Then change the value of the second element of b so that it is the same as the first element of b.

b <- c(a^2)
print(b)
## [1] 25 36 36
"Vector b is the product of vector a values squared."
## [1] "Vector b is the product of vector a values squared."
b <- replace(b, 2, 25)
print(b)
## [1] 25 25 36
"The replace function replaces the 2nd value of vector b with the desired value 25"
## [1] "The replace function replaces the 2nd value of vector b with the desired value 25"

Problem 3.

Find the result of a times b and assign it to a new variable, c. Then print out the sum of the elements of c.

c <- (a*b)
print(c)
## [1] 125 150 216
"Vector c is the value of vector a times vector b"
## [1] "Vector c is the value of vector a times vector b"

Problem 4.

Re-visit problems 1, 2, and 3, and add comments to your code explaining what the code does.

Problem 5.

Change the third element of b to -2. print out the values of b and c. c was defined using b, did the value of c change?

b <- replace(b,3,-2)
print(b)
## [1] 25 25 -2
print(c)
## [1] 125 150 216

The value of c did not change, because the values of c rely on the previous b vector where the 3rd value is 36. If i rewrite the code now after changing b, c will also change.

c <- c(a*b)
print(c)
## [1] 125 150 -12

Problem 6.

create a new variable d which is the result of converting a into a character vector. combine a, b, c, and d into a list called letters.

d <- as.character(a)
print(d)
## [1] "5" "6" "6"
letters <- list(a,b,c,d)
print(letters)
## [[1]]
## [1] 5 6 6
## 
## [[2]]
## [1] 25 25 -2
## 
## [[3]]
## [1] 125 150 -12
## 
## [[4]]
## [1] "5" "6" "6"

Problem 7.

create a data frame from letters and call it df. Change the column names of df to have names a, b, c, and d, respectively. Then print out the dimension of df.

df <- data.frame(letters)

colnames(df) <- c("a","b","c","d")

names(df)
## [1] "a" "b" "c" "d"
dim(df)
## [1] 3 4

Problem 8.

Create a logical vector by comparing which elements in a are equal to 6.

a==6
## [1] FALSE  TRUE  TRUE

Problem 9.

remove the letters variable from R. Did this also remove a, b, c, and d?

drop <- c("letters")

print(df)
##   a  b   c d
## 1 5 25 125 5
## 2 6 25 150 6
## 3 6 -2 -12 6

No, it did not remove a,b,c,d from the dataframe.

Problem 10.

Create a numeric vector called two which has elements 1 and 2. Create a numeric vector called four which has four ones. Normally you can’t add vectors of different length, but let’s try anyways. Print the result of two plus four. What did R do?

two <- c(1,2)

four <- c(1,1,1,1)

print (two + four)
## [1] 2 3 2 3

End

This is the end of the assignment! You can knit the document and upload it to Canvas