Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## 2024-07-04 - R μ–Έμ–΄μ—μ„œ 루프 λ‚΄ 데이터 ν”„λ ˆμž„ 탐색 병λͺ© μ΅œμ ν™”
**Learning:** Rμ—μ„œ 루프λ₯Ό λŒλ©΄μ„œ 맀번 데이터 ν”„λ ˆμž„μ„ μ„œλΈŒμ…‹νŒ…(subsetting)ν•˜λŠ” μž‘μ—…μ€ 볡사 μ˜€λ²„ν—€λ“œλ‘œ 인해 맀우 느렀질 수 μžˆμŠ΅λ‹ˆλ‹€. 특히 곡톡 λ¬Έν•­ μˆ˜κ°€ λ§Žμ•„μ§ˆ 경우 O(N^2)의 λΉ„νš¨μœ¨μ„ μ΄ˆλž˜ν•©λ‹ˆλ‹€.
**Action:** 루프 λ‚΄μ—μ„œ μˆ˜ν–‰ν•˜λ˜ 데이터 ν”„λ ˆμž„ 쑰회λ₯Ό 루프 μ™ΈλΆ€μ—μ„œ ν•œ λ²ˆμ— `as.character(unlist(...))`둜 μ²˜λ¦¬ν•˜λŠ” 벑터 μ—°μ‚°μœΌλ‘œ λ³€κ²½ν•˜μ—¬ νƒ€μž… λ³€ν™˜ 없이 O(1) μˆ˜μ€€μœΌλ‘œ μ„±λŠ₯을 크게 ν–₯μƒμ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€.
## 2024-07-06 - Replacing multiple grep() with a single !grepl()
**Learning:** Combining multiple negative `grep()` outputs (e.g., `x[-c(grep("A", x), grep("B", x))]`) is inefficient and unsafe in base R. If no patterns match, it evaluates to `x[-integer(0)]` which unexpectedly returns `character(0)`, wiping out the entire vector. Furthermore, multiple `grep` calls are computationally more expensive than a single `grepl`.
**Action:** Always prefer `!grepl("^(A|B|C)", x)` for filtering vectors to improve execution performance and prevent unexpected empty vectors.
10 changes: 1 addition & 9 deletions R/aFIPC.R
Original file line number Diff line number Diff line change
Expand Up @@ -603,15 +603,7 @@ autoFIPC <-
# IPD estimation
IPDParmNames <- OldScaleParms$name
IPDParmNames <- IPDParmNames[!duplicated(IPDParmNames)]
IPDParmNames <-
IPDParmNames[
-c(
grep("^MEAN", IPDParmNames),
grep("^COV", IPDParmNames),
grep("^ak", IPDParmNames),
grep("^d0$", IPDParmNames)
)
]
IPDParmNames <- IPDParmNames[!grepl("^(MEAN|COV|ak|d0$)", IPDParmNames)]
IPDParmNames <- as.character(IPDParmNames)

mirt::mirtCluster()
Expand Down
11 changes: 11 additions & 0 deletions test_grep.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
x <- c("MEAN1", "COV2", "ak3", "d0", "d01", "other")
orig <- x[-c(grep("^MEAN", x), grep("^COV", x), grep("^ak", x), grep("^d0$", x))]
new_way <- x[!grepl("^(MEAN|COV|ak|d0$)", x)]
print("Original:")
print(orig)
print("New way:")
print(new_way)

x_empty <- c("other", "something_else")
print(x_empty[-c(grep("^MEAN", x_empty), grep("^COV", x_empty), grep("^ak", x_empty), grep("^d0$", x_empty))])
print(x_empty[!grepl("^(MEAN|COV|ak|d0$)", x_empty)])
Loading