Data Transformation with dplyr:: Cheatsheet dplyr functions work with pipes and expect tidy data. In tidy data: nnn III & pipes Each variable is in Each observation, or x%>%f(y) its own column case, is in its own row becomes f(x, y) Summarise Cases These apply summary functions to columns to create a new table. Summary functions take vectors as input and return one value (see back). S summary function summarise!.data, • ••) Compute table of summaries. Also summarised). summarise(mtcars, avg = mean(mpg)) count(x,wt = NULL, sort = FALSE) Count number of rows in each group defined by the variables in ... Also tally (). count(iris, Species) VARIATIONS summarise_all() - Apply funs to every column. summarise_at() - Apply funs to specific columns. summarise_if() - Apply funs to all cols of one type. Group Cases Use group_by() to created a "grouped" copy of a table, dplyr functions will manipulate each "group" separately and then combine the results. mtcars%>% group_by(cyl) %>% summarise(avg = mean(mpg)) group_by(.data,add = FALSE) Returns copy of table grouped by... gjris <- group_by(iris, Species) ungroup(x,...) Returns ungrouped copy of table. ungroup(gjris) @Stud Manipulate Cases EXTRACT CASES Row functions return a subset of rows as a new table. Use a variant that ends in _ for non-standard evaluation friendly code. filter(.data,...) Extract rows that meet logical criteria. Also filter_(). filter(iris, Sepal.Length > 7) distinct(.data,.keep_all = FALSE) Remove rows with duplicate values. Also distinct_(). distinct(iris, Species) sample_frac(tbl, size = 1, replace = FALSE, weight = NULL, .env= parent.framef)) Randomly select fraction of rows. sample_frac(iris, 0.5, replace = TRUE) sample_n(tbl, size, replace = FALSE, weight = NULL, .env = parent.framef)) Randomly select size rows. sample_n(iris, 10, replace = TRUE) slice(.data,...) Select rows by position. Also slice_(). slice(iris, 10:15) top_n(x, n, wt) Select and order top n entries (by group if grouped data). top_n(iris, 5, Sepal.Width) Logical and boolean operators to use with filter)) < <= is.na() %in% | xor() > >= !is.na() ! & See ?base::logic and ?Comparison for help. ARRANGE CASES ADD CASES arrange).data,...) Order rows by values of a column (low to high), use with desc() to order from high to low. arrange(mtcars, mpg) arrange(mtcars, desc(mpg)) add_row(.data,.before = NULL, .after = NULL) Add one or more rows to a table. add_row(faithful, eruptions = 1, waiting =1) Column functions return a set of columns as a new table. Use a variant that ends in _ for non-standard evaluation friendly code. ■■■ _^ ■ select(.data,...) Extract columns by name. Also select_if() select(iris, Sepal.Length, Species) Use these helpers with select (), e.g. select(iris, starts_with("Sepal")) contains(match) ends_with(match) matches(match) num_range(prefix, range) one_of(...) starts_with(match) e.g. mpgxyl -, e.g, -Species MAKE NEW VARIABLES These apply vectorized functions to columns. Vectorized funs take vectors as input and return vectors of the same length as output (see back). ! vectorized function mutate).data,...) Compute new column(s). mutate(mtcars, gpm = 1/mpg) transmute).data,...) Compute new column(s), drop others. transmute(mtcars, gpm = 1/mpg) mutate_all(.tbl, .funs,...) Apply funs to every column. Use with funs(). mutate_all(faithful, funs(log(.), log2(.))) mutate_at(.tbl, .cols, .funs,...) Apply funs to specific columns. Use with funs(), vars() and the helper functions for select(). mutate_at(iris, vars( -Species), funs(log(.))) mutate_if(.tbl, .predicate, .funs,...) Apply funs to all columns of one type. Use with funs(). mutate_if(iris, is.numeric, funs(log(.))) add_column(.data,.before = NULL, .after = NULL) Add new column(s). add_column(mtcars, new = 1:32) renamef.data,...) Rename columns. rename(iris, Length = Sepal.Length) 10 RStudio® is a trademark of RStudio, Inc. • CCBYRStudio- info@rstudio.com • 844-448-1212 • rstudio.com • Learn more with browseVignettesfpackage - cfdplyr", "tibble")) • dplyr 0.5.0« tibble 1.2.0 • Updated: 2017-01 Vectorized Functions TO USE WITH MUTATE () mutate)) and transmute)) apply vectorized functions to columns to create new columns. Vectorized functions take vectors as input and return vectors of the same length as output. i vectorized function OFFSETS dplyr::lag() - Offset elements by 1 dplyr::lead() - Offset elements by -1 CUMULATIVE AGGREGATES dplyr::cumall() - Cumulative aII() dplyr::cumany() - Cumulative any() cummax() - Cumulative max() dplyr::cummean() - Cumulative mean() cummin() - Cumulative min() cumprod() - Cumulative prod() cumsum() - Cumulative sum() RANKINGS dplyr::cume_dist() - Proportion of all values <= dplyr::dense_rank() - rank with ties = min, no gaps dplyr::min_rank() - rank with ties = min dplyr::ntile() - bins into n bins dplyr::percent_rank() - min_rankscaled to [0,1] dplyr::row_number() - rank with ties = "first" MATH +)-)*) />A) %/%, %% - arithmetic ops log(), log2(), loglOO - logs == - logical comparisons <. <=. >. >= MISC dplyr::between() - x >= left & x <= right dplyr::case_when() - multi-case if_else() dplyr::coalesce() - first non-NA values by element across a set of vectors dplyr::if_else() - element-wise if() + else() dplyr::na_if() - replace specific values with NA pmax() - element-wise max() pmin() - element-wise min() dplyr::recode() - Vectorized switch() dplyr::recode_factor() - Vectorized switch() for factors @Stud 10 Summary Functions TO USE WITH SUMMARISE () summarise)) applies summary functions to columns to create a new table. Summary functions take vectors as input and return single values as output. i summary function COUNTS dplyr::n() - number of values/rows dplyr::n_distinct() - # of uniques sum(!is.na()) - # of non-NA's LOCATION mean() - mean, also mean(!is.na()) median)) - median LOGICALS mean() - Proportion of TRUE's sum()-#ofTRUE's POSITION/ORDER dplyr::first() - first value dplyr::last() - last value dplyr::nth() - value in nth location of vector RANK quantile() - nth quantile min() - minimum value max() - maximum value SPREAD IQR() - Inter-Quartile Range mad() - mean absolute deviation sd() - standard deviation var() - variance Row Names Tidy data does not use rownames, which store a variable outside of the columns. To work with the rownames, first move them into a column. b c aa rownames_to_column() . l a t Move row names into col. 2 b u a <- rownames_to_column(iris, var 3 c v = „c„j HB column_to_rownames() 1 a * ~*l b u Move co' m row names. 3 c l 3 c I column_to_rownames(a,var="C") Also has_rownames(), remove_rownames() Combine Tables COMBINE VARIABLES x y bbb beb a t 1 I a t 3 b u 2 b u 2 c v 3 d w 1 a t 1 a t 3 b u 2 b u 2 c v 3 d w 1 Use bind_cols() to paste tables beside each other as they are. bind_cols(...) Returns tables placed side by side as a single table. BE SURE THAT ROWS ALIGN. Use a "Mutating Join" to join one table to columns from another, matching values with the rows that they correspond to. Each join retains a different combination of values from the tables. Finnn left join(x, y, by = NULL, a 1 1 3 copy=FALSE, suffix=c(".x",".y"),...) c v 3 fjA Join matching values from y to x. Finnn rightJoin(x, y, by = NULL, copy = a t i 3 FALSE, suffix=c(".x",".y"),...) Join matching values from x to y. b u 2 2 d w NA 1 FlFinr inner join(x, y, by = NULL, copy = a t i s FALSE, suffix=c(".x",".y"),...) b u 2 2 Join data. Retain only rows with matches. HBHB fulljoin(x, y, by = NULL, llH copy=FALSE, suffix=c(".x",".y"),...) c v 3 na Join data. Retain all values, all rows. nma s Use by _ ^"cou", »coi2") to b u 2 u 2 specify the column(s) to match on. c v 3 na na left_join(x,y,by="A") Use a named vector, by = c("coll" = "col2"), to match on columns with different names in each data set. leftjoinfx, y by = c("C" = "D")) PTIR1H Use suffix to specify suffix to give to a t i d w duplicate column names. c I 3 a ,u left_join(x, y by = c("C" = "D"), suffix = c("r;"2")) a t 1 cl w b u 2 b u c v 3 a t COMBINE CASES nnn a t 1 C v 3 y d w 4 Use bind_rows() to paste tables below each other as they are. mnnn bind_rows(..., .id = NULL) SSSS Returns tables one on top of the other „ c v 3 as a single table. Set .id to a column z c » 3 name to add a column of the original z d w 4 table names (as pictured) HHH intersect(x, y,...) c v 3 Rows that appear in both x and z. ^ Finn setdiff(x,y,...) at 1 Rows that appear in x but not z. b u 2 Finn union(x,y,...) at 1 Rows that appear in x or z. bc 1 2 (Duplicates removed). union_all() d»4 retains duplicates. Use setequal() to test whether two data sets contain the exact same rows (in any order). EXTRACT ROWS x y bbb a t 1 I a t 3 b u 2 T b u 2 Use a "Filtering Join" to filter one table against the rows of another. nnn semijoin(x,y, by = NULL,...) at i Return rows of x that have a match in y. b"2 USEFULTO SEE WHATWILL BE JOINED. Finn anti join(x, y, by = NULL,...) cv3 Return rows of x that do not have a match iny. USEFULTO SEE WHATWILL NOT BE JOINED. RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more with browseVignettesfpackage - cf'dplyr", "tibble")) • dplyr 0.5.0« tibble 1.2.0 • Updated: 2017-01