- Create an integer variable
e
holding the value42
! Check the object class ofe
withclass()
!
Click here to see the answer
<- 42L
e
class(e)
## [1] "integer"
- Convert
e
to the character data type withas.character()
! Check the object class again!
Click here to see the answer
<- as.character(e)
e
class(e)
## [1] "character"
- Create a character vector
friends
with four names from your circle of friends or acquaintances!
Click here to see the answer
<- c("Anna", "Otto", "Natan", "Ede")
friends
friends## [1] "Anna" "Otto" "Natan" "Ede"
- Index the second element from the vector
friends
!
Click here to see the answer
2]
friends[## [1] "Otto"
- Replace the first element of the vector
friends
with “Isolde” and check the updated vector again!
Click here to see the answer
1] <- "Isolde"
freunde[
freunde## [1] "Isolde" "Otto" "Natan" "Ede"
- Create a vector
v1
from the following elements1, "Hello", 2, "World"
! Check the object class!
Click here to see the answer
<- c(1, "Hello", 2, "World")
v1
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
<- c(4, 5, 6, 7, 8, 9, 10)
v2
v2## [1] 4 5 6 7 8 9 10
# or use the sequence operator ":"
<- c(4:10)
v2
v2## [1] 4 5 6 7 8 9 10
- Index the first three elements from
v2
!
Click here to see the answer
1:3]
v2[## [1] 4 5 6
# or:
c(1, 2, 3) ]
v2[ ## [1] 4 5 6
- Index all elements of
v2
except the second element and save the result asv2.subset
!
Click here to see the answer
<- v2[-2]
v2.subset
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