WWW - Cuchd.in Campus: Gharuan, Mohali
WWW - Cuchd.in Campus: Gharuan, Mohali
R prompt:
_var_name
www.cuchd.in invalid Starts with _ which is not valid
Campus: Gharuan, Mohali
Your First R Session
[1] 81
www.cuchd.in Campus: Gharuan, Mohali
>2* # When we want to type ‘2*4’ but the line is incomplete. If a
+4 line is not syntactically completed, then a continuation prompts
[1] 8 (+) appears.
Functions:
# R functions are invoked by its name, then followed by the
> c(1, 2, 3) parenthesis, and zero or more arguments. The following apply
[1] 1 2 3 the function c to combine three numeric values into a vector.
Comments: # All text after the pound sign "#" within the same line is
considered a comment.
>1+1
# this is comment
[1] 2
> 15 -2 When we type '15 -2' and hit the <Enter> key, R will
[1] 13 show the result of the calculation, which is equal to
13 .
> 20*5
[1] 100 When we type ’20*5' and hit the <Enter> key, R will
show the result of the calculation, which is equal to
100 .
> 10/2 When we type '10/2' and hit the <Enter> key, R will
[1] 5 show the result of the calculation, which is equal to 5 .
Integer
Character
Logical
Complex number
> sqrt(as.complex(−1))
[1] 0+1i
Operators Descriptions
+ Addition
- Subtraction
* Multiplication
/ Division
^ or ** Exponentiation
x %% y Modulus (x mod y) Exp. 5%%2 is 1
x %/% y Integer division Exp. 5%/%2 is 2
Operator Description
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== exactly equal to
!= not equal to
! Not !x means not x
| OR x OR y
& AND x AND y
isTRUE(x) test if x is TRUE
www.cuchd.in Campus: Gharuan, Mohali
Logical & Relational Operators
Generally logical and relational operators are used with
conditional programming statements if and else.
[1] 2
> if(w!=2)u<-w
>u
Error: Object "u" not found
Control Structures
Control by repetition, or looping, allows you to efficiently repeat
code without having to write the same code over and over.
Syntax Comments
if()...else Determine which set of expressions to run depending on whether or
not a condition is TRUE
ifelse() Do something to each element in a data structure depending on
whether or not a condition is TRUE for that particular element
switch() Evaluate different expressions depending on a given value
for() Loop for a fixed number of iterations
while() Loop until a condition is FALSE
repeat Repeat until iterations are halted with a call to break
break Break out of a loop
next Stop processing the current iteration and advance the looping index
if()...else Determine which set of expressions to run depending on whether or
not a condition is TRUE
>x<- c(15,14,12,18,19,11)
> n <- length(x)
> sort.x <- sort(x)
> if(n%%2==0) {
+ median <-
(sort.x[n/2]+sort.x[1+n/2])/2
+ } else median <- sort.x[(n+1)/2]
> median
[1] 62
if else()
ifelse() returns a value with the same structure as test
which is filled with elements selected from either yes or no
depending on whether the element of test is TRUE or
FALSE. ifelse(test, yes, no)
> (x <- seq(3,15,len=6))
[1] 3.0 5.4 7.8 10.2 12.6 15.0
print(seq(5, 9, by=0.4))
Seq(1:15)
Seq(2,50,by=4)
s <- c('apple','red',5,TRUE)
print(s)
print(result[1,3,1])
print(result[,,2])