Skip to content

Instantly share code, notes, and snippets.

View DrNickRedfern's full-sized avatar

Nick Redfern DrNickRedfern

View GitHub Profile
@DrNickRedfern
DrNickRedfern / kyoto.omp.json
Created April 8, 2024 19:16
Kyoto: variation on the Tokyo theme for Oh My Posh
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"alignment": "left",
"segments": [
{
"style": "plain",
"foreground": "#51829b",
"template": "\u2552[<#ffffff>\uF007</> {{ .UserName }}@{{ .HostName }}]",
{
"config": {
"terminology": {
"item": "Researcher",
"items": "Researchers",
"link": "Co-authorship link",
"links": "Co-authorship links",
"link_strength": "Co-authorships",
"total_link_strength": "Total co-authorships"
},
@DrNickRedfern
DrNickRedfern / control_flow_while.R
Last active September 8, 2022 10:19
An illustration of a while loop in R
# Set the initial value of a
a <- 1
# Print the current value of a while a is less than or equal to 5
while (a <= 5){
# Print the current value of a
print(a)
# Increment the value of a by 1
a = a + 1
}
@DrNickRedfern
DrNickRedfern / control_flow_loop.R
Last active September 8, 2022 09:40
An illustration of a for loop in R
a <- 1:3
b <- 4:6
# Loop over every element in a and print the result of the i-th elememt of a plus 10
for (i in seq_along(a)){
print(i + 10)
}
[1] 11
[1] 12
@DrNickRedfern
DrNickRedfern / control_flow_ifelse.R
Last active September 8, 2022 10:16
An illustration of an ifelse statement in R
a <- 2
if (a > 0){
print(paste0(a, " is a positive number"))
} else if (a < 0) {
print(paste0(a, " is negative number"))
} else {
print(paste0(a, " equals zero"))
}
@DrNickRedfern
DrNickRedfern / control_flow_if.R
Created September 8, 2022 08:52
An illustration of an if statement in R
# The code in the braces is run when the condition is true
a <- 2
if (a > 0) {print("positive")}
[1] "positive"
# The code in the braces is NOT run when the condition is false
b <- -3
if (b > 0) {print("positive")}
@DrNickRedfern
DrNickRedfern / vectorisation_2.R
Created September 8, 2022 07:58
Another illustration of vectorisation in R
# Create two vectors of equal length
X <- c(1, 3, 5, 8)
Y <- c(2, 4, 6, 7)
# Add the vectors together
X + Y
[1] 3 7 11 15
@DrNickRedfern
DrNickRedfern / vectorisation_1.R
Last active September 8, 2022 07:58
An illustration of vectorisation in R
# The red colour channel of an image using the [0, 1] scale
RED <- c(1.000 0.259 0.557 0.329 1.000 0.259 0.263 0.263 1.000 0.263)
# Convert RED to the [0, 255] scale by multiplying by 255
red <- RED * 255
# Inspect the rescaled object red
red
[1] 255.000 66.045 142.035 83.895 255.000 66.045 67.065 67.065 255.000 67.065
@DrNickRedfern
DrNickRedfern / sp.R
Last active August 22, 2022 13:15
An edited version of syuzhet::simple_plot() that returns a data frame of the fitted smoothers
sp <- function (raw_values, title = "Syuzhet Plot", legend_pos = "top",
lps = 10, window = 0.1, plot = TRUE)
{
wdw <- round(length(raw_values) * window)
rolled <- rescale(zoo::rollmean(raw_values, k = wdw, fill = 0))
half <- round(wdw/2)
rolled[1:half] <- NA
end <- length(rolled) - half
rolled[end:length(rolled)] <- NA
trans <- get_dct_transform(raw_values, low_pass_size = lps,
@DrNickRedfern
DrNickRedfern / syuzhet::simple_plot.R
Created August 16, 2022 08:31
The simple_plot function from the syuzhet package
function (raw_values, title = "Syuzhet Plot", legend_pos = "top",
lps = 10, window = 0.1)
{
wdw <- round(length(raw_values) * window)
rolled <- rescale(zoo::rollmean(raw_values, k = wdw, fill = 0))
half <- round(wdw/2)
rolled[1:half] <- NA
end <- length(rolled) - half
rolled[end:length(rolled)] <- NA
trans <- get_dct_transform(raw_values, low_pass_size = lps,