假设我有一个类似于:
cell_type_A cell_type_B gene score_A score_B
<fct> <fct> <chr> <dbl> <dbl>
1 cd4_naive_A cd4_naive_B EEF1A1 0.447 0.440
2 cd4_naive_A cd4_naive_B RPL5 0.405 0.466
3 cd4_naive_A cd4_naive_B RPL3 0.310 0.377
4 cd4_naive_A cd4_naive_B RPL4 0.425 0.264
5 cd4_naive_A NA RPS5 0.335 0
6 cd4_naive_A NA GNB2L1 0.329 0
7 NA cd4_naive_B TMEM66 0 0.563
8 NA cd4_naive_B BTG1 0 1.10
9 cd4_helper_A cd4_helper_B EEF1D 0.335 0.262
10 cd4_helper_A NA RPL6 0.260 0我想计算A和B两种细胞类型对之间的相关性。
通常情况下,这将完成以下工作:
tibble %>%
group_by(cell_type_A, cell_type_B) %>%
summarize(cor(score_A, score_B))但是,我想在计算中包括几十行带有NAs的行。
在上面的例子中,在计算cd4_naive_A与cd4_naive_B的相关性时,应该包括第1-8行(而不是1-4行)。
如何使用tidyverse语法来实现这一点?
谢谢。
发布于 2022-01-11 12:49:46
在这种情况下,您可以将tidy::fill与前一个NA值一起考虑。
df <- data.frame( cell_type_A = c(rep('cd4_naive_A', 6), rep(NA, 2)),
cell_type_B = c(rep('cd4_naive_B', 4),rep(NA, 2),rep('cd4_naive_B', 2)))
cell_type_A cell_type_B
1 cd4_naive_A cd4_naive_B
2 cd4_naive_A cd4_naive_B
3 cd4_naive_A cd4_naive_B
4 cd4_naive_A cd4_naive_B
5 cd4_naive_A <NA>
6 cd4_naive_A <NA>
7 <NA> cd4_naive_B
8 <NA> cd4_naive_B
tidyr::fill(df, c(cell_type_A, cell_type_B), .direction = 'down')
cell_type_A cell_type_B
1 cd4_naive_A cd4_naive_B
2 cd4_naive_A cd4_naive_B
3 cd4_naive_A cd4_naive_B
4 cd4_naive_A cd4_naive_B
5 cd4_naive_A cd4_naive_B
6 cd4_naive_A cd4_naive_B
7 cd4_naive_A cd4_naive_B
8 cd4_naive_A cd4_naive_Bhttps://stackoverflow.com/questions/70666965
复制相似问题