-
Notifications
You must be signed in to change notification settings - Fork 0
/
R_script_0014.R
153 lines (82 loc) · 2.17 KB
/
R_script_0014.R
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
# Code by @AmirMotefaker
# R Vectors
# Vectors
# Solution 1
# Vector of strings
fruits <- c("banana", "apple", "orange")
# Print fruits
fruits
# [1] "banana" "apple" "orange"
# Solution 2
# Vector of numerical values
numbers <- c(1, 2, 3)
# Print numbers
numbers
# [1] 1 2 3
# Solution 3
# Vector with numerical values in a sequence
numbers <- 1:10
numbers
# [1] 1 2 3 4 5 6 7 8 9 10
# Solution 4
# Vector with numerical decimals in a sequence
numbers1 <- 1.5:6.5
numbers1
# [1] 1.5 2.5 3.5 4.5 5.5 6.5
# Vector with numerical decimals in a sequence where the last element is not used
numbers2 <- 1.5:6.3
numbers2
# [1] 1.5 2.5 3.5 4.5 5.5
# Solution 5
# Vector of logical values
log_values <- c(TRUE, FALSE, TRUE, FALSE)
log_values
# [1] TRUE FALSE TRUE FALSE
# Vector Length
fruits <- c("banana", "apple", "orange")
length(fruits)
# [1] 3
# Sort a Vector
fruits <- c("banana", "apple", "orange", "mango", "lemon")
numbers <- c(13, 3, 5, 7, 20, 2)
sort(fruits) # Sort a string
# [1] "apple" "banana" "lemon" "mango" "orange"
sort(numbers) # Sort numbers
# [1] 2 3 5 7 13 20
# Access Vectors
fruits <- c("banana", "apple", "orange")
# Access the first item (banana)
fruits[1]
# [1] "banana"
fruits <- c("banana", "apple", "orange", "mango", "lemon")
# Access the first and third item (banana and orange)
fruits[c(1, 3)]
# [1] "banana" "orange"
fruits <- c("banana", "apple", "orange", "mango", "lemon")
# Access all items except for the first item
fruits[c(-1)]
# [1] "apple" "orange" "mango" "lemon"
# Change an Item
fruits <- c("banana", "apple", "orange", "mango", "lemon")
# Change "banana" to "pear"
fruits[1] <- "pear"
# Print fruits
fruits
# [1] "pear" "apple" "orange" "mango" "lemon"
# Repeat Vectors
repeat_each <- rep(c(1,2,3), each = 3)
repeat_each
# [1] 1 1 1 2 2 2 3 3 3
repeat_times <- rep(c(1,2,3), times = 3)
repeat_times
# [1] 1 2 3 1 2 3 1 2 3
repeat_indepent <- rep(c(1,2,3), times = c(5,2,1))
repeat_indepent
# [1] 1 1 1 1 1 2 2 3
# Generating Sequenced Vectors
numbers <- 1:10
numbers
# [1] 1 2 3 4 5 6 7 8 9 10
numbers <- seq(from = 0, to = 100, by = 20)
numbers
# [1] 0 20 40 60 80 100