R – Exercise II

1) Create an integer variable e holding the value 42! Check the object class of e with class()!

e <- 42L

class(e)
## [1] "integer"

2) Convert e to the character data type with as.character()! Check the object class again!

e <- as.character(e)

class(e)
## [1] "character"

3) Create a character vector friends with four names from your circle of friends or acquaintances!

friends <- c("Anna", "Otto", "Natan", "Ede")

friends
## [1] "Anna"  "Otto"  "Natan" "Ede"

4) Index the second element from the vector friends!

friends[2]
## [1] "Otto"

5) Replace the first element of the vector friends with “Isolde” and check the updated vector again!

freunde[1] <- "Isolde"

freunde
## [1] "Isolde" "Otto"   "Natan"  "Ede"

6) Create a vector v1 from the following elements  1, “Hello”, 2, “World” ! Check the object class!

v1 <- c(1, "Hello", 2, "World")

v1
## [1] "1"     "Hello" "2"     "World"

class(v1)
## [1] "character"

7) Create a vector v2 with numerical values ​​(only integers) ranging from 4 to 10!

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

8) Index the first three elements from v2!

v2[1:3]
## [1] 4 5 6

# or:

v2[ c(1, 2, 3) ]
## [1] 4 5 6

9) Index all elements of v2 except the second element and save the result as v2.subset!

v2.subset <- v2[-2]

v2.subset
## [1]  4  6  7  8  9 10

10) Use the length () function to find out the length of v2.subset (= the number of elements in the vector)!

length(v2.subset)
## [1] 6

11) Calculate the arithmetic mean of vector v2! In addition, determine the standard deviation of v2.subset!

mean(v2)
## [1] 7

sd(v2.subset)
## [1] 2.160247