-
Notifications
You must be signed in to change notification settings - Fork 0
/
R2.R
168 lines (122 loc) · 2.86 KB
/
R2.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#--------------------------R naming convention-----------
# var_name2. valid Has letters, numbers, dot and underscore
# var_name% Invalid Has the character '%'. Only dot(.) and underscore allowed.
# 2var_name invalid Starts with a number
# .var_name , var.name valid Can start with a dot(.) but the dot(.)should not be
#followed by a number.
# .2var_name invalid The starting dot is followed by a number making it invalid
# _var_name invalid Starts with _ which is not valid
#---------Operators in R---------------
# arithmetic relational logical conditional ..
v <- c( 2,5.5,4)
t <- c(8, 3, 4)
v+t
v-t
v*t
v/t
v%%t # remainder function
v^t
#----- logical ------
v>t
v<t
v==t
v<=t
v>=t
v!=t
a<- c(0,0,T,3+4i) # last element complex ...
######
x <- 12
y <- 9
k <- 5:10
x %in% k
y %in% k
##############################################
#------if else conditions ------------
#
# if(boolean_expression) {
# // statement(s) will execute if the boolean expression is true.
# }
#....if statement
a <-"z"
if(is.character(a))
{
"a is not a numeric"
}
#....if-else statement
# if(boolean_expression) {
# // statement(s) will execute if the boolean expression is true.
# } else {
# // statement(s) will execute if the boolean expression is false.
# }
if(is.character(a))
{
"a is not a numeric"
}else{
"a is numeric"
}
#ifelse--------
ifelse(is.numeric("a"),1,ifelse(is.character(a),"is a character",0))
#----switch statement ----
x <- switch(
4,
"first",
"second",
"third",
"fourth"
)
x
#switch( expression, case1, case2, .....)
#------ for loop---------------
for(i in 1:4)
{
x<- switch(i,"first",
"second",
"third",
"fourth")
print(x)
}
#-----------------------loop and its working----------------
#
# while (test_expression) {
# statement
# }
v <- c("Hello","while loop")
cnt <- 2
while (cnt < 7){
print(v)
cnt = cnt + 1
}
#################################################
# function_name <- function(arg_1, arg_2, ...) {
# Function body
# }
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
new.function(6)
# Create a function without an argument.
new.function <- function() {
for(i in 1:5) {
print(i^2)
}
}
# Call the function without supplying an argument.
new.function()
new.function <- function(a,b,c) {
result <- a*b+c
print(result)
}
new.function(5,3,11)
new.function(a=11,b=5,c=3)
# Create a function with arguments.
new.function <- function(a = 3,b =6) {
result <- a*b
print(result)
}
# Call the function without giving any argument.
new.function()
# Call the function with giving new values of the argument.
new.function(9,5)