1. 如何计算标准差的bar?参考1
library(tidyverse)
 
#1. 手动计算
stat_data <- iris %>% 
    group_by(Species) %>% 
    summarise(
        mean_sepal_length = mean(Sepal.Length, na.rm = T),
        se_sepal_length = sd(Sepal.Length) / sqrt(length(Sepal.Length)),
        .groups = "drop"   
    ) %>%
    #计算上下限
    mutate(
        lower = mean_sepal_length - se_sepal_length,
        upper = mean_sepal_length + se_sepal_length,
    ) 
 
#2. 使用函数快速计算
stat_data2 <- iris %>% 
    group_by(Species) %>%
    summarise(y = mean_se(Sepal.Length)) %>% 
    unnest(cols = c(y))
 
#验证结果的一致性
p <- iris %>% 
    ggplot(aes(Species, Sepal.Length)) +
    stat_summary(geom = "bar") +
    stat_summary(geom = "errorbar")
 
layer_data(p, 2) #获取图形数据
 
 
  1. 如何读取gff文件? 读取gft文件 gff
library(rtracklayer)
library(tidyverse)
 
gff <- rtracklayer::import(example_gff)
gff2<-as.data.frame(gff)
  1. 如何同时传入多个列表 进行配对循环?
imp.list <- list(c(1, 2, 3), c(4, 5, 6))
hits <- list(c(1, 2), c(2, 3))
mapply(function(x, y) { sum(x[y]) }, imp.list, hits)
  1. 如何快速将2列的列表转换为deframe?
library(tidyverse)
df <- tibble(id = c("A", "B", "C"), value = c(10, 20, 30))
result <- df %>% deframe()
print(result)
  1. 如何按照多个因素设置因子的排序?(GO) GO排序
draw$label <- forcats::fct_reorder2(draw$label, draw$count, draw$namespace)
  1. 如果提供代码运行速度? 推荐方法2 3
method1 = function(n) {
  vec = NULL # Or vec = c()
  for (i in seq_len(n))
    vec = c(vec, i)
  vec
}
Method 2 creates an object of the final length and then changes the values in the object by subscripting:
方法 2 创建一个最终长度的对象,然后通过下标更改对象中的值:
 
method2 = function(n) {
  vec = numeric(n)
  for (i in seq_len(n))
    vec[i] = i
  vec
}
Method 3 directly creates the final object:
方法三直接创建最终对象:
method3 = function(n) seq_len(n)

Footnotes

  1. https://rstudio-pubs-static.s3.amazonaws.com/1062575_9b16a29e94ec47f195a874bee7931f4a.html] ↩