缘由
生信之路,道阻且长,记录经验,但渡有缘人。
碎碎念
及时当勉励,岁月不饶人 ——陶渊明
代码
如何拆分clusterprofile富集结果的前景集和背景集?
1
2
3
4
5
6
7
8
split_ratio <- function(ratio) {
split_ratio <- strsplit(ratio, "/")
do.call(rbind, lapply(split_ratio, function(x) data.frame(numerator = as.numeric(x[1]),
denominator = as.numeric(x[2])
)
)
)
}
如何基于clusterprofile的结果计算fold enrichment?
1
2
3
4
5
6
7
8
9
10
meta <- meta %>%
mutate(
pathway = str_replace(pathway, "\\D+", "map"),
metabo_count = as.numeric(metabo_count),
fgRatio_numerator = split_ratio(fgRatio)$numerator,
fgRatio_denominator = split_ratio(fgRatio)$denominator,
bgRatio_numerator = split_ratio(bgRatio)$numerator,
bgRatio_denominator = split_ratio(bgRatio)$denominator,
fold_enrichment=(fgRatio_numerator/fgRatio_denominator)/(bgRatio_numerator/bgRatio_denominator)
)
如何选择特定样本并从字符型为数值型?
1
2
df <-df %>% dplyr::select(sample) %>%
mutate(across(everything(), ~ as.numeric(.)))
如何在dplyr中将列名设置为变量?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 加载必要的库
library(dplyr)
library(stringr)
# 创建示例数据框
example_df <- data.frame(
pathway = c("has00123", "mmu01456", "rno057589")
)
process_dataframe <- function(df, pathway_col = "pathway") {
df %>%
mutate(
!!sym(pathway_col) := str_replace_all(.[[pathway_col]], "\\D+", "map")
)
}
processed_df <- process_dataframe(example_df)
# 打印处理后的数据框
print("处理后的数据框:")
print(processed_df)
如何在dplyr中按照行加和?
1
2
3
4
5
library(dplyr)
df <- tibble(x = c(1, 2, 3), y = c(4, 5, 6)) # 默认情况下,mutate() 不能逐行计算
df <- df %>% rowwise() %>% # 开启逐行处理模式
mutate(sum = sum(c(x, y))) %>%
ungroup() # 恢复为普通数据框