You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.7KB

  1. #' Insert a vector y inside another vector x at position
  2. #'
  3. #' The vectors in the y list will be inserted
  4. #' at positions respectively *after* the x[position] element of x
  5. #'
  6. #' @param x A vector to be inserted into
  7. #' @param y A vector or list of vectors to insert into x
  8. #' @param position The position / vector of positions to insert vector(s) y in vector x
  9. #' @return The combined vector
  10. #' @keywords internal
  11. insert <- function(x, y, position) {
  12. # y is supposed to be a list of vectors. If it is a single vector, make it a simple list containing that vector
  13. if (!is.list(y)) y <- list(y)
  14. # Stop if there is not as many positions as vectors to insert
  15. stopifnot(length(y) == length(position))
  16. # Create an empty return vector that will contain the partition of x and the inserts
  17. result <- vector("list", 2 * length(position) + 1)
  18. # Split x in groups between the insert positions
  19. old <- split(x, cumsum(seq_along(x) %in% (position + 1)))
  20. # Insert the x splits at odd positions in result
  21. result[seq(from = 1, by = 2, length.out = length(old))] <- old
  22. # Insert the y inserts at even positions in results
  23. result[c(F, T)] <- y
  24. # Return a simple vector
  25. unlist(result)
  26. }
  27. #' Is the object possibly a desctable?
  28. #'
  29. #' Check if the object is produced by desc_table.
  30. #' Return a string:
  31. #' - simple
  32. #' - grouped
  33. #' or FALSE if not a desctable
  34. #'
  35. #' @param desctable A potential desctable to check
  36. #' @return The type of desctable or FALSE
  37. #' @keywords internal
  38. which.desctable <- function(desctable)
  39. {
  40. attributes <- list()
  41. if (all(c("data", ".stats", ".vars") %in% names(desctable)))
  42. "grouped"
  43. else if (is.data.frame(desctable) & ("Variables" %in% names(desctable)))
  44. "simple"
  45. else
  46. ""
  47. }