Thursday, August 10, 2017

unique() {base}


unique() function is a generic function that extracts unique values from a vector, array or data frame.
The parameters of the function are: x: vector, array or data frame to remove duplicated values fromLast:logical indicating if duplication should be considered from the last
#`unique()` function using vectors:
x <- c(10 + 0:5, 1:5, 8:1)
x
##  [1] 10 11 12 13 14 15  1  2  3  4  5  8  7  6  5  4  3  2  1
u1 <- unique(x)
u1
##  [1] 10 11 12 13 14 15  1  2  3  4  5  8  7  6
u2 <- unique(x,  fromLast = TRUE) # different order
u2
##  [1] 10 11 12 13 14 15  8  7  6  5  4  3  2  1
y <- c(5:1,8:1, 10, 1:3)
y
##  [1]  5  4  3  2  1  8  7  6  5  4  3  2  1 10  1  2  3
u3 <- unique(y)
u3
## [1]  5  4  3  2  1  8  7  6 10
u4 <- unique(y,  fromLast = TRUE) # different order
u4
## [1]  8  7  6  5  4 10  1  2  3
#`unique()` function with data frames:
dim(ChickWeight)
## [1] 578   4
head(ChickWeight)
##   weight Time Chick Diet
## 1     42    0     1    1
## 2     51    2     1    1
## 3     59    4     1    1
## 4     64    6     1    1
## 5     76    8     1    1
## 6     93   10     1    1
nrow(unique(ChickWeight))
## [1] 578
unique(ChickWeight$Diet)
## [1] 1 2 3 4
## Levels: 1 2 3 4
length(unique(ChickWeight$weight))
## [1] 212

No comments:

Post a Comment

duplicated() {base}

duplicated()  function determines which elements are duplicated and returns a logical vector. The parameters of the function are:   ...