Skip to content

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.

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.23
10 CDH1 protein_coding chr16 374. -1.28 5.5 e- 2 0.1
11 BRAF protein_coding chr7 1931. 0.91 1.7 e- 1 0.23
12 IDH1 protein_coding chr2 6273. 1.41 3.4 e- 2 0.068
13 SMAD4 protein_coding chr18 76.2 2.07 1.9 e- 3 NA
14 MET protein_coding chr7 289. -1.22 6.7 e- 2 0.11
15 MALAT1 lncRNA chr11 344. 1.01 1.3 e- 1 0.2
16 NEAT1 lncRNA chr11 865. -3.43 2.70e- 7 0.0000011
17 HOTAIR lncRNA chr12 287. -1.57 1.9 e- 2 0.042
18 XIST lncRNA chrX 16.6 -1.7 1.1 e- 2 NA
19 MIR21 miRNA chr17 21.6 -4.83 4.30e-13 NA
20 MIR155 miRNA chr21 1967. 0.07 9.2 e- 1 0.92

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.0000006
2 MYC protein_coding chr8 862. 2.43 0.00027 0.0009
3 KRAS protein_coding chr12 655. 3.79 0.000000013 0.00000013
4 RB1 protein_coding chr13 360. -3.53 0.00000012 0.0000006
5 NEAT1 lncRNA chr11 865. -3.43 0.00000027 0.0000011
6 HOTAIR lncRNA chr12 287. -1.57 0.019 0.042

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.0000006
2 MYC 862. 0.0009
3 KRAS 655. 0.00000013
4 RB1 360. 0.0000006
5 SMAD4 76.2 NA
6 NEAT1 865. 0.0000011
7 HOTAIR 287. 0.042
8 XIST 16.6 NA
9 MIR21 21.6 NA

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.56
2 KRAS protein_coding 3.79
3 RB1 protein_coding -3.53
4 NEAT1 lncRNA -3.43
5 MIR21 miRNA -4.83
6 MIR155 miRNA 0.07

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.00000013
2 BRCA1 -3.56 0.0000006
3 RB1 -3.53 0.0000006

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 NA
2 KRAS 3.79 0.00000013
3 BRCA1 -3.56 0.0000006
4 RB1 -3.53 0.0000006
5 NEAT1 -3.43 0.0000011

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_coding
2 lncRNA
3 miRNA

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.42
2 BRCA1 0.000000093 0.0000006
3 EGFR 0.61 0.64
genes |>
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.42
2 BRCA1 205. -3.56 0.000000093 0.0000006
3 EGFR 624. -0.34 0.61 0.64

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.36
2 BRCA1 0.0000006 protein_coding chr17 205. -3.56 0.000000093
3 EGFR 0.64 protein_coding chr7 624. -0.34 0.61

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"

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 FALSE
2 BRCA1 -3.56 6.22 TRUE
3 EGFR -0.34 0.194 FALSE
4 MYC 2.43 3.05 TRUE

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 4
2 ns 14
3 up 2

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 11
2 sig 6
3 untested 3

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 14
2 ncRNA 6

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 1
2 XIST 16.6 NA 1
3 MIR21 21.6 NA 1

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.1
2 S2 NA
3 S3 8.2

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.377
2 BRCA1 7.03 6.22
3 EGFR 0.215 0.194

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. 1704
2 MYC 1504 1391 3302 2950 1448. 3126
3 XIST 3 0 2 1 1.5 1.5

.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 4
2 lncRNA 4 -1.42 2
3 miRNA 2 -2.38 0

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.11
2 lncRNA 378. 355. -1.42 1.83
3 miRNA 994. 1375. -2.38 3.46

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 3
2 chr7 3
3 chr11 2
4 chr12 2

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 11
2 lncRNA 4 3
3 miRNA 2 2

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.79
2 NEAT1 lncRNA -3.43
3 MIR21 miRNA -4.83

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.13
2 protein_coding 0.5 -0.425
3 protein_coding 0.75 1.29
4 lncRNA 0.25 -2.13
5 lncRNA 0.5 -1.64
6 lncRNA 0.75 -0.925
7 miRNA 0.25 -3.60
8 miRNA 0.5 -2.38
9 miRNA 0.75 -1.16

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 NA
2 FOS 1 9.8 4.7
3 FOS 2 8.2 -1.60
4 FOS 4 6 -2.2
5 EGR1 0 6.3 NA
6 EGR1 1 10.4 4.1
7 EGR1 2 9.1 -1.3
8 EGR1 4 7.2 -1.9

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 1
2 FOS 1 9.8 1
3 FOS 2 8.2 2
4 FOS 4 6 NA
5 EGR1 0 6.3 1
6 EGR1 1 10.4 1
7 EGR1 2 9.1 2
8 EGR1 4 7.2 NA

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.572
2 MALAT1 lncRNA 344. 0.799
3 HOTAIR lncRNA 287. 0.989
4 XIST lncRNA 16.6 1
5 MIR155 miRNA 1967. 0.989
6 MIR21 miRNA 21.6 1

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 1
2 BRCA1 chr17 -3.56 2
3 TP53 chr17 -0.61 3
4 MET chr7 -1.22 1
5 BRAF chr7 0.91 2
6 EGFR chr7 -0.34 3

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 1
2 HOTAIR lncRNA 0.042 2
3 MIR155 miRNA 0.92 1
4 KRAS protein_coding 0.00000013 1
5 BRCA1 protein_coding 0.0000006 2
6 RB1 protein_coding 0.0000006 2

The 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"
)
)

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 signaling
2 TP53 -0.61 Apoptosis
3 BRCA1 -3.56 <NA>
4 EGFR -0.34 EGFR signaling
5 MYC 2.43 <NA>
6 KRAS 3.79 MAPK signaling

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

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 signaling
2 TP53 -0.61 Apoptosis
3 EGFR -0.34 EGFR signaling
4 KRAS 3.79 MAPK signaling
5 PTEN -0.51 PI3K-Akt signaling

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 signaling
2 TP53 -0.61 Apoptosis
3 EGFR -0.34 EGFR signaling
4 KRAS 3.79 MAPK signaling
5 PTEN -0.51 PI3K-Akt signaling
6 FOXP3 NA Treg differentiation

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"

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 7687538
2 var2 chr17 43100000 BRCA1 43044295 43125483
3 var3 chr7 55191822 EGFR 55019017 55211628
4 var4 chr1 1000000 <NA> NA NA

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.5
2 run_a MYC 2.3
3 run_b EGFR -0.8
4 run_b KRAS 0.4

The dplyr package by Wickham and colleagues supplies the grammar of data manipulation.

  • dplyr introduction

    The package guide explains row filters, column selection, grouped summaries and the relationship between these verbs.