Skip to content

Instantly share code, notes, and snippets.

@thinhong
thinhong / string-before.R
Created July 13, 2025 04:06
get string before a pattern
# Here the pattern is _, get all text before _
colnames(df_plot) |>
str_extract("^[^_]+")
@thinhong
thinhong / case-when.R
Created June 16, 2025 16:06
[case_when]
df <- df |>
mutate(
agegr = case_when(
agegr %in% c("< 6th", "<6 THANG") ~ "<6m",
agegr %in% c("6-9 THANG", "6 - 9th") ~ "6m-9m",
agegr %in% c("Trên 10 tuổi", "> 10T") ~ ">10y",
.default = agegr
)
)
@thinhong
thinhong / world-map.R
Created June 5, 2025 15:26
[plot world map] maybe SEA map
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
world <- ne_countries(scale = "medium", returnclass = "sf")
sea_region <- world |>
filter(
name_long %in% c(
"Cambodia",
@thinhong
thinhong / infl-point.R
Last active May 25, 2025 05:16
[find inflection point] tìm điểm uốn của hàm
x <- seq(-10, 10, 0.01)
y <- 2*x^3 - x^2 + 3*x - 5
# inflection point is where the second derivative changes signs
# formally, it is when second derivative is 0 or undefined, but we are doing it numerically, we potentially miss the only one point where it's 0, so find the point where it changes sign is more feasible
## compute second derivative
d2 <- diff(y, differences = 2)
## find where it changes signs
infl_idx <- which(d2[-length(d2)] * d2[-1] < 0) + 1
x[infl_idx]
@thinhong
thinhong / gadm_gpkg.R
Created May 13, 2025 09:31
[read gpkg data gadm]
tmp <- st_read("../data/map/gadm41_VNM.gpkg", layer = "ADM_ADM_1")
tmp$NAME_1 <- gsub(" - ", " ", tmp$NAME_1)
tmp$VARNAME_1 <- gsub(" - ", " ", tmp$VARNAME_1)
@thinhong
thinhong / gtsummary-or.md
Last active July 27, 2025 02:55
[gtsummary OR] univariable and multivariable

Univariable

tb1 <- train[, c("phando2", all_feats)] |>
  tbl_uvregression(
    method = glm,
    y = phando2,
    method.args = list(family = binomial),
    exponentiate = TRUE,
    include = c(GI, Experience, Countryofpractice, starts_with("PS_")),
@thinhong
thinhong / qt-img-link.md
Last active March 12, 2025 05:27
[quarto markdown add link to an image] figure that has url
@thinhong
thinhong / gt-merge-tbl.R
Created February 19, 2025 15:33
[gtsummary tbl_summary merge 2 tables]
t1 <- train |>
# Use select to sort the order of rows in the table because it will display in the order of the columns
select(Tuoicao, Sex, TSUTDT, TSPDT, TSGD, HTL, Ruou, BMI, kichthuoclon, Paris, rightcolon, NBI, WLE, nhompolyp) |>
tbl_uvregression(
method = glm,
y = nhompolyp,
method.args = list(family = binomial),
exponentiate = TRUE,
hide_n = TRUE,
include = c(Tuoicao, Sex, TSUTDT, TSPDT, TSGD, HTL, Ruou, BMI, kichthuoclon, Paris, rightcolon, NBI, WLE),
@thinhong
thinhong / manual-time.R
Created January 6, 2025 07:27
[xem manual cách ghi thời gian]
?strftime
@thinhong
thinhong / pvkey.md
Created December 24, 2024 15:17
[store private key and integrate github action]

First, create an .Renviron file

usethis::edit_r_environ(scope = "project")

Add all the private data here

ZOTERO_API_KEY=key
ZOTERO_USER_ID=id