python - 按第一列定义的间隔有效地平均第二列

标签 python perl r awk data.table

数据文件中有两个数字列。我需要根据第一列的间隔(例如 100)计算第二列的平均值。

我可以在 R 中编写此任务,但我的 R 代码对于相对较大的数据文件(数百万行,第一列的值在 1 到 33132539 之间变化)来说确实很慢。

在这里我展示了我的 R 代码。我怎样才能把它调得更快?其他基于 perl、python、awk 或 shell 的解决方案值得赞赏。

提前致谢。

(1) 我的数据文件(制表符分隔,百万行)

5380    30.07383\n
5390    30.87\n
5393    0.07383\n
5404    6\n
5428    30.07383\n
5437    1\n
5440    9\n
5443    30.07383\n
5459    6\n
5463    30.07383\n
5480    7\n
5521    30.07383\n
5538    0\n
5584    20\n
5673    30.07383\n
5720    30.07383\n
5841    3\n
5880    30.07383\n
5913    4\n
5958    30.07383\n

(2)我想得到什么,这里interval = 100

intervals_of_first_columns, average_of_2nd column_by_the_interval
100, 0\n
200, 0\n
300, 20.34074\n
400, 14.90325\n
.....

(3)R代码

chr1 <- 33132539 # set the limit for the interval
window <- 100 # set the size of interval

spe <- read.table("my_data_file", header=F) # read my data in
names(spe) <- c("pos", "rho") # name my data 

interval.chr1 <- data.frame(pos=seq(0, chr1, window)) # setup intervals
meanrho.chr1 <- NULL # object for the mean I want to get

# real calculation, really slow on my own data.
for(i in 1:nrow(interval.chr1)){
  count.sub<-subset(spe, chrom==1 & pos>=interval.chr1$pos[i] & pos<=interval.chr1$pos[i+1])
  meanrho.chr1[i]<-mean(count.sub$rho)
}

最佳答案

您实际上不需要设置输出 data.frame,但您可以根据需要设置。这是我的编码方式,我保证它会很快。

> dat$incrmt <- dat$V1 %/% 100
> dat
     V1       V2 incrmt
1  5380 30.07383     53
2  5390 30.87000     53
3  5393  0.07383     53
4  5404  6.00000     54
5  5428 30.07383     54
6  5437  1.00000     54
7  5440  9.00000     54
8  5443 30.07383     54
9  5459  6.00000     54
10 5463 30.07383     54
11 5480  7.00000     54
12 5521 30.07383     55
13 5538  0.00000     55
14 5584 20.00000     55
15 5673 30.07383     56
16 5720 30.07383     57
17 5841  3.00000     58
18 5880 30.07383     58
19 5913  4.00000     59
20 5958 30.07383     59

> with(dat, tapply(V2, incrmt, mean, na.rm=TRUE))
      53       54       55       56       57       58       59 
20.33922 14.90269 16.69128 30.07383 30.07383 16.53692 17.03692 

您本可以完成更少的设置(使用此代码跳过 incrmt 变量:

    > with(dat, tapply(V2, V1 %/% 100, mean, na.rm=TRUE))
      53       54       55       56       57       58       59 
20.33922 14.90269 16.69128 30.07383 30.07383 16.53692 17.03692 

如果您希望结果可用于某事:

by100MeanV2 <- with(dat, tapply(V2, V1 %/% 100, mean, na.rm=TRUE))

关于python - 按第一列定义的间隔有效地平均第二列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7538552/

相关文章:

r - 提取第 N 次出现的行号

r - R中的rep函数

python - 如何查看 sqlite3 Python 模块在 Ubuntu 16.04 上使用的 sqlite3 二进制文件?

python - 首先对较高层的数据帧进行排名,然后对较低层的数据帧进行排名

mysql - 记录集迭代

regex -\w 是否匹配 Unicode 标准中定义的所有字母数字字符?

python - 将用户输入限制在 Python 中的范围内

python:在字符串中查找子字符串

perl - 从远程服务器获取文件 - Perl/Unix

r - 如何使用 geom_label_repel 避免图中标签重叠?