files,blocks and functions in r

18

Click here to load reader

Upload: vladimir-bakhrushin

Post on 01-Jul-2015

118 views

Category:

Education


3 download

DESCRIPTION

Presentation on using data files, data frames, lists, blocks of expressions and functions in R

TRANSCRIPT

Page 1: Files,blocks and functions in R

DATA FILES, BLOCKS OF EXPRESSIONS, CYCLES AND

FUNCTIONS IN R

Vladimir Bakhrushin,

Professor, D.Sc. (Phys. & Math.)[email protected]

Page 2: Files,blocks and functions in R

Data reading

> data<-read.table(“File name", sep=";", dec=",", header=TRUE, …)

first argument is a reference to the file of *.csv format, that contains data;

argument sep indicates, by which sign the data are separated; argument dec indicates decimal separator for numbers; argument header indicates whether the first cells of each

column are the column names; argument row.names indicates whether one of columns

contains the row names; argument nrows specifies the number of lines that need to be

read from the table.

Page 3: Files,blocks and functions in R

Data reading

Page 4: Files,blocks and functions in R

Writing data to a file

write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ", na = "NA", dec = ".", row.names = TRUE, col.names = TRUE, qmethod = c("escape", "double"), …)

x – object that must be written to the file (usually it is a matrix or data table);

file – name of the file to which the data must be written or other connections opened to writing;

append – argument that indicates the need to append data to an existing file or to create a new file with the same name;

quote – indicates the need to take in quotes marks of rows and columns;

na – character set used for the missing values.

Page 5: Files,blocks and functions in R

Writing data to a file

write.table(a2, file = "a2.csv", sep = " ", dec = ".")

Page 6: Files,blocks and functions in R

Lists

For saving of heterogeneous data in the one object lists can be used. For example:> x1 = c("Milk", "Yogurt", "Sour cream", "Curd")> x2 = c(1, 1, 14, 7)> x3 = c(TRUE, FALSE)> l1 = list(Text = x1, Number = x2, Logic = x3)> l1

To identify specific elements of the list such constructions can be used : l1$Text[3]; l1$Text; l1[[1]][2]; l1[[2]].

Page 7: Files,blocks and functions in R

Lists

Page 8: Files,blocks and functions in R

Data frames

Page 9: Files,blocks and functions in R

Data editing using function fix()

Page 10: Files,blocks and functions in R

Comments, blocks of expressions

Any comment begins with the character #. The test located in the line after this character not regarded as part of the program to be executed.

Expressions can be combined into blocks with curly braces. The result of the expressions block is the result of the last of them.

> {+ x = 9;+ y = 4;+ x + y;+ }[1] 13

Page 11: Files,blocks and functions in R

Conditional constructs

“If” – “else” statement is an example of conditional construct:if (<condition>){<expression_1>}else{<expression_2>}

Page 12: Files,blocks and functions in R

Loops

Loops with predetermined number of iterations can be organized using function “for”:

for (<variable> in <expression_1>)<expression_2>

If it is need to repeat iterations until a certain condition is fulfilled, we can use a loop “while”:

while (<condition>) {<expression>}

Page 13: Files,blocks and functions in R

Loops

Page 14: Files,blocks and functions in R

Loops

Infinite loops “Repeat” are used when a procedure should be executed if the interruption condition is not performed. Command “break” is used to interrupt the cycle, and command “next” – to interrupt the current iteration and move to the next one.

Page 15: Files,blocks and functions in R

Functions

Function in R is an object, that for a given set of arguments returns a certain value. Usually when the function is declared its value is assigned to some variable.

<variabe> = function(<arguments>) {<expression>}.

Subsequently, the function can be called using the construction:

<result> = <variable>(<arguments>).

Page 16: Files,blocks and functions in R

Functions

Page 17: Files,blocks and functions in R

Functions

Page 18: Files,blocks and functions in R

Literature

1. An Introduction to R

2. Introduction to the R Language: Functions

3. R Tutorial: An R introduction to statistics

4. Kelly Black. R Tutorial