gpt4 book ai didi

r - 使用 data.table : how many 2x2 non NA values there are among the variables? 计算

转载 作者:行者123 更新时间:2023-12-04 02:16:57 24 4
gpt4 key购买 nike

假设我有这个 data.table(实际数据是 25061 x 5862):

require(data.table)
df
# gene P1 P2 P3 P4 P5
# 1: gene1 0.111 0.319 0.151 NA -0.397
# 2: gene10 1.627 2.252 1.462 -1.339 -0.644
# 3: gene2 -1.766 -0.056 -0.369 1.910 0.981
# 4: gene3 -1.346 1.283 0.322 -0.465 0.403
# 5: gene4 -0.783 NA -0.005 1.761 0.066
# 6: gene5 0.386 -0.309 -0.886 -0.072 0.161
# 7: gene6 0.547 -0.144 -0.725 -0.133 1.059
# 8: gene7 0.785 -1.827 0.986 1.555 -0.798
# 9: gene8 -0.186 NA 0.401 0.900 -1.075
# 10: gene9 -0.177 1.497 -1.370 -1.628 -1.044

我想知道如何利用 data.table 结构,我可以有效地计算每对基因值有多少对没有 NA 的夫妇。例如,对于基因 1、基因 2 对,我希望结果为 4。

使用基础 R,我这样做:
calc_nonNA <- !is.na(df[, -1, with=F])
Effectifs <- calc_nonNA %*% t(calc_nonNA)
# or, as suggested by @DavidArenburg and @Khashaa, more efficiently:
Effectifs <- tcrossprod(calc_nonNA)

但是,对于大型 df,需要几个小时......

我想要的输出,提供的例子是这样的:
       gene1 gene10 gene2 gene3 gene4 gene5 gene6 gene7 gene8 gene9
gene1 4 4 4 4 3 4 4 4 3 4
gene10 4 5 5 5 4 5 5 5 4 5
gene2 4 5 5 5 4 5 5 5 4 5
gene3 4 5 5 5 4 5 5 5 4 5
gene4 3 4 4 4 4 4 4 4 4 4
gene5 4 5 5 5 4 5 5 5 4 5
gene6 4 5 5 5 4 5 5 5 4 5
gene7 4 5 5 5 4 5 5 5 4 5
gene8 3 4 4 4 4 4 4 4 4 4
gene9 4 5 5 5 4 5 5 5 4 5

数据
df <- structure(list(gene = c("gene1", "gene10", "gene2", "gene3", 
"gene4", "gene5", "gene6", "gene7", "gene8", "gene9"), P1 = c(0.111,
1.627, -1.766, -1.346, -0.783, 0.386, 0.547, 0.785, -0.186, -0.177
), P2 = c(0.319, 2.252, -0.056, 1.283, NA, -0.309, -0.144, -1.827,
NA, 1.497), P3 = c(0.151, 1.462, -0.369, 0.322, -0.005, -0.886,
-0.725, 0.986, 0.401, -1.37), P4 = c(NA, -1.339, 1.91, -0.465,
1.761, -0.072, -0.133, 1.555, 0.9, -1.628), P5 = c(-0.397, -0.644,
0.981, 0.403, 0.066, 0.161, 1.059, -0.798, -1.075, -1.044)), .Names = c("gene",
"P1", "P2", "P3", "P4", "P5"), class = c("data.table", "data.frame"
), row.names = c(NA, -10L), .internal.selfref = <pointer: 0x022524a0>)

最佳答案

使用 dplyr,将数据宽转换为长,然后连接到自身并汇总。不确定它是否比您的解决方案更有效,对任何人进行一些基准测试?

library(dplyr)
library(tidyr)

# reshaping from wide to long
x <- df %>% gather(key = P, value = value, -c(1)) %>%
mutate(value=(!is.na(value)))

# result
left_join(x,x,by="P") %>%
group_by(gene.x,gene.y) %>%
summarise(N=sum(value.x & value.y)) %>%
spread(gene.y,N)

编辑:
可惜,这个 dplyr 解决方案对于更大的数据集 2600x600 失败,无法连接到自身 - internal vecseq reached physical limit ,大约 2^31 行...

顺便说一下,这里是 t 的基准测试对比 tcrossprod :
library(ggplot2)
library(microbenchmark)

op <- microbenchmark(
BASE_t={
calc_nonNA <- !is.na(df[, -1, with=F])
calc_nonNA %*% t(calc_nonNA)
},
BASE_tcrossprod={
calc_nonNA <- !is.na(df[, -1, with=F])
tcrossprod(calc_nonNA)
},
times=10
)

qplot(y=time, data=op, colour=expr) + scale_y_log10()

enter image description here

关于r - 使用 data.table : how many 2x2 non NA values there are among the variables? 计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31313049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com