英文:
Find Top5 min and max values per group in dataframe (determine min and max differentially expressed genes)
问题
In a dataframe of differential expression results, find the 5 genes per group that are maximally and minimally regulated (= highest and lowest log fold change).
example data
set.seed(47)
gene_creator <- paste("gene",1:100,sep="")
genes = sample(gene_creator,8)
dex_A <- data.frame(
gene = genes,
group = "group_A",
logFC = sample(c(-5:5), replace=T, size=8),
FDR = sample(c(0.01,1), replace=T, size=8)
)
dex_B <- data.frame(
gene = genes,
group = "group_B",
logFC = sample(c(-5:5), replace=T, size=8),
FDR = sample(c(0.01,1), replace=T, size=8)
)
dex_df <- rbind(dex_A, dex_B)
solution (not working)
library(tidyverse)
dex_df %>%
filter(FDR < 0.05) %>%
group_by(group) %>%
mutate(
top5 = slice_max(logFC, n = 5),
min5 = slice_min(logFC, n = 5))
英文:
In a dataframe of differential expression results, find the 5 genes per group that are maximally and minimally regulated (= highest and lowest log fold change).
My solution with mutate and slice_max/min somehow wont work.
example data
set.seed(47)
gene_creator <- paste("gene",1:100,sep="")
genes = sample(gene_creator,8)
dex_A <- data.frame(
gene = genes,
group = "group_A",
logFC = sample(c(-5:5), replace=T, size=8),
FDR = sample(c(0.01,1), replace=T, size=8)
)
dex_B <- data.frame(
gene = genes,
group = "group_B",
logFC = sample(c(-5:5), replace=T, size=8),
FDR = sample(c(0.01,1), replace=T, size=8)
)
dex_df <- rbind(dex_A, dex_B)
solution (not working)
library(tidyverse)
dex_df %>%
filter(FDR < 0.05) %>%
group_by(group) %>%
mutate(
top5 = slice_max(logFC, n = 5),
min5 = slice_min(logFC, n = 5))
答案1
得分: 3
You cannot use slice_max()
or slice_min()
directly with mutate
. You have to use them separately and row bind them, i.e.
您不能直接在mutate
中使用slice_max()
或slice_min()
。您必须将它们分开使用,然后将它们行绑定,即:
英文:
You cannot use slice_max()
or slice_min()
directly with mutate
. You have to use them separately and row bind them, i.e.
bind_rows(dex_df %>%
filter(FDR < 0.05) %>%
group_by(group) %>%
slice_max(logFC, n = 5) %>%
mutate(rank = "top5"),
dex_df %>%
filter(FDR < 0.05) %>%
group_by(group) %>%
slice_min(logFC, n = 5) %>%
mutate(rank = "min5"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论