1. DATA FILES, BLOCKS OF
EXPRESSIONS, CYCLES AND
FUNCTIONS IN R
Vladimir Bakhrushin,
Professor, D.Sc. (Phys. & Math.)
Vladimir.Bakhrushin@gmail.com
2. 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.
4. 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.
5. Writing data to a file
write.table(a2, file = "a2.csv", sep =
" ", dec = ".")
6. 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]].
10. 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
11. Conditional constructs
“If” – “else” statement is an example of conditional construct:
if (<condition>)
{<expression_1>
}else{
<expression_2>}
12. 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>
}
14. 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.
15. 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>).
18. 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