Skip to main content

Posts

Showing posts with the label unique()

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