March, 2018
&|!TRUE & FALSE
## [1] FALSE
TRUE | FALSE
## [1] TRUE
!TRUE
## [1] FALSE
(3>2)&(2>3)
## [1] FALSE
(3>2)|(2>3)
## [1] TRUE
!(2>3)
## [1] TRUE
a = 1
b = 0
if (a<2)
{
b=b+1
}
a = 3
b = 0
if (a<2)
{
b = b+1
} else {
b = b-1
}
variable in vector)x = 0
for (i in 1:3)
{
x = x + i
}
x = 0
v = c(2,4,6,8)
for (i in v)
{
x = x + i
}
x = 0
for (i in 1:3)
{
for (j in 1:2)
{
x = x + i + j
}
}
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()function makes the object of function that implements a set of codes.function consists two parts function(arguements){statement}return()function()testFunction = function(x1,x2)
{
v = x1^2 + x2
return(v)
}
testFunction(x1=1,x2=2)
## [1] 3
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
function()testFunction = function(x)
{
v = x^2 + y
return(v)
}
testFunction(x)
y = 1
testFunction(x)
y = 10
testFunction(x)
function()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
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)
}
getwd()
## [1] "C:/Users/Jeon/Dropbox/class/2018 EDA/week_4/doc_code"
./ (current directory), ../ (upper directory)./Rplot.jpg, ..../block/source/1.pngsetwd('./fig')
?read.table and explain this function.A = read.table("CO2.dat", header = TRUE, sep = " ",
stringsAsFactors= FALSE)
head(A)
class(A$Plant)
CO2.dat are separated by comma.read.table converts character data into factor. By using stringsAsFactors = F the function can read the character data without the conversion.install.packages('xlsx')
library(xlsx)
A = read.xlsx("CO2.xlsx", header = TRUE, sheetIndex = 1,
stringsAsFactors= FALSE)
head(A)
class(A$Plant)