Created
December 1, 2024 16:15
-
-
Save chenpanliao/fc3ed86f8b4eff2ab85113891af5eb14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 預先切過的頭尾 | |
| # 當然也可以是完全還沒切過的樣子,例如 cbind(f1 = 1, f2 = 100) | |
| mat.in <- | |
| cbind( | |
| f1 = c(3, 23, 47, 78), | |
| f2 = c(9, 26, 55, 88) | |
| ) | |
| # 想要再切的頭尾 | |
| # 可以包括已經被切掉的部份 | |
| # 可以超出原本的最大範圍 | |
| # 可以有重復切到的部份 | |
| mat.trim <- | |
| cbind( | |
| f1 = c(1, 42, 76, 80, 81), | |
| f2 = c(10, 46, 78, 82, 86) | |
| ) | |
| #### 以下為算法,可自行改寫成 function #### | |
| # 重建原始資料 x | |
| x0 <- seq.int(min(mat.in, mat.trim), max(mat.in, mat.trim)) | |
| # 留下在 mat.in 之內 (包括頭尾) 的部份 | |
| remianed <- | |
| apply(mat.in, 1, function(x){ | |
| x0 %in% seq(x[1], x[2]) | |
| }) |> | |
| apply(1, any) | |
| x0.remained <- x0[remianed] | |
| # 切掉所有被 mat.trim 包括的部份 | |
| cutted <- | |
| apply(mat.trim, 1, function(x){ | |
| x0.remained %in% seq(x[1], x[2]) | |
| }) |> | |
| apply(1, any) |> | |
| (function(x) !x)() | |
| x0.cutted.remained <- x0.remained[cutted] | |
| # 按是否連續來判斷誰是頭誰是尾 | |
| index <- | |
| c(1, | |
| which(diff(x0.cutted.remained) > 1), | |
| which(diff(x0.cutted.remained) > 1) + 1, | |
| length(x0.cutted.remained)) |> | |
| sort() |> | |
| matrix(ncol = 2, byrow = T, dimnames = list(NULL, c("f1", "f2"))) | |
| x0.cutted.remained[index] |> | |
| matrix(ncol = 2, byrow = F, dimnames = list(NULL, c("f1", "f2"))) | |
| # 為所求 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment