Section 1.1

Q1 Which of the following code using to install packages into R will cause an error?

#install.packages(r02pro) ,the right one should be install.packages("r02pro")

Q2 Write R code to load the packages r02pro

#install.packages("r02pro")

Q3 calculate 2 + 3

2+3
## [1] 5

Section 1.2

Q1 compute √5×5

sqrt(5*5)
## [1] 5

Q2 get help on the function floor.

floor(1.15)
## [1] 1

Q3 compute the square of π and round it to 4 digits after the decimal point

round(sqrt(pi),digits=4)
## [1] 1.7725

Q4 compute the logarithm of 1 billion with base 1000

log(10^9,1000)
## [1] 3

Q5 verify sin2(x)+cos2(x)=1, for x=724

sin(724)^2+cos(724)^2
## [1] 1

Section 2.1

Q1 assign the value 20 to the name num_1

num_1 <- 20

Q2 Which of the following is a valid object name in R?

#I_am_not_a_valid_name

Q3 get the list of all objects in the environment

ls() 
## [1] "num_1"

Section 2.2

Q1 create a numeric vector named vec_1 with values 7 24 8 26, get its length, and find out its type

vec_1 <- c(7,24,8,26)
length(vec_1)
## [1] 4
class(vec_1)
## [1] "numeric"

Q2 create a character vector named char_1 with values “I”, “am”, “learning”, “R!”, get its length, find out its type, and concatenate the vector into a single string with space as the separator.

char_1 <- c("I","am","learning","R")
length(char_1)
## [1] 4
class(char_1)
## [1] "character"
one_long_string <- paste("I","am","learning","R")
one_long_string
## [1] "I am learning R"

Q3 For the char_1 defined in Q2, find the number of characters in each string, and convert each string to upper case

char_1 <- c("I","am","learning","R")
nchar(char_1) 
## [1] 1 2 8 1
toupper(char_1)
## [1] "I"        "AM"       "LEARNING" "R"

Q4 Create a length-2 logical vector representing whether vec_1 and char_1 are of character type

logic1 <- c(is.character(vec_1),is.character(char_1))
logic1
## [1] FALSE  TRUE
length(logic1)
## [1] 2

Q5 Let class1 <- c(7, TRUE). Which of the following is the class of class1?

class1 <- c(7, TRUE)
class1
## [1] 7 1
class(class1)
## [1] "numeric"
#numeric

Q6 Let class2 <- c(7, TRUE, “char”). Which of the following is the class of class2?

class2 <- c(7, TRUE, "char")
class2
## [1] "7"    "TRUE" "char"
class(class2)
## [1] "character"
#character