-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter1.Rmd
166 lines (116 loc) · 3.74 KB
/
chapter1.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
title : Creating a Vector
description : In this chapter we will learn how to create a vector
--- type:NormalExercise lang:r xp:50 skills:1 key:0b8961fe9d
##Creating a Vector
In this chapter we will create a vector.
- A vector is a simple tool to store data.
- To create a vector in R, we use the **c()** function aka combine function.
- A vector is of mainly 3 types:
* a. **Numeric Vector** - one that contains only numeric values (integer of float).
* b. **Character Vector** - one that contains either single character values or strings as values.
* c. **Logical Vector** - one that contains logical values (TRUE or FALSE).
- Here are some examples to get you started.
##### Create a numeric vector
```code numeric_vector <- c(1,2,3,4,5)```
##### Create a float vector
```float_vector <- c(10.0,20.0,30.0,40.0,50.0)```
##### Create a character vector with single characters
```scharacter_vector <- c('H','e','l','l','o')```
##### Create a character vector with single characters
```character_vector <- c('Hello','Python','Scala','Java','PHP')```
##### Create a logical vector
```logical_vector <- c(TRUE,FALSE,FALSE,TRUE,FALSE)```
*** =instructions
- create the vectors as taught in information.
- click submit to run.
*** =hint
- click Submit to run the code.
*** =pre_exercise_code
```{r}
```
*** =sample_code
```{r}
# create a numeric_vector with values from 21 to 30
# print the numeric_vector
# create a character_vector with values from A to H
# print the character_vector
# create a logical_vector with values from TRUE,FALSE,FALSE,TRUE,TRUE
# print the logical_vector
```
*** =solution
```{r}
# create a numeric_vector with values from 21 to 30
numeric_vector <- c(21:30)
# print the numeric_vector
print(numeric_vector)
# create a character_vector with values from A to H
character_vector <- c('A','B','C','D','E','F','G','H')
# print the character_vector
print(character_vector)
# create a logical_vector with values from TRUE,FALSE,FALSE,TRUE,TRUE
logical_vector <- c(TRUE,FALSE,FALSE,TRUE,TRUE)
# print the logical_vector
print(logical_vector)
```
*** =sct
```{r}
msg_bad <- "You have got it wrong!!!"
msg_success <- "Good work!!!"
test_mc(correct = 2, feedback_msgs = c(msg_bad, msg_success, msg_bad, msg_bad))
```
--- type:NormalExercise lang:r xp:50 skills:1 key:33331b1823
##Creating a Vector using the : (colon) operator
You can also create create a vector using the **:** operator.
Take a look at the example below:
```
vector_a <- 5:13
```
the output of this will be : 5, 6, 7, 8, 9, 10, 11, 12, 13
The same thing can be done for a float vector
```
vector_b <- 4.5:10.5
```
The output of this will be : 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5
*** =instructions
- create a vector a using : (colon) operator.
- create a float vector using : operator.
- create a vector in descending order using : operator.
*** =hint
- You have to use : operator.
*** =pre_exercise_code
```{r}
```
*** =sample_code
```{r}
# create a vector a with values from 15 to 22
# print the vector a
# create a float vector b with values from 2.3 to 6.7
# print the float vector
# create a vector c with values from 10 to 2 in descending order
# print the vector c
```
*** =solution
```{r}
# create a vector a with values from 15 to 22
a <- 15:22
# print the vector a
a
# create a float vector b with values from 2.3 to 6.7
b <- 2.3:6.7
# print the float vector
b
# create a vector c with values from 10 to 2 in descending order
c <- 10:2
# print the vector c
c
```
*** =sct
```{r}
# SCT written with testwhat: https://github.com/datacamp/testwhat/wiki
test_object("a",
eval = TRUE,
undefined_msg = "You have not used the same names for vectors",
incorrect_msg = "Did you use the same values?")
success_msg("Great work! Keep Going!!!")
```