Yuriy
MFE Alum
- Joined
- 2/1/05
- Messages
- 694
- Points
- 28
In this thread, I will be posting interesting R examples that will give you a flavor of what R is. You can download R for free at http://www.r-project.org (there was a discussion about downloading R or S-Plus elsewhere on the forum).
One good thing about R is that it is a free program and if you need to do something, chances are that someone has already done something similar and you can find an open source code somewhere on the Internet.
R is a console application where you write your script and it may or may not be processed as you type it. All variables and functions that you create in your code become objects and can be used at any time later on (but once you close R you lose your objects).
Comments in R:
=> Comments in R are preceded by # on a given line. For example,
# This line is a comment and will be ignored
Variables:
=> To create variables and assign values to them, you do the following:
x = 5
# variable x is now equal to 5, to output the value of x on the screen type
x
Vectors:
=> To create vectors do the following
y = c(1,3,5,7,9,11)
# vector y is now [1, 3, 5, 7, 9, 11]
y
Matrices:
=> To create a matrix do the following
z = matrix(y,3,2,T)
z
# this creates a 3 by 2 matrix from vector y packing the numbers row by row
z = matrix(y,3,2,F)
# this creates a 3 by 2 matrix from vector y packing the numbers column by column
z
Help:
=> Every built in function has explanation in the help file, to access help type
help("matrix")
Matrix Transpose:
=> A transpose of a matrix is
z1 = t(z)
z1
Matrix Multiplication:
=> Multiplication of matrices is done using %*%
z1=z # make a copy of matrix z
z2=t(z) # set z2 equal to the transpose of z
product = z1%*%z2 # multiply the two matrices
product
Inverse of a Matrix:
=> To invert a matrix use the function solve()
A = matrix(c(1,0,2,1),2,2,T) # you can type in the vector directly into the formula
B = solve(A)
Initialize a Matrix:
=> To initialize a matrix use, for example, the command rep() that will repeat a value a certain number of times
C = matrix(rep(0,9),3,3)
# if you don't specify how you want to pack the matrix, the default 'by column' is used
C
# you get a 3 by 3 matrix of zeros
Creating a Sequence:
=> Sequences of numbers can be created in different ways, one way is
s = seq(0,1,0.1)
# a sequence from 0 to 1 with step size 0.1
s
Now to generating random numbers...
One good thing about R is that it is a free program and if you need to do something, chances are that someone has already done something similar and you can find an open source code somewhere on the Internet.
R is a console application where you write your script and it may or may not be processed as you type it. All variables and functions that you create in your code become objects and can be used at any time later on (but once you close R you lose your objects).
Comments in R:
=> Comments in R are preceded by # on a given line. For example,
# This line is a comment and will be ignored
Variables:
=> To create variables and assign values to them, you do the following:
x = 5
# variable x is now equal to 5, to output the value of x on the screen type
x
Vectors:
=> To create vectors do the following
y = c(1,3,5,7,9,11)
# vector y is now [1, 3, 5, 7, 9, 11]
y
Matrices:
=> To create a matrix do the following
z = matrix(y,3,2,T)
z
# this creates a 3 by 2 matrix from vector y packing the numbers row by row
z = matrix(y,3,2,F)
# this creates a 3 by 2 matrix from vector y packing the numbers column by column
z
Help:
=> Every built in function has explanation in the help file, to access help type
help("matrix")
Matrix Transpose:
=> A transpose of a matrix is
z1 = t(z)
z1
Matrix Multiplication:
=> Multiplication of matrices is done using %*%
z1=z # make a copy of matrix z
z2=t(z) # set z2 equal to the transpose of z
product = z1%*%z2 # multiply the two matrices
product
Inverse of a Matrix:
=> To invert a matrix use the function solve()
A = matrix(c(1,0,2,1),2,2,T) # you can type in the vector directly into the formula
B = solve(A)
Initialize a Matrix:
=> To initialize a matrix use, for example, the command rep() that will repeat a value a certain number of times
C = matrix(rep(0,9),3,3)
# if you don't specify how you want to pack the matrix, the default 'by column' is used
C
# you get a 3 by 3 matrix of zeros
Creating a Sequence:
=> Sequences of numbers can be created in different ways, one way is
s = seq(0,1,0.1)
# a sequence from 0 to 1 with step size 0.1
s
Now to generating random numbers...