Ordering and Tabulating
Purpose
I came across 350 commonly used functions by Hadley Wickham. I thought atleast I should know the basic vocabulory of R. Going over the list, I figured out that there are quite a number of functions that I have no clue on
Ordering and tabulating
I will code atleast a few lines of code duplicated, unique, merge,order, rank, quantile, sort,table, ftable
duplicated function
> x <- c(9:20, 1:5, 3:7, 0:8) > cbind(x, duplicated(x)) x [1,] 9 0 [2,] 10 0 [3,] 11 0 [4,] 12 0 [5,] 13 0 [6,] 14 0 [7,] 15 0 [8,] 16 0 [9,] 17 0 [10,] 18 0 [11,] 19 0 [12,] 20 0 [13,] 1 0 [14,] 2 0 [15,] 3 0 [16,] 4 0 [17,] 5 0 [18,] 3 1 [19,] 4 1 [20,] 5 1 [21,] 6 0 [22,] 7 0 [23,] 0 0 [24,] 1 1 [25,] 2 1 [26,] 3 1 [27,] 4 1 [28,] 5 1 [29,] 6 1 [30,] 7 1 [31,] 8 0 > cbind(rev(x), duplicated(x, fromLast = T)) [,1] [,2] [1,] 8 0 [2,] 7 0 [3,] 6 0 [4,] 5 0 [5,] 4 0 [6,] 3 0 [7,] 2 0 [8,] 1 0 [9,] 0 0 [10,] 7 0 [11,] 6 0 [12,] 5 0 [13,] 4 1 [14,] 3 1 [15,] 5 1 [16,] 4 1 [17,] 3 1 [18,] 2 1 [19,] 1 1 [20,] 20 1 [21,] 19 1 [22,] 18 1 [23,] 17 0 [24,] 16 0 [25,] 15 0 [26,] 14 0 [27,] 13 0 [28,] 12 0 [29,] 11 0 [30,] 10 0 [31,] 9 0 |
unique
> unique(x) [1] 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 0 8 > unique(x, fromLast = T) [1] 9 10 11 12 13 14 15 16 17 18 19 20 0 1 2 3 4 5 6 7 8 |
merge
> authors <- data.frame(surname = I(c("Tukey", "Venables", "Tierney", + "Ripley", "McNeil")), nationality = c("US", "Australia", + "US", "UK", "Australia"), deceased = c("yes", rep("no", 4))) > books <- data.frame(name = I(c("Tukey", "Venables", "Tierney", + "Ripley", "Ripley", "McNeil", "R Core")), title = c("Exploratory Data Analysis", + "Modern Applied Statistics ...", "LISP-STAT", "Spatial Statistics", + "Stochastic Simulation", "Interactive Data Analysis", "An Introduction to R"), + other.author = c(NA, "Ripley", NA, NA, NA, NA, "Venables & Smith")) > merge(authors, books, by.x = "surname", by.y = "name") surname nationality deceased title other.author 1 McNeil Australia no Interactive Data Analysis <NA> 2 Ripley UK no Spatial Statistics <NA> 3 Ripley UK no Stochastic Simulation <NA> 4 Tierney US no LISP-STAT <NA> 5 Tukey US yes Exploratory Data Analysis <NA> 6 Venables Australia no Modern Applied Statistics ... Ripley > merge(authors, books, by.x = "surname", by.y = "name", all = T) surname nationality deceased title other.author 1 McNeil Australia no Interactive Data Analysis <NA> 2 R Core <NA> <NA> An Introduction to R Venables & Smith 3 Ripley UK no Spatial Statistics <NA> 4 Ripley UK no Stochastic Simulation <NA> 5 Tierney US no LISP-STAT <NA> 6 Tukey US yes Exploratory Data Analysis <NA> 7 Venables Australia no Modern Applied Statistics ... Ripley > x <- data.frame(k1 = c(NA, NA, 3, 4, 5), k2 = c(1, NA, NA, 4, + 5), data = 1:5) > y <- data.frame(k1 = c(NA, 2, NA, 4, 5), k2 = c(NA, NA, 3, 4, + 5), data = 1:5) > x k1 k2 data 1 NA 1 1 2 NA NA 2 3 3 NA 3 4 4 4 4 5 5 5 5 > y k1 k2 data 1 NA NA 1 2 2 NA 2 3 NA 3 3 4 4 4 4 5 5 5 5 > merge(x, y, by = c("k1", "k2")) k1 k2 data.x data.y 1 4 4 4 4 2 5 5 5 5 3 NA NA 2 1 > merge(x, y, by = c("k1", "k2"), incomparables = NA) k1 k2 data.x data.y 1 4 4 4 4 2 5 5 5 5 3 NA NA 2 1 > merge(x, y, by = "k1") k1 k2.x data.x k2.y data.y 1 4 4 4 4 4 2 5 5 5 5 5 3 NA 1 1 NA 1 4 NA 1 1 3 3 5 NA NA 2 NA 1 6 NA NA 2 3 3 > merge(x, y, by = "k2", incomparables = NA) k2 k1.x data.x k1.y data.y 1 4 4 4 4 4 2 5 5 5 5 5 |
order , sort ,quantile, table
Have used it numerous times
ftable
> y <- mtcars[c("cyl", "vs", "am", "gear")] > x <- ftable(y) > x gear 3 4 5 cyl vs am 4 0 0 0 0 0 1 0 0 1 1 0 1 2 0 1 0 6 1 6 0 0 0 0 0 1 0 2 1 1 0 2 2 0 1 0 0 0 8 0 0 12 0 0 1 0 0 2 1 0 0 0 0 1 0 0 0 |