andrewallenbruce / fuimus

Functions That Have Been
https://andrewallenbruce.github.io/fuimus/
Other
2 stars 0 forks source link

Functions to explore #1

Open andrewallenbruce opened 4 months ago

andrewallenbruce commented 4 months ago
andrewallenbruce commented 1 month ago
remove_quotes <- function(x) {
   stringfish::sf_gsub(x, '["\']', "")
}
andrewallenbruce commented 1 month ago

library(data.table)

# Given vector with some letters missing
letters_vector <- c("a", "b", "d", "e", "f", "h", "i", "j")

# Create a data.table with letters
dt <- data.table(letter = letters_vector)

# Function to check for sequences
check_sequences <- function(dt, seq_length) {
  for (i in 1:(nrow(dt) - seq_length + 1)) {
    if (all(diff(as.numeric(factor(dt$letter[i:(i + seq_length - 1)], levels = letters))) == 1)) {
      return(TRUE)
    }
  }
  return(FALSE)
}

# Check for sequences of 3 or more letters
found_sequence <- check_sequences(dt, 3)
print(found_sequence)

library(dplyr)

# Given vector with some letters missing
letters_vector <- c("a", "b", "d", "e", "f", "h", "i", "j")

# Create a data frame
df <- data.frame(letter = letters_vector)

# Function to check for sequences
check_sequences <- function(df, seq_length) {
  df <- df %>%
    mutate(index = as.numeric(factor(letter, levels = letters))) %>%
    arrange(index)

  for (i in 1:(nrow(df) - seq_length + 1)) {
    if (all(diff(df$index[i:(i + seq_length - 1)]) == 1)) {
      return(TRUE)
    }
  }
  return(FALSE)
}

# Check for sequences of 3 or more letters
found_sequence <- check_sequences(df, 3)
print(found_sequence)
andrewallenbruce commented 3 days ago

tidyselect solution lazyeval tidytext