-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16 vectors.r
More file actions
112 lines (63 loc) · 1.9 KB
/
16 vectors.r
File metadata and controls
112 lines (63 loc) · 1.9 KB
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
# A vector is simply a list of items that are of the same type.
# Vector of string
fruits <- c("banana", "apple", "orange")
# Print fruits
fruits
# Vector of numerical value
numbers <- c(1, 2, 3)
# Print numbers
numbers
# Vector with numerical values in a sequence
numbers <- 1:10
numbers
# Vector with numerical decimals in a sequence
numbers1 <- 1.5:6.5
numbers1
# Vector with numerical decimals in a sequence where the last element is not used
numbers2 <- 1.5:6.3
numbers2
# Vector of logical values
log_values <- c(TRUE, FALSE, TRUE, FALSE)
log_values
#Vector length
fruits <- c("banana", "apple", "orange")
length(fruits)
#Sorting of vector
fruits <- c("banana", "apple", "orange", "mango", "lemon")
numbers <- c(13, 3, 5, 7, 20, 2)
sort(fruits) # Sort a string
sort(numbers) # Sort numbers
#Acessing the Vectors
fruits <- c("banana", "apple", "orange")
# Access the first item (banana)
fruits[1]
#You can also access multiple elements by referring to different index positions with the c() function:
fruits <- c("banana", "apple", "orange", "mango", "lemon")
# Access the first and third item (banana and orange)
fruits[c(1, 3)]
#Change an Item
fruits <- c("banana", "apple", "orange", "mango", "lemon")
# Change "banana" to "pear"
fruits[1] <- "pear"
# Print fruits
fruits
# Repeat Vectors
# To repeat vectors, use the rep() function:
repeat_each <- rep(c(1,2,3), each = 2)
repeat_each
repeat_each <- rep(c("banana","orange","grapes"), each = 2)
repeat_each
#Repeat the sequence of the vector:
repeat_time <- rep(c(1,2,3), time = 2)
repeat_time
#Repeat each value independently:
repeat_indepent <- rep(c(1,2,3), times = c(5,2,1))
repeat_indepent
#Generating Sequenced Vectors
# Vector with numerical values in a sequence
numbers <- 1:10
# Print numbers
numbers
#To make bigger or smaller steps in a sequence, use the seq() function:
numbers <- seq(from = 0, to = 100, by = 20)
numbers