Sie sind auf Seite 1von 47

R is used for coding and calculating

Anything typed in the R console prompt is an expression


<- is the assignment operator
print() function prints the value of the respective variable on the R console
Everything on the right of ## are accepted as a comment
Printing can be
Explicit Printing
Auto printing
2

Variable
declaration with
<- operator

Explicit printing
by print() function

Auto-printing

Comments with
##

R contains only four types of variables

Character - string or any alphanumeric characters are accepted e.g. Hello World
Numeric any numeric object or double precession real number e.g. 10.55 , 4.99 etc.
Complex numbers any complex numbers e.g. 2+4i , 2-4i etc.
Integer any integer e.g. 1,2,3,5 etc. Have to declare with a L suffix e.g. 1L
Logical - this can be either TRUE or FALSE

Every variable that is declared in R console is a Vector


A Vector can contain more than one value of any one of the data types
Vector() can create an empty vector
R also contains two special numbers
INF : Infinity e.g. 1/0 gives us INF while 1/INF gives 0
NaN : Not a number

Every variable that is declared in R console is a Vector


A Vector can contain more than one value of any one of the

data types
Vector() can create an empty vector
R also contains two special numbers
Inf : Infinity e.g. 1/0 gives us INF while 1/INF gives 0
NaN : Not a number
NA
9

Attributes are properties of an object


They can be accessed by attributes() function
They can be
Names
Dimensions
Class
Length
Other user defined attributes

Attributes of the
matrix, the number of
rows and number of
column

10

Vectors can be created by two ways


By the C() function

By the vector() functions

11

Vectors can be explicitly converted by the as.<Data_Type>(<variable_name>) function

After conversion
the resultant can be
stored in a separate
variable
The conversion
does not affect the
data type of the
variable y

12

Mixing more than one objects in a vector results in a vector with a data type of least common

object so that every element is of same type

The first conversion results in a vector


of data type character as it can covert
1.7 to a character element
In second case, the logical element
TRUE is converted to a numeric
element which is greater than 0
In third case, the logical element TRUE
is converted to a character element

13

List is just like a vector in which each element can be of different type

Here a literal is defined such that, the


first element is a character, second
one is a numeric value, third one is an
integer, fourth one is a logical value
and fifth one is a complex number.

14

These are multidimensional vectors


It has a dimension attributes of two variable nrow and ncols
Syntax : <Variable_Name> <- matrix( <data_values>, nrow=<row_value>,ncol=<col_value>)

15

Matrix creation by changing the dimension variables

Transforms x into a
matrix of 2 rows and
5 columns.

16

Matrix creation by cbind() and rbind() function

17

They normally used to arrange categorical data


It is equivalent to a integer vector where each integer has a label
Syntax : <Variable_Name> <- factor( <data_values>[,levels=<level_values>])

Each level is
associated with a
integer value

18

Factor creation :

Levels are not selected in a


alphabetical order.

19

There are two missing values NA and NaN

is.na() : test for a NA value. It returns TRUE logical value for the NA and NaN values
is.nan() : test if a value is not a number. It returns TRUE logical value for the NaN value

20

Data frames stores tabular data

each column is of same length & can be of different types


There is a special attributes called row.names
To covert a matrix to data frame use method data.matrix()

21

Normally done through read.csv() and read.table() functions


Both the functions read rows and columns and returns a data frame
readline() function is used to read line by line from a text file and return a character vector.

source() loads predefined function form a second file to console


Load() and unserialize load binary objects

22

read.table() loads from a text


file

read.csv() loads from a coma


separated excel file

23

Normally done through write.csv() and write.table() functions


Both the functions writes rows and columns
writeline() function is used to read line by line to a text file

dump() writes predefined function to a second file from console


save() and serialize() saves binary objects

24

file = file name needed to be read


header = logical value to indicate if there is a file header present or not
sep = indicates the separator used in the file
colClasses = data types of the columns
nrows = number of rows needed to read
comment.char = for comment cahrecters
skip = integer value indicating number of rwos needed to skip from top
stringsAsFactors = logical: should character vectors be converted to factors
25

Make a rough estimate of the memory needed and check the RAM requirements
Set comment.char = in case there are no comments
Set colClasses so that R dont have determine the data types in runtime

26

Dput() stores data in a text file along with metadata


More meta data about classes of each column
This data is editable and can be obtained by source() or dget()

27

Dump() is used to store multiple objects

File structure

28

Example with a function

File structure

29

Abstracts out the mechanism to connect to a file


File = regular uncompressed file
Gzfile = compressed .gz file
Bzfile = compressed .bz file
url = from a web source
r = read mode
a = append mode

w = write mode
rb , wb , ab = read, write and append on binary file

30

Used for conditional execution of R expressions


Syntax:

if (<logical_condition>){
## Executable statement 1
}
else if (<logical_condition>){

## Executable statement 2
}
else{

If the logical condition is


tue then the executable
statement 1 is executed
Else the next condition is
evaluated
If none of the condition is
TRUE then the executable
statement 3 is executed

## Executable statement 3
}
31

As the logical condition is


true thus the value of y is
10

32

The previous expression is


same as this one
This gives better visibility

33

Used when a set of statements are needed to repeat a fixed number of times
Syntax:

Type1:

Type 2 :

for(<range>){

for(<var> in seq_along(<var>)){

## Executable statement

## Executable statement

Type 3 :

Type 4 :

for(<var> in <var>){

for (<var> in seq_len(<var>)){

## Executable statement

## Executable statement
}

}
34

Here the range is from 1 to


10
For each time the
expression print(i) is
executed as shown in the
output

35

Here the seq_along()


gives the integer
sequence of the number
of elements in the
vector x

36

Here the sequence runs


for each elements
present in the vector x

37

Here is an
example of nested
for loop
This loop loops
around all the
elements of matrix
x and prints them

38

Used for repeated execution of R expressions depending on a logical condition


Syntax:

while(<logical_condition>){
## Executable statement
}

The executable statements


are executed till the logical
condition is false
The statement must affect/
change the variables
effecting the logical
condition else it will result
in an infinite loop
Loop terminating condition
can be a break statement

39

Here the loop is executed


till the condition is
evaluated to false
The count variable is
increased so that it does
not lead to a infinite loop

40

Used for repeated execution of R expressions


Syntax:

repeat{
## Executable statement
}

The executable statements


are executed infinitely till
the loop termination
statement is reached
Loop terminating condition
can be a break statement

41

Here the loop is executed


till the break statement is
reached

The loop results in an


infinite loop in absence of
the break statement and
the increment of count
variable

42

Break is used when we need to terminate a loop


Next is used when we need to skip the rest of the statements

43

Syntax:

<function_name> <- function(<arg1>,,<argN>){


## Executable statements
}
Function name is the name of function
Arg1 to argN are the arguments passed to
the function

The last expression is the return value of


the functions

44

Example:

Function name is x

a and b are the arguments passed


to the function
The function calculated the value
of a+b and returns the value
X(2,3) calls the function with
arguments 2 and 3

45

Syntax:

<function_name> <- function(<arg1>,,<argN>=<value>){


## Executable statements
}
Here the argument N have a default value
Thus if no value is passed to the variable
in case of calling the function, it will take
the default value

If a value is passed during calling, then


the default value will not be used.
46

Example:

a and b are the arguments passed


to the function. b have a default
value of 2
Thus when called with only a
value of 5 , the default value of b
is taken and output is 7

X(5,10) is called then the value 10


overwrites the default value of the
argument b and the default value
is not used.

47

Das könnte Ihnen auch gefallen