[英]Split a data.table at position
I want to calculate the mean of a column in a data.table way by using row numbers or a vector with position.我想通过使用行号或带有 position 的向量来计算 data.table 方式中列的平均值。
Here is a sample data and a postion vector:这是一个样本数据和一个位置向量:
x <- data.table(a = c(1,2,3,4,5,6,7,8))
pos <- c(3,5)
I tried:我试过了:
x[mean(a), by = pos]
So i want the mean from row 1:2, 3:4 and 5:8.所以我想要第 1:2、3:4 和 5:8 行的平均值。
Here is an option:这是一个选项:
x[, mean(a), cumsum(replace(rep(0, nrow(x)), pos, 1L))]
output: output:
cumsum V1
1: 0 1.5
2: 1 3.5
3: 2 6.5
You can use findInterval
/ cut
to create groups based on pos
:您可以使用
findInterval
/ cut
根据pos
创建组:
library(data.table)
x[, mean(a), findInterval(a, pos)]
# findInterval V1
#1: 0 1.5
#2: 1 3.5
#3: 2 6.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.