First, a function to group the column names that are within 0.1 of each other:
group_vector <- function (vec, threshold=0.1) {
vec <- sort(vec)
groups <- as.list(1:length(vec))
ngroups <- 1
for (i in 2:length(vec)) {
if ((vec[i]-vec[i-1])<=threshold) {
groups[[ngroups]] <- c(groups[[ngroups]], i)
} else {
ngroups <- ngroups + 1
groups[[ngroups]] <- i
}
}
groups[1:ngroups]
}
Then, a function that calculates the maximum value for each row within grouped columns, and renames the new column as the average value:
group_data_max <- function (original_data, threshold) {
vec <- as.numeric(names(original_data))
original_data <- original_data[, order(vec)]
groups <- group_vector(vec=vec, threshold=threshold)
new.data <- data.frame(lapply(groups, function (x) {
apply(data.frame(original_data[, x]), 1, max)
}))
names(new.data) <- sapply(groups, function (x) mean(sort(vec)[x]))
new.data
}
Testing on an example data set:
set.seed(1000)
example.data <- data.frame(replicate(length(vec), sapply(rnorm(1000, 0, 1e-3), max, 0)))
names(example.data) <- round(runif(10, 0, 2), 3)
head(example.data)
1.656 0.894 0.708 1.307 0.818 1.899
1 0.000000e+00 0.0020804209 1.222081e-03 0.0000000000 0.0006729516 0.0022213225
2 0.000000e+00 0.0000000000 0.000000e+00 0.0000000000 0.0000000000 0.0000000000
3 4.112631e-05 0.0008626092 0.000000e+00 0.0006871080 0.0003988231 0.0015567983
4 6.393884e-04 0.0006410248 9.315820e-04 0.0001706286 0.0000000000 0.0000000000
5 0.000000e+00 0.0000000000 9.995761e-05 0.0000000000 0.0006471052 0.0005108526
6 0.000000e+00 0.0000000000 0.000000e+00 0.0013138954 0.0012562174 0.0005090567
0.994 1.641 1.751 1.138
1 0.0003542045 0.0000000000 0.000000e+00 0.0006481930
2 0.0003942478 0.0000000000 0.000000e+00 0.0015370211
3 0.0013130688 0.0000000000 8.991744e-04 0.0005104541
4 0.0000000000 0.0001117057 1.011685e-03 0.0002280315
5 0.0001000137 0.0000000000 1.733699e-05 0.0000000000
6 0.0000000000 0.0020320953 3.266437e-04 0.0011959593
Result:
group_data_max(example.data, threshold=0.1)
0.708 0.902 1.138 1.307 1.68266666666667
1 1.222081e-03 0.0020804209 0.0006481930 0.0000000000 0.000000e+00
2 0.000000e+00 0.0003942478 0.0015370211 0.0000000000 0.000000e+00
3 0.000000e+00 0.0013130688 0.0005104541 0.0006871080 8.991744e-04
4 9.315820e-04 0.0006410248 0.0002280315 0.0001706286 1.011685e-03
5 9.995761e-05 0.0006471052 0.0000000000 0.0000000000 1.733699e-05
6 0.000000e+00 0.0012562174 0.0011959593 0.0013138954 2.032095e-03
1.899
1 0.0022213225
2 0.0000000000
3 0.0015567983
4 0.0000000000
5 0.0005108526
6 0.0005090567