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