# NTU Computer Programming - HW05 # Name: ³s¤p¤O # ID: B01931001 # Subject: Collection of my own functions # Date: 3/12, 2017 # determine the normalization of x # updated on 3/13, 2017 myNormal <- function( x ) { value <- x return( value ) } # determine the sing of x # updated on 3/13, 2017 mySign <- function( x ) { if ( x < 0 ) { value <- -1 } else if ( x == 0 ) { value <- 0 } else { value <- 1 } return(value) } # determine the sum of x # updated on 3/13, 2017 mySum <- function( x ) { Num <- length( x ) temp_sum <- 0 for( k in 1:Num ) { temp_sum <- temp_sum + x[ k ] } return( temp_sum ) } # determine the mean of x # updated on 3/13, 2017 myMean <- function( x ) { Num <- length( x ) temp_sum <- 0 for( k in 1:Num ) { temp_sum <- temp_sum + x[ k ] } return( temp_sum/Num ) } # determine the sd, standard deviation of x # updated on 3/13, 2017 mySD <- function( x ) { Num <- length( x ) temp_sum <- 0 temp_mean <- 0 temp_diff <- rep(0, length.out = Num) for( k in 1:Num ) { temp_sum <- temp_sum + x[ k ] } temp_mean <- temp_sum/Num for( k in 1:Num ) { temp_diff[ k ] <- ( x[ k ] - temp_mean )^2 } temp_sum_diff <- mySum( temp_diff ) temp_SD <- sqrt( temp_sum_diff / ( Num - 1 ) ) return( temp_SD ) } # sort x # updated on 3/13, 2017 mySort <- function( x ) { itemCount <- length( x ) repeat { hasChanged <- FALSE itemCount <- itemCount - 1 if ( itemCount >= 1 ){ for( k in 1 : itemCount ) { if ( x[ k ] > x[ k+1 ] ) { t <- x[ k ] x[ k ] <- x[ k+1 ] x[ k+1 ] <- t hasChanged <- TRUE } print( c( k , x ) ) } } if ( !hasChanged ) break; } return( x ) } # determine the max of x # updated on 3/13, 2017 myMax <- function( x ) { Num <- length( x ) temp <- mySort( x ) return( temp[ Num ] ) } # determine the min of x # updated on 3/13, 2017 myMin <- function( x ) { Num <- length( x ) temp <- mySort( x ) return( temp[ 1 ] ) } # determine the range of x # updated on 3/13, 2017 myRange <- function( x ) { temp_max <- myMax( x ) temp_min <- myMin( x ) temp <- c( temp_min, temp_max ) return( temp ) } # determine the median (50%) of x # updated on 3/13, 2017 myMedian <- function( x ) { Num <- length( x ) x_sort <- mySort( x ) if( (Num-1)/2 == floor( (Num-1)/2 ) ){ temp_median <- x_sort[ (Num-1)/2+1 ] } else { temp <- floor( Num/2 ) temp_median <- ( x_sort[ temp ] + x_sort[ temp+1 ] )/2 } return( temp_median ) } # determine the first quantile (25%) of x # updated on 3/13, 2017 my25p <- function( x ) { tp_num <- length( x ) x_sort <- mySort( x ) tp_max <- myMax( x ) tp_min <- myMin( x ) tp1 <- floor ( (tp_num-1) * 0.25 ) +1 tp2 <- ( (tp_num-1) * 0.25 ) +1 tp3 <- ceiling ( (tp_num-1) * 0.25 ) +1 if( tp2 == tp1 ){ tp_25p <- x_sort[ tp1 ] } else { tp_25p <- x_sort[ tp1 ] + ( (tp2-tp1)/(tp3-tp1) )*( x_sort[tp3]-x_sort[tp1] ) } return( tp_25p ) } # determine the third quantile (75%) of x # updated on 3/13, 2017 my75p <- function( x ) { tp_num <- length( x ) x_sort <- mySort( x ) tp_max <- myMax( x ) tp_min <- myMin( x ) tp1 <- floor ( (tp_num-1) * 0.75 ) +1 tp2 <- ( (tp_num-1) * 0.75 ) +1 tp3 <- ceiling ( (tp_num-1) * 0.75 ) +1 if( tp2 == tp1 ){ tp_75p <- x_sort[ tp1 ] } else { tp_75p <- x_sort[ tp1 ] + ( (tp2-tp1)/(tp3-tp1) )*( x_sort[tp3]-x_sort[tp1] ) } return( tp_75p ) } # determine the summary of x # updated on 3/13, 2017 mySummary <- function( x ) { tp_summary <- c( myMin( x ), my25p( x ), myMedian( x ), myMean( x ), my75p( x ), myMax( x ) ) return( tp_summary ) }