wincowgerDEV / OpenSpecy-package

Analyze, Process, Identify, and Share, Raman and (FT)IR Spectra
http://wincowger.com/OpenSpecy-package/
Creative Commons Attribution 4.0 International
23 stars 11 forks source link

[Feature]: Add support for vectors in addition to Open Specy objects #166

Open wincowgerDEV opened 6 months ago

wincowgerDEV commented 6 months ago

Guidelines

Description

I know we had this chat a while back but I was thinking it could be good to revisit it. I am running into some challenges implementing the Open Specy package because sometimes it is easiest to just implement the functions on the spectra vectors individually. At the core, I think many of the functions already operate on vectors, and we are currently hiding that capability from the user.

Problem

Sometimes it is easiest to run function on intensity vectors rather than Open Specy objects.

Proposed Solution

Can be implemented as default function in s3 for most of the spectral intensity functions. Could also be implemented as .Vector functions in S3.

Alternatives Considered

Disallow vector operations for function consistency, require users to create their own routines.

wincowgerDEV commented 6 months ago

I think this would also make it easier to combine functions with eachother.

wincowgerDEV commented 6 months ago

Another useful thing to add to this update would be too allow ignoring NA values in spectral intensities. I got this function for it:

# Define the function to apply, ignoring NA values
apply_function_ignoring_na <- function(x, fun) {
  # Identify NA positions
  na_positions <- is.na(x)

  # Apply the function only to non-NA elements
  result <- numeric(length(x))
  result[!na_positions] <- fun(x[!na_positions])

  # Reinsert NA values in their original positions
  result[na_positions] <- NA

  return(result)
}

# Example vector with NA values
example_vector <- c(1, NA, 3, NA, 5)

# Example function to apply (e.g., multiply by 2)
example_function <- function(x) {
  x * 2
}

# Apply the function to the vector, ignoring NA values
result_vector <- apply_function_ignoring_na(example_vector, example_function)

# Print the result
print(result_vector)