dplyr
dplyr gives you one verb per table operation, and each verb takes a data frame first and
returns a data frame, so the verbs chain with the native pipe |>. The examples use a
simulated result table for 20 real genes. The chromosomes and biotypes are the real ones,
the fold changes are random, and each p-value follows from its fold change.
Quick reference
Section titled “Quick reference”| Verb | Purpose | Example |
|---|---|---|
filter() |
Keep rows where all conditions are TRUE |
filter(padj < 0.05) |
filter_out() |
Drop matching rows, keep NA rows |
filter_out(padj >= 0.05) |
slice_min(), slice_max() |
Rows with the smallest or largest values | slice_min(padj, n = 10) |
arrange() |
Sort rows | arrange(desc(abs(log2fc))) |
distinct() |
Unique rows | distinct(biotype) |
select() |
Pick columns | select(gene, starts_with("p")) |
rename() |
Rename columns | rename(symbol = gene) |
relocate() |
Move columns | relocate(padj, .after = gene) |
pull() |
One column as a vector | pull(gene) |
mutate() |
Add or change columns | mutate(nlp = -log10(padj)) |
case_when() |
Many way condition | case_when(x > 1 ~ "up", .default = "ns") |
if_else() |
Two way condition | if_else(p < 0.05, "sig", "ns") |
recode_values() |
Map old values to new ones | recode_values(x, "a" ~ "b") |
coalesce() |
First value that is not NA |
coalesce(padj, 1) |
na_if() |
Turn a placeholder into NA |
na_if(rin, -1) |
across() |
One function, many columns | across(c(x, y), mean) |
pick() |
Columns as a data frame | rowMeans(pick(starts_with("ctrl"))) |
summarise() |
One row per group | summarise(n = n(), .by = biotype) |
reframe() |
Any number of rows per group | reframe(q = quantile(x), .by = g) |
count() |
Rows per group | count(chrom, sort = TRUE) |
n_distinct() |
Unique values per group | summarise(k = n_distinct(chrom), .by = g) |
lag(), lead() |
Previous or next row | mutate(d = x - lag(x), .by = gene) |
cumsum() |
Running total | mutate(total = cumsum(x), .by = g) |
row_number() |
Rank with ties broken by row order | row_number(desc(abs(log2fc))) |
min_rank() |
Rank with ties sharing the lowest rank | min_rank(padj) |
left_join() |
Keep all left rows | left_join(y, by = join_by(id)) |
inner_join() |
Keep rows that match | inner_join(y, by = join_by(a == b)) |
full_join() |
Keep all rows from both tables | full_join(y, by = join_by(id)) |
semi_join(), anti_join() |
Filter by a match in another table | anti_join(y, by = join_by(id)) |
bind_rows() |
Stack tables | bind_rows(a = x, b = y, .id = "src") |
padj is NA where basemean is below 100, which mimics the genes DESeq2 drops by
independent filtering.
library(dplyr)
# Match the printed width on this page.options(width = 80)set.seed(42)
genes <- tibble( gene = c( "TP53", "BRCA1", "EGFR", "MYC", "KRAS", "PIK3CA", "PTEN", "RB1", "APC", "CDH1", "BRAF", "IDH1", "SMAD4", "MET", "MALAT1", "NEAT1", "HOTAIR", "XIST", "MIR21", "MIR155" ), biotype = rep(c("protein_coding", "lncRNA", "miRNA"), times = c(14, 4, 2)), chrom = c( "chr17", "chr17", "chr7", "chr8", "chr12", "chr3", "chr10", "chr13", "chr5", "chr16", "chr7", "chr2", "chr18", "chr7", "chr11", "chr11", "chr12", "chrX", "chr17", "chr21" ), basemean = round(rlnorm(20, meanlog = 6, sdlog = 1.2), 1), log2fc = round(rnorm(20, mean = 0, sd = 2), 2), pvalue = signif(2 * pnorm(-abs(log2fc) * 1.5), 2), padj = if_else(basemean < 100, NA, signif(p.adjust(pvalue, "BH"), 2)))genes# A tibble: 20 × 7 gene biotype chrom basemean log2fc pvalue padj <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> 1 TP53 protein_coding chr17 2090. -0.61 3.6 e- 1 0.42 2 BRCA1 protein_coding chr17 205. -3.56 9.3 e- 8 0.0000006 3 EGFR protein_coding chr7 624. -0.34 6.1 e- 1 0.64 4 MYC protein_coding chr8 862. 2.43 2.7 e- 4 0.0009 5 KRAS protein_coding chr12 655. 3.79 1.3 e- 8 0.00000013 6 PIK3CA protein_coding chr3 355. -0.86 2 e- 1 0.25 7 PTEN protein_coding chr10 2475. -0.51 4.4 e- 1 0.49 8 RB1 protein_coding chr13 360. -3.53 1.20e- 7 0.0000006 9 APC protein_coding chr5 4546. 0.92 1.7 e- 1 0.2310 CDH1 protein_coding chr16 374. -1.28 5.5 e- 2 0.111 BRAF protein_coding chr7 1931. 0.91 1.7 e- 1 0.2312 IDH1 protein_coding chr2 6273. 1.41 3.4 e- 2 0.06813 SMAD4 protein_coding chr18 76.2 2.07 1.9 e- 3 NA14 MET protein_coding chr7 289. -1.22 6.7 e- 2 0.1115 MALAT1 lncRNA chr11 344. 1.01 1.3 e- 1 0.216 NEAT1 lncRNA chr11 865. -3.43 2.70e- 7 0.000001117 HOTAIR lncRNA chr12 287. -1.57 1.9 e- 2 0.04218 XIST lncRNA chrX 16.6 -1.7 1.1 e- 2 NA19 MIR21 miRNA chr17 21.6 -4.83 4.30e-13 NA20 MIR155 miRNA chr21 1967. 0.07 9.2 e- 1 0.92filter()
Section titled “filter()”Commas between conditions mean AND.
genes |> filter(padj < 0.05, abs(log2fc) > 1)# A tibble: 6 × 7 gene biotype chrom basemean log2fc pvalue padj <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>1 BRCA1 protein_coding chr17 205. -3.56 0.000000093 0.00000062 MYC protein_coding chr8 862. 2.43 0.00027 0.00093 KRAS protein_coding chr12 655. 3.79 0.000000013 0.000000134 RB1 protein_coding chr13 360. -3.53 0.00000012 0.00000065 NEAT1 lncRNA chr11 865. -3.43 0.00000027 0.00000116 HOTAIR lncRNA chr12 287. -1.57 0.019 0.042Rows with NA and filter_out()
Section titled “Rows with NA and filter_out()”filter() keeps a row only where the condition is TRUE, so a gene with padj = NA drops
out of padj < 0.05 and out of padj >= 0.05 alike. filter_out(), new in dplyr 1.2, drops
the rows that match and keeps the NA rows.
genes |> filter_out(padj >= 0.05) |> select(gene, basemean, padj)# A tibble: 9 × 3 gene basemean padj <chr> <dbl> <dbl>1 BRCA1 205. 0.00000062 MYC 862. 0.00093 KRAS 655. 0.000000134 RB1 360. 0.00000065 SMAD4 76.2 NA6 NEAT1 865. 0.00000117 HOTAIR 287. 0.0428 XIST 16.6 NA9 MIR21 21.6 NAOR across conditions with when_any()
Section titled “OR across conditions with when_any()”Use | for two conditions. when_any() takes a comma separated list and joins it with OR.
genes |> filter(when_any(log2fc > 3, log2fc < -3, biotype == "miRNA")) |> select(gene, biotype, log2fc)# A tibble: 6 × 3 gene biotype log2fc <chr> <chr> <dbl>1 BRCA1 protein_coding -3.562 KRAS protein_coding 3.793 RB1 protein_coding -3.534 NEAT1 lncRNA -3.435 MIR21 miRNA -4.836 MIR155 miRNA 0.07slice_min() and slice_max()
Section titled “slice_min() and slice_max()”slice_min() returns the rows with the smallest values. A tie returns more than n rows
unless you pass with_ties = FALSE.
genes |> slice_min(padj, n = 3) |> select(gene, log2fc, padj)# A tibble: 3 × 3 gene log2fc padj <chr> <dbl> <dbl>1 KRAS 3.79 0.000000132 BRCA1 -3.56 0.00000063 RB1 -3.53 0.0000006arrange()
Section titled “arrange()”Wrap a column in desc() to sort from largest to smallest.
genes |> arrange(desc(abs(log2fc))) |> select(gene, log2fc, padj) |> head(5)# A tibble: 5 × 3 gene log2fc padj <chr> <dbl> <dbl>1 MIR21 -4.83 NA2 KRAS 3.79 0.000000133 BRCA1 -3.56 0.00000064 RB1 -3.53 0.00000065 NEAT1 -3.43 0.0000011distinct()
Section titled “distinct()”distinct() keeps the first row of each unique combination of the columns you name.
genes |> distinct(biotype)# A tibble: 3 × 1 biotype <chr>1 protein_coding2 lncRNA3 miRNAColumns
Section titled “Columns”select() with helpers
Section titled “select() with helpers”starts_with(), ends_with(), contains(), and where() pick columns by name or type.
genes |> select(gene, starts_with("p")) |> head(3)# A tibble: 3 × 3 gene pvalue padj <chr> <dbl> <dbl>1 TP53 0.36 0.422 BRCA1 0.000000093 0.00000063 EGFR 0.61 0.64genes |> select(gene, where(is.numeric)) |> head(3)# A tibble: 3 × 5 gene basemean log2fc pvalue padj <chr> <dbl> <dbl> <dbl> <dbl>1 TP53 2090. -0.61 0.36 0.422 BRCA1 205. -3.56 0.000000093 0.00000063 EGFR 624. -0.34 0.61 0.64rename() and relocate()
Section titled “rename() and relocate()”rename() takes new = old. relocate() moves columns without dropping any.
genes |> rename(symbol = gene) |> relocate(padj, .after = symbol) |> head(3)# A tibble: 3 × 7 symbol padj biotype chrom basemean log2fc pvalue <chr> <dbl> <chr> <chr> <dbl> <dbl> <dbl>1 TP53 0.42 protein_coding chr17 2090. -0.61 0.362 BRCA1 0.0000006 protein_coding chr17 205. -3.56 0.0000000933 EGFR 0.64 protein_coding chr7 624. -0.34 0.61pull()
Section titled “pull()”pull() returns one column as a vector, ready for a gene list or %in%.
genes |> filter(padj < 0.05) |> pull(gene)[1] "BRCA1" "MYC" "KRAS" "RB1" "NEAT1" "HOTAIR"New columns
Section titled “New columns”mutate()
Section titled “mutate()”A new column can use one created earlier in the same call.
genes |> mutate( neg_log10_padj = -log10(padj), passes = neg_log10_padj > 2 & abs(log2fc) > 1 ) |> select(gene, log2fc, neg_log10_padj, passes) |> head(4)# A tibble: 4 × 4 gene log2fc neg_log10_padj passes <chr> <dbl> <dbl> <lgl>1 TP53 -0.61 0.377 FALSE2 BRCA1 -3.56 6.22 TRUE3 EGFR -0.34 0.194 FALSE4 MYC 2.43 3.05 TRUEcase_when()
Section titled “case_when()”The first true condition wins. .default sets the value for everything else, including
rows where a condition is NA. Older code writes TRUE ~ "ns" for the same thing.
genes |> mutate(direction = case_when( padj < 0.05 & log2fc > 1 ~ "up", padj < 0.05 & log2fc < -1 ~ "down", .default = "ns" )) |> count(direction)# A tibble: 3 × 2 direction n <chr> <int>1 down 42 ns 143 up 2if_else()
Section titled “if_else()”if_else() checks that both branches have the same type, and missing sets the value
where the condition is NA. Base ifelse() does neither.
genes |> mutate(call = if_else(padj < 0.05, "sig", "ns", missing = "untested")) |> count(call)# A tibble: 3 × 2 call n <chr> <int>1 ns 112 sig 63 untested 3recode_values() with a lookup table
Section titled “recode_values() with a lookup table”recode_values() maps old values to new ones, from formulas or from two vectors. dplyr 1.2
deprecated case_match() in its favor, and recode() is older still.
biotype_map <- tibble( biotype = c("protein_coding", "lncRNA", "miRNA"), rna_class = c("mRNA", "ncRNA", "ncRNA"))genes |> mutate(rna_class = recode_values( biotype, from = biotype_map$biotype, to = biotype_map$rna_class )) |> count(rna_class)# A tibble: 2 × 2 rna_class n <chr> <int>1 mRNA 142 ncRNA 6coalesce()
Section titled “coalesce()”coalesce() takes the first value that is not NA, column by column or from a constant.
Here an untested gene gets padj = 1 so that it lands at the bottom of a volcano plot.
genes |> mutate(padj_plot = coalesce(padj, 1)) |> filter(is.na(padj)) |> select(gene, basemean, padj, padj_plot)# A tibble: 3 × 4 gene basemean padj padj_plot <chr> <dbl> <dbl> <dbl>1 SMAD4 76.2 NA 12 XIST 16.6 NA 13 MIR21 21.6 NA 1na_if()
Section titled “na_if()”na_if() turns a placeholder value into NA. A QC sheet that writes -1 for a RIN that
was never measured is the usual case.
qc <- tibble(sample = c("S1", "S2", "S3"), rin = c(9.1, -1, 8.2))qc |> mutate(rin = na_if(rin, -1))# A tibble: 3 × 2 sample rin <chr> <dbl>1 S1 9.12 S2 NA3 S3 8.2across()
Section titled “across()”across() applies one function to many columns. .names sets the output names. The
_at(), _if(), and _all() verb variants that older code uses are superseded by it.
genes |> mutate( across(c(pvalue, padj), \(p) -log10(p), .names = "nlog10_{.col}") ) |> select(gene, starts_with("nlog10")) |> head(3)# A tibble: 3 × 3 gene nlog10_pvalue nlog10_padj <chr> <dbl> <dbl>1 TP53 0.444 0.3772 BRCA1 7.03 6.223 EGFR 0.215 0.194Row means with pick()
Section titled “Row means with pick()”pick() hands a set of columns to a function that wants a data frame, such as
rowMeans().
counts <- tibble( gene = c("TP53", "MYC", "XIST"), ctrl_1 = c(812, 1504, 3), ctrl_2 = c(945, 1391, 0), trt_1 = c(1720, 3302, 2), trt_2 = c(1688, 2950, 1))counts |> mutate( mean_ctrl = rowMeans(pick(starts_with("ctrl"))), mean_trt = rowMeans(pick(starts_with("trt"))) )# A tibble: 3 × 7 gene ctrl_1 ctrl_2 trt_1 trt_2 mean_ctrl mean_trt <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>1 TP53 812 945 1720 1688 878. 17042 MYC 1504 1391 3302 2950 1448. 31263 XIST 3 0 2 1 1.5 1.5Groups
Section titled “Groups”summarise() with .by
Section titled “summarise() with .by”.by groups for one call and returns an ungrouped table, with groups in the order they
first appear. The older form, group_by(biotype) |> summarise(...), sorts the groups. With
two or more grouping columns it also leaves the result grouped unless you pass
.groups = "drop".
genes |> summarise( n_genes = n(), mean_log2fc = mean(log2fc), n_sig = sum(padj < 0.05, na.rm = TRUE), .by = biotype )# A tibble: 3 × 4 biotype n_genes mean_log2fc n_sig <chr> <int> <dbl> <int>1 protein_coding 14 -0.0271 42 lncRNA 4 -1.42 23 miRNA 2 -2.38 0across() inside summarise()
Section titled “across() inside summarise()”A named list of functions gives one column per column and function pair.
genes |> summarise( across(c(basemean, log2fc), list(mean = mean, sd = sd)), .by = biotype )# A tibble: 3 × 5 biotype basemean_mean basemean_sd log2fc_mean log2fc_sd <chr> <dbl> <dbl> <dbl> <dbl>1 protein_coding 1508. 1850. -0.0271 2.112 lncRNA 378. 355. -1.42 1.833 miRNA 994. 1375. -2.38 3.46count()
Section titled “count()”count() returns the number of rows in each group, sorted by the group values.
sort = TRUE puts the largest group first instead.
genes |> count(chrom, sort = TRUE) |> head(4)# A tibble: 4 × 2 chrom n <chr> <int>1 chr17 32 chr7 33 chr11 24 chr12 2n_distinct()
Section titled “n_distinct()”n_distinct() counts the unique values in each group, where n() counts the rows.
genes |> summarise( n_genes = n(), n_chrom = n_distinct(chrom), .by = biotype )# A tibble: 3 × 3 biotype n_genes n_chrom <chr> <int> <int>1 protein_coding 14 112 lncRNA 4 33 miRNA 2 2The top row per group
Section titled “The top row per group”The slice_*() verbs take by, without the dot.
genes |> slice_max(abs(log2fc), n = 1, by = biotype) |> select(gene, biotype, log2fc)# A tibble: 3 × 3 gene biotype log2fc <chr> <chr> <dbl>1 KRAS protein_coding 3.792 NEAT1 lncRNA -3.433 MIR21 miRNA -4.83Several rows per group with reframe()
Section titled “Several rows per group with reframe()”reframe() returns any number of rows per group. summarise() in dplyr 1.2 stops with an
error when a group returns more than one row, where dplyr 1.1 only warned.
genes |> reframe( quantile = c(0.25, 0.5, 0.75), log2fc = quantile(log2fc, quantile), .by = biotype )# A tibble: 9 × 3 biotype quantile log2fc <chr> <dbl> <dbl>1 protein_coding 0.25 -1.132 protein_coding 0.5 -0.4253 protein_coding 0.75 1.294 lncRNA 0.25 -2.135 lncRNA 0.5 -1.646 lncRNA 0.75 -0.9257 miRNA 0.25 -3.608 miRNA 0.5 -2.389 miRNA 0.75 -1.16Window functions
Section titled “Window functions”A window function returns one value per row, computed from the other rows of its group, so
it goes in mutate() with .by. The time course below follows two immediate early genes
after stimulation.
time_course <- tibble( gene = rep(c("FOS", "EGR1"), each = 4), hour = rep(c(0, 1, 2, 4), times = 2), log_expr = c(5.1, 9.8, 8.2, 6.0, 6.3, 10.4, 9.1, 7.2))lag() returns the value from the previous row, so the first row of each gene gets NA.
Sort the rows first, or pass order_by = hour.
time_course |> mutate(change = log_expr - lag(log_expr), .by = gene)# A tibble: 8 × 4 gene hour log_expr change <chr> <dbl> <dbl> <dbl>1 FOS 0 5.1 NA2 FOS 1 9.8 4.73 FOS 2 8.2 -1.604 FOS 4 6 -2.25 EGR1 0 6.3 NA6 EGR1 1 10.4 4.17 EGR1 2 9.1 -1.38 EGR1 4 7.2 -1.9lead()
Section titled “lead()”lead() returns the value from the next row. With uneven sampling it gives the gap to the
next time point.
time_course |> mutate(hours_to_next = lead(hour) - hour, .by = gene)# A tibble: 8 × 4 gene hour log_expr hours_to_next <chr> <dbl> <dbl> <dbl>1 FOS 0 5.1 12 FOS 1 9.8 13 FOS 2 8.2 24 FOS 4 6 NA5 EGR1 0 6.3 16 EGR1 1 10.4 17 EGR1 2 9.1 28 EGR1 4 7.2 NAcumsum()
Section titled “cumsum()”cumsum() adds up the rows in order. Sorted by basemean within each biotype, it gives
the share of the biotype’s counts that its top genes carry.
genes |> arrange(biotype, desc(basemean)) |> mutate(cum_share = cumsum(basemean) / sum(basemean), .by = biotype) |> filter(biotype != "protein_coding") |> select(gene, biotype, basemean, cum_share)# A tibble: 6 × 4 gene biotype basemean cum_share <chr> <chr> <dbl> <dbl>1 NEAT1 lncRNA 865. 0.5722 MALAT1 lncRNA 344. 0.7993 HOTAIR lncRNA 287. 0.9894 XIST lncRNA 16.6 15 MIR155 miRNA 1967. 0.9896 MIR21 miRNA 21.6 1row_number()
Section titled “row_number()”row_number() numbers the rows of each group in the order of its argument. Here it ranks
the genes on each chromosome by the size of their fold change.
genes |> mutate(rank_in_chrom = row_number(desc(abs(log2fc))), .by = chrom) |> filter(chrom %in% c("chr7", "chr17")) |> arrange(chrom, rank_in_chrom) |> select(gene, chrom, log2fc, rank_in_chrom)# A tibble: 6 × 4 gene chrom log2fc rank_in_chrom <chr> <chr> <dbl> <int>1 MIR21 chr17 -4.83 12 BRCA1 chr17 -3.56 23 TP53 chr17 -0.61 34 MET chr7 -1.22 15 BRAF chr7 0.91 26 EGFR chr7 -0.34 3min_rank()
Section titled “min_rank()”min_rank() gives tied values the same rank and skips the next one, so BRCA1 and RB1 share
rank 2 and the next protein coding gene would get rank 4. row_number() would break the tie
by row order. A missing value gets rank NA, which is why MIR21 drops out.
genes |> mutate(padj_rank = min_rank(padj), .by = biotype) |> filter(padj_rank <= 2) |> arrange(biotype, padj_rank) |> select(gene, biotype, padj, padj_rank)# A tibble: 6 × 4 gene biotype padj padj_rank <chr> <chr> <dbl> <int>1 NEAT1 lncRNA 0.0000011 12 HOTAIR lncRNA 0.042 23 MIR155 miRNA 0.92 14 KRAS protein_coding 0.00000013 15 BRCA1 protein_coding 0.0000006 26 RB1 protein_coding 0.0000006 2The pathway table names its key symbol, and TP53 sits in two pathways.
pathways <- tibble( symbol = c("TP53", "TP53", "EGFR", "KRAS", "PTEN", "FOXP3"), pathway = c( "p53 signaling", "Apoptosis", "EGFR signaling", "MAPK signaling", "PI3K-Akt signaling", "Treg differentiation" ))left_join() with join_by()
Section titled “left_join() with join_by()”join_by(gene == symbol) matches columns with different names. A left join keeps every
gene, and TP53 now fills two rows.
genes |> select(gene, log2fc) |> left_join(pathways, by = join_by(gene == symbol)) |> head(6)# A tibble: 6 × 3 gene log2fc pathway <chr> <dbl> <chr>1 TP53 -0.61 p53 signaling2 TP53 -0.61 Apoptosis3 BRCA1 -3.56 <NA>4 EGFR -0.34 EGFR signaling5 MYC 2.43 <NA>6 KRAS 3.79 MAPK signalingStop a join from adding rows
Section titled “Stop a join from adding rows”relationship = "many-to-one" says each gene should match at most one pathway. The join
then fails instead of growing the table.
tryCatch( left_join( genes, pathways, by = join_by(gene == symbol), relationship = "many-to-one" ), error = \(e) cat(conditionMessage(e), sep = "\n"))Each row in `x` must match at most 1 row in `y`.ℹ Row 1 of `x` matches multiple rows in `y`.inner_join()
Section titled “inner_join()”An inner join keeps the rows that match in both tables. FOXP3 is not in genes, so it
drops out.
genes |> select(gene, log2fc) |> inner_join(pathways, by = join_by(gene == symbol))# A tibble: 5 × 3 gene log2fc pathway <chr> <dbl> <chr>1 TP53 -0.61 p53 signaling2 TP53 -0.61 Apoptosis3 EGFR -0.34 EGFR signaling4 KRAS 3.79 MAPK signaling5 PTEN -0.51 PI3K-Akt signalingfull_join()
Section titled “full_join()”A full join keeps every row from both tables. FOXP3 has a pathway but no row in genes, so
it arrives with NA for log2fc. The filter hides the genes without a pathway.
genes |> select(gene, log2fc) |> full_join(pathways, by = join_by(gene == symbol)) |> filter(!is.na(pathway))# A tibble: 6 × 3 gene log2fc pathway <chr> <dbl> <chr>1 TP53 -0.61 p53 signaling2 TP53 -0.61 Apoptosis3 EGFR -0.34 EGFR signaling4 KRAS 3.79 MAPK signaling5 PTEN -0.51 PI3K-Akt signaling6 FOXP3 NA Treg differentiationsemi_join() and anti_join()
Section titled “semi_join() and anti_join()”These two filter genes by the other table and add no columns. semi_join() keeps the
genes with a pathway, and anti_join() keeps the genes without one.
genes |> semi_join(pathways, by = join_by(gene == symbol)) |> pull(gene)genes |> anti_join(pathways, by = join_by(gene == symbol)) |> pull(gene)[1] "TP53" "EGFR" "KRAS" "PTEN" [1] "BRCA1" "MYC" "PIK3CA" "RB1" "APC" "CDH1" "BRAF" "IDH1" [9] "SMAD4" "MET" "MALAT1" "NEAT1" "HOTAIR" "XIST" "MIR21" "MIR155"Overlap join with between()
Section titled “Overlap join with between()”join_by() also matches on a range. Here each variant finds the gene span that contains
its position, and a variant outside every span keeps NA.
gene_spans <- tibble( gene = c("TP53", "BRCA1", "EGFR"), chrom = c("chr17", "chr17", "chr7"), start = c(7661779, 43044295, 55019017), end = c(7687538, 43125483, 55211628))variants <- tibble( variant = c("var1", "var2", "var3", "var4"), chrom = c("chr17", "chr17", "chr7", "chr1"), pos = c(7676154, 43100000, 55191822, 1000000))variants |> left_join(gene_spans, by = join_by(chrom, between(pos, start, end)))# A tibble: 4 × 6 variant chrom pos gene start end <chr> <chr> <dbl> <chr> <dbl> <dbl>1 var1 chr17 7676154 TP53 7661779 76875382 var2 chr17 43100000 BRCA1 43044295 431254833 var3 chr7 55191822 EGFR 55019017 552116284 var4 chr1 1000000 <NA> NA NAStack tables
Section titled “Stack tables”bind_rows()
Section titled “bind_rows()”Named arguments become the values of the .id column.
run_a <- tibble(gene = c("TP53", "MYC"), log2fc = c(1.5, 2.3))run_b <- tibble(gene = c("EGFR", "KRAS"), log2fc = c(-0.8, 0.4))bind_rows(run_a = run_a, run_b = run_b, .id = "batch")# A tibble: 4 × 3 batch gene log2fc <chr> <chr> <dbl>1 run_a TP53 1.52 run_a MYC 2.33 run_b EGFR -0.84 run_b KRAS 0.4See also
Section titled “See also”- Tidyverse Essentials builds up the core verbs one at a time on a small gene table.
- Joins compares the mutating and filtering joins in dplyr and Polars on the same tables.
- Grouped Summaries and Window & Rank go further into per group work.
- Conditional Logic covers
case_when()and its Polars equivalent,when(),then(), andotherwise(). - The tidyr cheatsheet handles the reshaping that dplyr does not do, such as wide to long.
Scientific references
Section titled “Scientific references”The dplyr package by Wickham and colleagues supplies the grammar of data manipulation.
Further reading
Section titled “Further reading”-
The package guide explains row filters, column selection, grouped summaries and the relationship between these verbs.