我有一个8Million x 10列的矩阵。该矩阵的子集如下所示:
df <- matrix(c("0.221,0.767,0.011" ,"0.97,0.03,0", "0.967,0.033,0",
"0.922,0.077,0.001", "0.664,0.329,0.007", "0.953,0.047,0",
"0.993,0.007,0", "0.994,0.006,0", "0.992,0.008,0",
"0.477,0.52,0.002", "0.953,0.047,0", "0.993,0.007,0",
"0.994,0.006,0", "0.992,0.008,0", "0.476,0.521,0.003",
"0.952,0.048,0" , "0.993,0.007,0", "0.994,0.006,0",
"0.992,0.008,0", "0.485,0.512,0.003"), ncol=5, byrow=TRUE)
我需要在此矩阵中进行一系列计算。例如,如果x <- df[1,1]
,那么我需要将第一个元素0.221
乘以两倍,并将结果加到第二个元素0.767
。
结果如下:
[,1] [,2] [,3] [,4] [,5]
[1,] 1.209 1.970 1.967 1.921 1.657
[2,] 1.953 1.993 1.994 1.992 1.474
[3,] 1.953 1.993 1.994 1.992 1.473
[4,] 1.952 1.993 1.994 1.992 1.482
我使用@erocoar提供的这个解决方案,适用于小型数据集:
out <- lapply(strsplit(df, ","), function(x) {
x <- as.numeric(x)
return((2 * x[1]) + x[2])
})
out <- do.call(rbind, out)
dim(out) <- dim(df)
然而,这个解决方案需要大量内存,并且在我的真实数据集中需要花费大量时间。我也试过这个:
y = function(x) {a <- strsplit(x, ",")
z <- as.numeric(a)
return((2 * z[1]) + z[2])
}
m <- matrix(-9, nrow = nrow(df), ncol = ncol(df))
m[] <- vapply(df, y, numeric(1))
但是这给我一个关于格式的错误。
更新
原始文件来自存储遗传信息的vcf文件(遗传变异文件),
首先我用read.vcfR
读取了文件,这里是代码:
library(vcfR)
vcf <- read.vcfR("/mnt/lustre/scratch/home/proximal.vcf.gz")
df <- vcf@gt
df <- dosages[, -1]
df <- gsub(".+:.+:(.*)", "\\1", dosages, perl = TRUE)
然后我想在帖子的第一部分进行计算
答案 0 :(得分:1)
您不需要任何循环,只需再次读取数据即可。这次sep
字符定义了两次。对于粘贴sep=\n
和read.table
sep=\n
。
首先将矩阵设为dataframe
。将列逐列粘贴在一起,然后再次将其读回。
dd=do.call(paste,c(data.frame(df),sep="\n"))
m=read.table(text=dd,sep=",")
matrix(2*m[,1]+m[,2],4,byrow=T)
[,1] [,2] [,3] [,4] [,5]
[1,] 1.209 1.970 1.967 1.921 1.657
[2,] 1.953 1.993 1.994 1.992 1.474
[3,] 1.953 1.993 1.994 1.992 1.473
[4,] 1.952 1.993 1.994 1.992 1.482