Skip to main content

Posts

Showing posts with the label generic function

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...

which() function in R

which()  function is a generic function that returns the position of a logical object. The parameters of the function are:   x : vector or array   arr.ind : logical, to decide if the indices should be returned in an array   ind : integer-valued index vector x = c ( 1 , 1 , 1 , 1 , 2 ) x ## [1] 1 1 1 1 2 which ( x == 2 ) ## [1] 5 summary ( chickwts ) ## weight feed ## Min. :108.0 casein :12 ## 1st Qu.:204.5 horsebean:10 ## Median :258.0 linseed :12 ## Mean :261.3 meatmeal :11 ## 3rd Qu.:323.5 soybean :14 ## Max. :423.0 sunflower:12 which ( chickwts $ feed == 'horsebean' ) ## [1] 1 2 3 4 5 6 7 8 9 10 which ( chickwts $ feed == 'soybean' ) ## [1] 23 24 25 26 27 28 29 30 31 32 33 34 35 36 x = matrix ( c ( 2 , 3 , 2 , 3 , 1 , 1 , 1 , 1 , 2 , 3 ) , ncol = 5 ) x ## [,1] [,2] [,3] [,4] [,5] ## [1,] 2 2 1 1 2 ## [2,] 3 3 1 1 3 #which are t...

head() and tail() functions in R

head()  and  tail()  functions: head()  function displays the first values from a data frame, matrix or vector, while   tail()  function displays the last values. Parameters:   x :object to display the values from   n :number of values to be displayed, default values is 6. #head() and tail() functions with a vector head ( 1 : 20 ) ## [1] 1 2 3 4 5 6 head ( 1 : 20 , n = 10 ) #10 first rows in the vector ## [1] 1 2 3 4 5 6 7 8 9 10 head ( 1 : 20 , n = - 10 ) #all the rows in the vector but the LAST 10!! ## [1] 1 2 3 4 5 6 7 8 9 10 tail ( 1 : 20 ) ## [1] 15 16 17 18 19 20 tail ( 1 : 20 , n = 10 ) #10 first rows ## [1] 11 12 13 14 15 16 17 18 19 20 tail ( 1 : 20 , n = - 5 ) ##all the rows in the vector but the FIRST 10!! ## [1] 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #head() and tail() functions with a matrix A = head ( matrix ( 1 : 50 , ncol = 5 ) , n = 3 ) #assign...