[go: up one dir, main page]

0% found this document useful (0 votes)
15 views34 pages

R Basics - 02

Uploaded by

Vipul Khandke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views34 pages

R Basics - 02

Uploaded by

Vipul Khandke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

ICS422 Applied Predictive Analytics [3- 0-0-3]

R Programming Basics

Class 06
Presented by
Dr. Selvi C
Assistant
Professor
IIIT Kottayam
Syntax of R Programming
• R Programming is a very popular
programming language which is
broadly used in data analysis. The
way in which we define its code is
quite simple. The "Hello World!" is
the basic program for all the
languages, and now we will
understand the syntax of R
programming with "Hello world"
program. We can write our code
either in command prompt, or we
can use an R script file.
R Command Prompt
• It is required that we have already
installed the R environment set up
in our system to work on the R
command prompt. After the
installation of R environment
setup, we can easily start R
command prompt by typing R in
our Windows command prompt.
When we press enter after typing
R, it will launch interpreter, and
we will get a prompt on which we
can code our program.
2
R Script File
• The R script file is another way on which we can write our programs, and then we
execute those scripts at our command prompt with the help of R interpreter known
as Rscript. We make a text file and write the following code. We will save this file with
.R extension as:
• Demo.R

• To execute this file in Windows and other operating systems, the process will remain
the same as mentioned below.

• When we press enter it will give us the following output:

3
Comments
• In R programming, comments are the programmer readable
explanation in the source code of an R program. The purpose
of adding these comments is to make the source code easier
to understand. These comments are generally ignored by
compilers and interpreters.
• In R programming there is only single-line comment. R doesn't
support multi-line comment. But if we want to perform multi-
line comments, then we can add our code in a false block.
• Single-line comment

4
The trick for multi-line comment

5
R Variables
Creating Variables in R
• Variables are containers for storing data values.
• R does not have a command for declaring a variable. A variable is
created the moment you first assign a value to it. To assign a value to a
variable, use the <- sign. To output (or print) the variable value, just
type the variable name:
Example
• name <- "John"
age <- 40

name # output "John"


age # output 40

6
Note:
• From the example above, name and age are variables,
while "John" and 40 are values.
• In other programming language, it is common to use = as an
assignment operator. In R, we can use both = and <- as assignment
operators.
• However, <- is preferred in most cases because the = operator can be
forbidden in some context in R.

7
Print / Output Variables
• Compared to many other programming languages, you do not have to use a function to
print/output variables in R. You can just type the name of the variable:
Example
• name <- "John Doe"

name # auto-print the value of the name variable


• However, R does have a print() function available if you want to use it. This might be
useful if you are familiar with other programming languages, such as Python, which often
use a print() function to output variables.
Example
• name <- "John Doe"

print(name) # print the value of the name variable

8
Concatenate Elements
• You can also concatenate, or join, two or
more elements, by using • For numbers, the + character works as a mathematical
the paste() function. operator:
• To combine both text and a variable, R uses Example
comma (,): • num1 <- 5
Example num2 <- 10
• text <- "awesome" num1 + num2

paste("R is", text) • If you try to combine a string (text) and a number, R will
give you an error:
• [1] "R is awesome"
Example
• You can also use , to add a variable to
another variable: • num <- 5
text <- "Some text"
Example
• text1 num + text
<- "R is"
text2 <- "awesome" Result:

paste(text1, text2) • Error in num + text : non-numeric argument to binary


operator

9
Multiple Variables
• R allows you to assign the same value to multiple variables in one line:
Example
• # Assign the same value to multiple variables in one line
var1 <- var2 <- var3 <- "Orange"

# Print variable values


var1
var2
var3

10
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume). Rules for R variables are:
• A variable name must start with a letter and can be a combination of letters, digits,
period(.)
and underscore(_). If it starts with period(.), it cannot be followed by a digit.
• A variable name cannot start with a number or underscore (_)
• Variable names are case-sensitive (age, Age and AGE are three different variables)
• Reserved words cannot be used as variables (TRUE, FALSE, NULL, if...)
• Variable names are case-sensitive

11
Data Types in R Programming
• In programming languages, we need to
use various variables to store various
information. Variables are the reserved
memory location to store values. As we
create a variable in our program, some
space is reserved in memory.
• In R, there are several data types such
as integer, string, etc. The operating
system allocates memory based on the
data type of the variable and decides
what can be stored in the reserved
memory.
• There are the following data types
which are used in R programming:

12
Data Example Description
type
Logical True, False It is a special data type for data with only two possible values
which can be construed as true/false.

Numeric 12,32,112,5432 Decimal value is called numeric in R, and it is the default


computational data type.

Integer 3L, 66L, 2346L Here, L tells R to store the value as an integer,

Complex Z=1+2i, t=7+3i A complex value in R is defined as the pure imaginary value i.

Character 'a', '"good'", "TRUE", In R programming, a character is used to represent string


'35.4' values. We convert objects into character values with the help
ofas.character() function.

Raw A raw data type is used to holds raw bytes.


13
14
R Numbers
• There are three number types in R:
• numeric
• integer
• complex
• Variables of number types are created when you assign a
value to them:
Example
• x <- 10.5 # numeric
y <- 10L # integer
z <- 1i # complex

