- Create an integer variable
eholding the value42! Check the object class ofewithclass()!
Click here to see the answer
e <- 42L
class(e)
## [1] "integer"- Convert
eto the character data type withas.character()! Check the object class again!
Click here to see the answer
e <- as.character(e)
class(e)
## [1] "character"- Create a character vector
friendswith four names from your circle of friends or acquaintances!
Click here to see the answer
friends <- c("Anna", "Otto", "Natan", "Ede")
friends
## [1] "Anna" "Otto" "Natan" "Ede"- Index the second element from the vector
friends!
Click here to see the answer
friends[2]
## [1] "Otto"- Replace the first element of the vector
friendswith “Isolde” and check the updated vector again!
Click here to see the answer
freunde[1] <- "Isolde"
freunde
## [1] "Isolde" "Otto" "Natan" "Ede"- Create a vector
v1from the following elements1, "Hello", 2, "World"! Check the object class!
Click here to see the answer
v1 <- c(1, "Hello", 2, "World")
v1
## [1] "1" "Hello" "2" "World"
class(v1)
## [1] "character"- Create a vector v2 with numerical values (only integers) ranging from 4 to 10!
Click here to see the answer
v2 <- c(4, 5, 6, 7, 8, 9, 10)
v2
## [1] 4 5 6 7 8 9 10
# or use the sequence operator ":"
v2 <- c(4:10)
v2
## [1] 4 5 6 7 8 9 10- Index the first three elements from
v2!
Click here to see the answer
v2[1:3]
## [1] 4 5 6
# or:
v2[ c(1, 2, 3) ]
## [1] 4 5 6- Index all elements of
v2except the second element and save the result asv2.subset!
Click here to see the answer
v2.subset <- v2[-2]
v2.subset
## [1] 4 6 7 8 9 10- Use the length () function to find out the length of
v2.subset(= the number of elements in the vector)!
Click here to see the answer
length(v2.subset)
## [1] 6- Calculate the arithmetic mean of vector
v2! In addition, determine the standard deviation ofv2.subset!
Click here to see the answer
mean(v2)
## [1] 7
sd(v2.subset)
## [1] 2.160247