March, 2018

Conditional Statement

Bitwise operation

  • And: &
  • Or: |
  • not: !
TRUE & FALSE
## [1] FALSE
TRUE | FALSE
## [1] TRUE
!TRUE
## [1] FALSE

Bitwise opertion

  • Example
(3>2)&(2>3)
## [1] FALSE
(3>2)|(2>3)
## [1] TRUE
!(2>3)
## [1] TRUE

Conditional statement 1

  • 'If statement' consists of three parts: If (condition) {statement}
  • Only when the condition is true, the statement is implemeted.
a = 1
b = 0
if (a<2)
{
  b=b+1
} 

Conditional statement 2

  • 'If ~ else ~ statement' consists of four parts: If (condition) {statement-1} else {statement-2}.
  • If the condition is true then the statment-1 is implemented. Otherwise the statement-2 is implemented.
a = 3
b = 0
if (a<2)
{
  b = b+1
} else {
  b = b-1
}

Loop

  • 'For' statement consists of three parts: for (index argument) {statement}
  • The index argment usually written as (variable in vector)
  • The statement is iteratively implemented #(the length of vector) times.
  • In the jth iteration, the value of the variable is given by vector[j].

Loop

x = 0
for (i in 1:3)
{
  x = x + i 
}
  • The statement is repeated three times.
  • At the end of the implemetation, the value of variable \(i\) is 3.

Loop

x = 0
v = c(2,4,6,8)
for (i in v)
{
  x = x + i
}
  • The statement is repeated four times.
  • At the third iteration, the value of \(i\) is 6 (v[3]).

Loop

  • The loop statement can be doubly appied.
x = 0
for (i in 1:3)
{
  for (j in 1:2)
  {
    x = x + i + j
  }
}
  • In this case the inner loop is the statement of outer loop.

Loop

  • Control the loop in the for statement.
  • break: stop the loop including the break statement.
  • next: pass the current statement in the loop.
  • stop: stop the all loops including the stop statement.

(HW) Make examples to use the control arguments in R.

function

create function

function()

  • function makes the object of function that implements a set of codes.
  • function consists two parts function(arguements){statement}
  • In the end of statement function can return the value of objects or an object itselt by using return()
  • When a variable is defined in the function, the variable is not referred in the outside of function.
  • If a variable not defined in the function is called in the function, the names of variables is referred in the outside of the function. (Be careful!) If the variable is not defined outside, the error message is returned.

create function

function()

testFunction = function(x1,x2)
{
  v = x1^2 + x2
  return(v)
}
testFunction(x1=1,x2=2)
## [1] 3

create function

function()

testFunction = function(x1,x2)
{
  v1 = x1^2 + x2
  v2 = x1^2 -x2
  return( c(v1,v2) )
}
testFunction(x1=1,x2=2)
## [1]  3 -1

create function

function()

testFunction = function(x)
{
  v = x^2 + y
  return(v)
}
testFunction(x)
y = 1
testFunction(x)
y = 10
testFunction(x)

create function

function()

  • function can return the list
testFunction = function(x)
{
  v1 = x^2
  v2 = matrix((1:5)^x,1,5)
  return(list(first = v1, second = v2))
}
testFunction(2)
## $first
## [1] 4
## 
## $second
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    4    9   16   25

create function

Example: column average function

s_colMean = function(x)
{
  if ( class(x) != "matrix") stop()
  v = rep(0, ncol(x))
  for ( i in 1:ncol(x))
  {
    v[i] = mean( x[i,] )
  }
  return(v)  
}

create function

HW: rowwise average function

File

path name

absolute path name/ relative path name절대경로, 상대 경로

getwd()
## [1] "C:/Users/Jeon/Dropbox/class/2018 EDA/week_4/doc_code"
  • set a relative path name: ./ (current directory), ../ (upper directory)
  • ex) describe the relative path name. ./Rplot.jpg, ..../block/source/1.png
setwd('./fig')
  • explain the above code.

read files

`read.table'

  • type ?read.table and explain this function.
A = read.table("CO2.dat", header = TRUE, sep = " ",
               stringsAsFactors= FALSE)
head(A)
class(A$Plant)
  • Note that data of CO2.dat are separated by comma.
  • By default read.table converts character data into factor. By using stringsAsFactors = F the function can read the character data without the conversion.

read files

read xlsx file.

install.packages('xlsx')
library(xlsx)
A = read.xlsx("CO2.xlsx", header = TRUE, sheetIndex = 1, 
              stringsAsFactors= FALSE)
head(A)
class(A$Plant)