Skip to main content

Posts

Showing posts with the label functions

abs() and sqrt() functions

abs()  function computes the absolute value of x, while  sqrt()  functions computes the square root of x. the only parameter for these functions is  x , which is the value to compute. abs ( 10 ) ## [1] 10 abs ( - 10 ) ## [1] 10 sqrt ( 10 ) ## [1] 3.162278 sqrt ( - 10 ) ## Warning in sqrt(-10): NaNs produced ## [1] NaN sqrt ( abs ( - 10 ) ) ## [1] 3.162278 plot ( 15 : - 15 , sqrt ( abs ( 15 : - 15 ) ) , col = "orange" ) lines ( spline ( 15 : - 15 , sqrt ( abs ( 15 : - 15 ) ) ) , col = "gold" )

duplicated() and unique() functions in R

duplicated()  function is a general function that determines which elements are duplicated, it returns a logical vector: The parameters of the function are:  x : vector, array or data frame   fromLast :logical indicating if duplication should be considered from the last x = c ( 5 : 1 , 5 : 1 , 5 ) x ## [1] 5 4 3 2 1 5 4 3 2 1 5 duplicated ( x ) ## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE duplicated ( x , fromLast = TRUE ) ## [1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE ## extract duplicated elements, those elements that duplicated(x) == TRUE, may be repeted elements: x [ duplicated ( x ) ] ## [1] 5 4 3 2 1 5 ## extract unique elements x [ ! duplicated ( x ) ] ## [1] 5 4 3 2 1 ## extract unique elements starting from the righmost value (different order): x [ ! duplicated ( x , fromLast = TRUE ) ] ## [1] 4 3 2 1 5 #duplicated using a data frmae duplicated ( iris ) ## [1] FALSE FALS...

Initial data analysis functions

Create a function in R: Function  function()  allows us to create specific functions. Between parenthesis we put the name of the parameters we want to create to be used in our function.     Function(parameter1, parameter2, ...){         statements         return(object) } NameFunction0 = function ( x , y ) { return ( ( x + y ) / 2 ) } NameFunction0 ( 4 , 5 ) ## [1] 4.5 If we want use a parameter that may or may not be specified we use  parameter = NULL : NameFunction1 = function ( x , y = NULL ) { if ( ! is.null ( y ) ) x = x + y return ( x ) } NameFunction1 ( 5 , 10 ) #with 2 paramers (x,y) ## [1] 15 NameFunction1 ( 5 ) #with only only parameter (x) ## [1] 5 Initial Data Analysis Functions Here we have created some basic functions that can be used for initial data analysis of our data: INLIERS  returns the values of a vector that are between the vales ...