Using vector elements to select observations from and then subset a tibble.

Everyone,

I need a programmatic method to first select observations from a tibble and then interactively to subset that tibble into multiple "child" tibbles.

I haven't been able to find a method that is completely programmatic with NO human interaction during the selecting and sub-setting steps.

Does anyone have any suggestions, please?

Thanks in advance for sharing your knowledge and experience.

Linda

# I want to use a programmatic method to use each element of a vector (termination_id_vector) 
 # to select observations from a tibble (master_tibble)
 # and then iteratively to subset the tibble into multiple child tibbles (child_tibbleX56666, etc).

# i = 1 to 4
termination_id_vector <- c("X56666", "X98453", "X09916", "X11446")

# The table to be subsetted iteratively into i separate tables
master_tibble <- tibble::tribble(
  ~ID,       ~status,
  "X56666", "ORIGINATION",
  "X56666", "CONTINUATION",
  "X56666", "TERMINATION",
  "X98453", "ORIGINATION",
  "X98453", "TERMINATION",
  "X09916", "ORIGINATION",
  "X09916", "TERMINATION",
  "X11446", "ORIGINATION",
  "X11446", "TERMINATION",
  "X11111", "ORIGINATION",
  "X11111", "CONTINUATION"
  )

# the four output tables (number = i) that I need to obtain programmatically:
# i=1
child_tibbleX56666 <- tibble::tribble(
  ~ID,       ~status,
  "X56666", "ORIGINATION",
  "X56666", "CONTINUATION",
  "X56666", "TERMINATION"
)

head(child_tibbleX56666)

# i=2
child_tibbleX98453 <- tibble::tribble(
  ~ID,       ~status,
  "X98453", "ORIGINATION",
  "X98453", "TERMINATION"
)

head(child_tibbleX98453)

# i=3
child_tibbleX09916 <- tibble::tribble(
  ~ID,       ~status,
  "X09916", "ORIGINATION",
  "X09916", "TERMINATION"
)

head(child_tibbleX09916)

# i=4
child_tibbleX11446 <- tibble::tribble(
  ~ID,       ~status,
  "X11446", "ORIGINATION",
  "X11446", "TERMINATION"
)

head(child_tibbleX11446)

Please define human interaction.

Does this help?

termination_id_vector <- c("X56666", "X98453", "X09916", "X11446")

master_tibble <- tibble::tribble(
  ~ID,       ~status,
  "X56666", "ORIGINATION",
  "X56666", "CONTINUATION",
  "X56666", "TERMINATION",
  "X98453", "ORIGINATION",
  "X98453", "TERMINATION",
  "X09916", "ORIGINATION",
  "X09916", "TERMINATION",
  "X11446", "ORIGINATION",
  "X11446", "TERMINATION",
  "X11111", "ORIGINATION",
  "X11111", "CONTINUATION"
)

child_names <- paste0("child_table", termination_id_vector)
child_values <- purrr::map(termination_id_vector, ~ dplyr::filter(master_tibble, ID == .x))

purrr::walk2(child_names, child_values, ~ assign(.x, .y, envir = .GlobalEnv))

Created on 2019-11-29 by the reprex package (v0.3.0)

3 Likes

:smiley: @Yarnabrina, "human interaction" means hard coding the different ID numbers into one or more filter() commands to create the multiple tables. I need to do that programmatically. No human fingers allowed.

Thanks for this suggestion. I'll give it a try.

Linda

@Yarnabrina, thanks for the excellent suggestion. It accomplished exactly what I wanted and I learned something new from your demonstration. Thanks for both.

Linda

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.