?array
array( 1:12 )
array( , c(3,4) )
array( 1:12, c(3,4) )
array( data = 1:12, dim = c(3,4) )
args( array )
array( data = 1:60, dim = c(3,4,5) )
x <- c( 1, 8, 5, 2, 3, 1 )
x
length( x )
diff( x )
sum( x )
prod( x )
max( x )
min( x )
which.max( x )
which.min( x )
?max
?min
range( x )
round( x*pi )
round( x )
round( x*pi )
x*pi
round( x*pi )
round( x*pi, 2 )
cumsum( x )
?cumsum( )
cumprod( x )
x
unique( x )
x
?unique
x
mean( x )
median( x )
y <- c( x, 3)
y
median( y )
y <- c( x, 9)
y
median( y )
sort( y )
sort( x )
var( x )
sd( x )
summary( x )
sort( x )
rank( x )
?rank
x
rank( x, ties.method = "first" )
x
rank( x, ties.method = "average" )
rank( x, ties.method = "last" )
rank( x, ties.method = "random" )
rank( x, ties.method = "max" )
rank( x, ties.method = "min" )
order( x )
?order
order( x, decreasing = TRUE )
x
order( x )

x[ order( x ) ]
order( x )
x
order( x )[ 3 ]
rank( x )
x <- c( 1.2, -3.4, 5.7, -6, 0, 3 )
x
rank( x )
sort( x )
sort( x )[ 3 ]
rank( x ) == 3
which( rank( x ) == 3 )
order( x )
order( x )[ 3 ]
x[ order( x )[ 3 ] ]
x
sort( x )
sort( x, decreasing = TRUE )
rev( sort( x ) )
rank( x )
rev( rank( x ) )
order( x )
order( x, decreasing = TRUE )
rev( order( x ) )
x <- c( 2.5, 2.5, 2.5, 2.5, 2.3, 4.7, -2.2, 4.6, 4.6 )
sor( x )
sort( x )
rank( x )
rank( x, ties.method = "average" )
rank( x, ties.method = "first" )
rank( x, ties.method = "random" )
rank( x, ties.method = "max" )
rank( x, ties.method = "min" )
rank( x )
## matrix function
?apply
?sweep
A <- matrix( 1:12, nrow = 4, ncol = 3 )
A
apply( A, MARGIN = 1, FUN = mean )
apply( A, MARGIN = 2, FUN = mean )
apply( A, MARGIN = 2, FUN = max )
apply( A, MARGIN = 1, FUN = max )
apply( A, MARGIN = 2, FUN = sum )
apply( A, MARGIN = 1, FUN = sum )
apply( A, MARGIN = 1, FUN = function(x) sd(x) / mean(x) )
apply( A, MARGIN = 2, FUN = function(x) sd(x) / mean(x) )
apply( A, MARGIN = 1, FUN = sd )
apply( A, MARGIN = 2, FUN = sd )
apply( A, MARGIN = 2, FUN = mean )
apply( A, MARGIN = 2, FUN = function(x) sd(x) / mean(x) )
1.290994/2.5
myfunc <- function( x, c1, c2) c(  mean( x[c1] ), sum( x[c2] ) )
myfunc
apply( A, MARGIN = 2, FUN = myfunc )
apply( A, MARGIN = 2, FUN = myfunc, c1 = c(1,2), c2 = c(2,3) )
A <- array( 1:24, dim = c(4, 3, 2) )
A
apply( A, MARGIN = 1, FUN = sum  )
apply( A, MARGIN = 2, FUN = sum  )
apply( A, MARGIN = c(1,2), FUN = sum  )
A
?sweep
A <- matrix( 1:12, nrow = 4, ncol = 3 )
A
?matrix
A <- matrix( 1:12, nrow = 4, ncol = 3, byrow = TRUE )
A
A <- matrix( 1:12, nrow = 4, ncol = 3 )
A
sweep( A, MARGIN = 2, STATS = 1:3, FUN = "+" )
sweep( A, MARGIN = 2, STATS = c(100, 200, 300), FUN = "+" )
A

u <- apply( A, MARGIN = 2, FUN = mean )
u
w <- sweep( A, MARGIN = 2, STATS = u, FUN = "-" )
w
u <- apply( A, MARGIN = 2, FUN = mean )
v <- apply( A, MARGIN = 2, FUN = sd )
w <- sweep( A, MARGIN = 2, STATS = u, FUN = "-" )
z <- sweep( A, MARGIN = 2, STATS = v, FUN = "/" )
z
scale( A )
myfunc <- function( x ) {
u <- apply( x, MARGIN = 2, FUN = mean )
v <- apply( x, MARGIN = 2, FUN = sd )
w <- sweep( x, MARGIN = 2, STATS = u, FUN = "-" )
z <- sweep( w, MARGIN = 2, STATS = v, FUN = "/" )
return( z )
}
myfunc( A )
scale( a )
scale( A )
z <- sweep( w, MARGIN = 2, STATS = v, FUN = "/" )
z
?with
?tapply
?sapply
?tapply
CO2
summary( CO2 )
with( CO2, tapply( uptake, INDEX = Type, FUN = mean ) )
with( CO2, tapply( uptake, INDEX = Treatment, FUN = mean ) )
with( CO2, tapply( uptake, INDEX = list( Type, Treatment), FUN = mean ) )
apply( CO2[, 4:5], MARGIN = 2, FUN = mean )
sapply( CO2[ , 4:5 ], FUN = mean )
lapply( CO2[ , 4:5 ], FUN = mean )
?sort

