1.如何统计肽段漏切位点的比例?

 
library(dplyr)
 
# 示例数据
draw <- data.frame(
    Missed_cleavages = c(0, 0, 1, 1, 2, 2, 3, 3),
    count = c(100, 150, 80, 70, 30, 20, 10, 5)
)
 
#方法1
sum(draw[draw$Missed_cleavages==0,]$count)/sum(draw$count)
sum(draw[draw$Missed_cleavages==1,]$count)/sum(draw$count)
sum(draw[draw$Missed_cleavages==2,]$count)/sum(draw$count) 
 
#方法2
draw %>%
    group_by(Missed_cleavages) %>%
    summarise(percentage = sum(count) / sum(draw$count)) %>%
    filter(Missed_cleavages %in% c(0, 1, 2))

2.如何反选的列名并转换为小写?

library(dplyr)
# 假设df是你的数据框,samples是包含要保留的列名的向量
df <- data.frame(ID = 1:3, Name = c("A", "B", "C"), AGE = c(20, 25, 30), 
                 Gender = c("M", "F", "M"), SCORE = c(85, 90, 95))
 
samples <- c("ID", "Name")  # 这些列名将保持不变
 
# 方法1: 使用names()和%in%
names(df)[!names(df) %in% samples] <- tolower(names(df)[!names(df) %in% samples])
# 方法2
df <- df %>% rename_with(~ tolower(.), .cols = !all_of(samples))

3.R语言跳出某个循环?

# 打印1到10中的奇数
for (i in 1:10) {
    if (i %% 2 == 0) {  # 如果是偶数
        next  # 跳过本轮循环
    }
    print(paste("奇数:", i))
}

4.如何批量数据读取?

library(dplyr)
df_list <- lapply(path, function(x) read.delim(x, check.names = FALSE) %>% mutate(source = x)) 
data <- purrr::map_dfr(path, ~ read.delim(.x, check.names = FALSE) %>% mutate(source = .x))

5.如何安装行加和以及取得最大值?

library(dplyr)
# 创建示例数据
df <- tibble(
    a = 1:3,
    b = 4:6,
    c = 7:9
)
 
# 计算每行的总和
df %>%
    rowwise() %>%
    mutate(total = sum(c(a, b, c)))
 
 
# 计算每行最大值
df %>%
    rowwise() %>%
    mutate(max_ab = max(a, b,c))