15
Numeric
• A numeric data type is the most common type in R, and contains any number with or without a
decimal, like: 10.5, 55, 787:
Example
• x <- 10.5
y <- 55

# Print values of x and y


x
y

# Print the class name of x and y


class(x)
class(y)
• [1] 10.5
• [1] 55
• [1] "numeric"
• [1] "numeric"
16
Integer
• Integers are numeric data without decimals. This is used when you are certain that you will never create a
variable that should contain decimals. To create an integer variable, you must use the letter L after the
integer value:
Example
• x <- 1000L
y <- 55L

# Print values of x and y


x
y

# Print the class name of x and y


class(x)
class(y)
• [1] 1000
• [1] 55
• [1] "integer"
• [1] "integer"

17
Complex
• A complex number is written with an "i" as the imaginary part:
Example
• x <- 3+5i
y <- 5i

# Print values of x and y


x
y

# Print the class name of x and y


class(x)
class(y)
• [1] 3+5i
• [1] 0+5i
• [1] "complex"
• [1] "complex"

18
Type Conversion
• You can convert from one
type to another with the
following functions:
• as.numeric()
• as.integer()
• as.complex()

19
Keywords in R Programming
In programming, a keyword is a word which
is reserved by a program because it has a
special meaning. A keyword can be a
command or a parameter. Like in C, C++,
Java, there is also a set of keywords in R. A
keyword can't be used as a variable name.
Keywords are also called as "reserved
names."
There are the following keywords as per ?
reserved or help(reserved) command:
if else repeat
while function for
next break TRUE
FALSE NULL Inf
NaN NA NA_integer_
NA_real_ NA_complex_ NA_characte
r_

20
1) if
• The if statement consists of a Boolean expression which is followed by one or more
statements. In R, if statement is the simplest conditional statement which is used to
decide whether a block of the statement will be executed or not.
Example:
a<-11
if(a<15)
+ print("I am lesser than 15")
Output:

21
2) else
• The R else statement is associated with if statement. When the if statement's condition is false
only then else block will be executed. Let see an example to make it clear:
Example:
a<-22
if(a<20){
cat("I am lesser than 20")
}else{
cat("I am larger than 20")
}

22
3) repeat
• The repeat keyword is used to iterate over a block of code multiple numbers of times. In R,
repeat is a loop, and in this loop statement, there is no condition to exit from the loop. For
exiting the loop, we will use the break statement.
Example:
x <- 1
repeat {
cat(x)
x = x+1
if (x == 6){
break
}
}

23
4) while
• A while keyword is used as a loop. The while loop is executed
until the given condition is true. This is also used to make an
infinite loop.
Example:
a <- 20
while(a!=0){
cat(a)
a = a-2
}

24
5) function
• A function is an object in R programming. The keyword function is used to create a
user-define function in R. R has some pre-defined functions also, such as seq, mean,
and sum.
Example:
new.function<- function(n) {
for(i in 1:n) {
a <- i^2
print(a)
}
}
new.function(6)

25
6) for
• The for is a keyword which is used for looping or iterating over a
sequence (dictionary, string, list, set or tuple).
• We can execute a set of a statement once for each item in the iterator
(list, set, tuple, etc.) with the help of for loop.
Example:
v <- LETTERS[1:4]
for ( i in v) {
print(i)
}

26
7) next
• The next keyword skips the current iteration of a loop without terminating it. When R
parser found next, it skips further evaluation and starts the new iteration of the loop.
Example:
v <- LETTERS[1:6]
for ( i in v) {
if (i == "D") {
next
}
print(i)
}

27
8) break
• The break keyword is used to terminate the loop if the condition is true. The control of
the program firstly passes to the outer statement then passes to the body of the break
statement.
Example:
n<-1
while(n<10){
if(n==3)
break
n=n+1
cat(n,"\n")
}
cat("End of the program")

28
9) TRUE/FALSE
• The TRUE and FALSE keywords are used to represent a
Boolean true and Boolean false. If the given statement is true,
then the interpreter returns true else the interpreter returns
false.

29
10) NULL
• In R, NULL represents the null object. NULL is used to
represent missing and undefined values. NULL is the logical
representation of a statement which is neither TRUE nor
FALSE.

30
11) Inf and NaN
• The is.finite and is.infinite function returns a vector of the same length indicating
which elements are finite or infinite.
• Inf and -Inf are positive and negative infinity. NaN stands for 'Not a Number.' NaN
applies on numeric values and real and imaginary parts of complex values, but it will
not apply to the values of integer vectors.
Usage
• is.finite(x)
• is.infinite(x)
• is.nan(x)

• Inf
• NaN

31
12) NA
• NA is a logical constant of length 1 that contains a missing value indicator. It can be coerced to
any other vector type except raw. There are other types of constant also, such as NA_Integer_,
NA_real_, NA_complex_, and NA_character. These constants are of the other atomic vector type
which supports missing values.
Usage->NA
• is.na(x)
• anyNA(x, recursive = FALSE)
• is.na(x)
• is.na(x) <- value

32
Any
Queries?
Thank you

You might also like