bubble_sort <- function( array )
{ array }

A
bubble_sort( A )
bubble_sort <- function(array) {
bubble_sort <- function(array) {
count <- 0
while(1){
count_swaps <- 0
for (j in 1 : (length(array) - 1 - count))
{if (array[j] > array[j + 1])
{
s <- array[j]
array[j] <- array[j+1]
array[j+1] <- s
count_swaps <- count_swaps + 1
}
}
count <- count + 1
if(count_swaps == 0) break
}
array
}
bubble_sort <- function(array) {
count <- 0
while(1){
count_swaps <- 0
for (j in 1 : (length(array) - 1 - count))
{if (array[j] > array[j + 1])
{
s <- array[j]
array[j] <- array[j+1]
array[j+1] <- s
array[j+1] <- s
count_swaps <- count_swaps + 1
}
}
count <- count + 1
if(count_swaps == 0) break
}
array
}
bubble_sort( x )
bubble_sort <- function(array) {
count <- 0
while(1){
count_swaps <- 0
for (j in 1 : (length(array) - 1 - count))
{if (array[j] > array[j + 1])
{
s <- array[j]
array[j] <- array[j+1]
array[j+1] <- s
count_swaps <- count_swaps + 1
}
}
count <- count + 1
if(count_swaps == 0) break
}
array
}
x
bubble_sort( x )
sort( x )
myNormalization <- function( x ) {
u <- apply( x, MARGIN = 2, FUN = mean )
v <- apply( x, MARGIN = 2, FUN = sd )
w <- sweep( x, MARGIN = 2, STATS = u, FUN = "-" )
z <- sweep( w, MARGIN = 2, STATS = v, FUN = "/" )
return(z)
}
myNormalization( A )
z
scale( A )
sgn
sgn <- function( x ) {
if ( x < 0 ) {
value <- -1
} else if ( x == 0 ) {
value <- 0
} else {
value <- 1
}
return(value)
}
sgn( -1 )
sgn( -2 )
sgn( 3 )
sgn( 0 )
sgn( c( 2, -3, 0 ))
sgnvec <- function( x ) {
n <- length( x )
value <- integer( n )
for ( i in 1:n ) {
if ( x[i] < 0 ) {
value[i] <- -1
} else if ( x[i] == 0 ) {
value[i] <- 0
} else {
value[i] <- 1
}
}
return( value )
}
sgnvec( c( 2, -3, 0 ))
x <- c( -2, -5, -7, -9, 12, 17, -18 )
myRange <- function( x ) {
if ( length(x) == 1 ) {
mymin <- x[1]; mymax <- x[1]
} else {
if ( x[1] < x[2] ) {
mymin <- x[1]; mymax <- x[2]
} else {
mymin <- x[2]; mymax <- x[1]
}
if ( length(x) > 2 ) {
for ( i in 3:length(x) ) {
if ( x[i] < mymin )   mymin  <- x[i]
if ( x[i] > mymax )   mymax <- x[i]
}
}
}
return( list( mymin = mymin, mymax = mymax ) )
}
myRange( c( 1, 2, 4 ))
center <- function( x, type ) {
switch( type,
mean = mean( x ),
median = median( x ),
trimmed.mean = mean( x, trim = 0.2 ) )
}
set.seed( 1)
x <- rcauchy( 10 )
x

center( x, "mean" )
center( x, "median" )
center( x, "trimmed.mean" )
fx <- function(x) x^3 * cos(x) - 2 * x^2 * sin(x) + 5 * x - 1
fx( -5 )
fx( 0 )
fx( 5 )
fx( c( -5, 0, 5 ) )
fxy <- function( x, y ) x^3 * cos(y) - 2 * x^2 * sin(y) + 5 * x - 1
fxy( -5, pi )
fxy( 1, pi )
fxy( 1, pi/2 )
fxy( 0, pi/6 )
fxyz <- function( x, y, z ) x^3 * cos(y) - 2 * x^2 * sin(z) + 5 * x - 1
fxyz( -5, pi, pi )
fxyz( -5, pi, pi/2 )
fxyz( 1, pi, pi/2 )
fxyz( 0, pi/6, 3*pi )
