0

大家好,我之前在这里问过一个类似的问题,我得到了一些非常棒的答案。但事实证明我有更多的数据需要处理。所以我有这种格式的样本数据,它是使用 head(data) 生成的。所以我有 3 个带有自己的时间和速度数据的样本……我并没有将速度用于我的实际数据

Time Speed Time.1 Speed.1 Time.2 Speed.2

Error in unique.default(x) : unique() applies only to vectors

各位大神能不能告诉我怎么办?我可以用公式在 excel 中做到这一点,但我有太多数据导致 excel 崩溃,所以我真的需要“R”,但对此知之甚少......谢谢大家......

4

1 回答 1

0

运行相同的代码。你为什么要编辑by=3?你想创建idx = 1, 3, 5 .... by=3, 将创建1, 4, 7.... 你的数据,只要是格式的Time, data, Time, data, Time, data, ..., ...,都可以用同样的代码

require(IRanges)
# by equals 2 because we want to get the `Time` column index every time
idx <- seq(1, ncol(data), by=2)
# idx is now 1, 3, 5. It will be passed one value at a time to `i`.
# that is, `i` will take values 1 first, then 3 and then 5 and each time
# the code within is executed.
o <- lapply(idx, function(i) {  
    ir1 <- IRanges(start=seq(0, max(data[[i]]), by=401), width=401)
    ir2 <- IRanges(start=data[[i]], width=1)
    t <- findOverlaps(ir1, ir2)
    d <- data.frame(mean=tapply(data[[i+1]], queryHits(t), mean))
    cbind(as.data.frame(ir1), d)
})

给我这个数据:

# > o
# [[1]]
#   start end width mean
# 1     0 400   401 1.05
# 
# [[2]]
#   start end width mean
# 1     0 400   401  1.1
# 
# [[3]]
#   start end width     mean
# 1     0 400   401 1.383333
于 2013-02-22T07:18:48.327 回答