-1

I have tibble as follows:

    my_tibble <- tibble(`A` = c(1,2),
                    `B` = c(2,1),
                    `C` = c(2,2),
                    `D` = c(1,3),
                    `E` = c(2,3),
                    `F` = c(5,2)) %>% 
  mutate(`LIST 1` = rowSums(.[1:4]),
         `LIST 2` = rowSums(.[5:6])) %>% 
  select(`LIST 1`,A:D,`LIST 2`,E:F)

Variables form A to D belong to LIST1 and accordingly - E,F to LIST2

Is there any method to split this df automatically into two separate dfs and get:

    list1df <- tibble(`A` = c(1,2),
                `B` = c(2,1),
                `C` = c(2,2),
                `D` = c(1,3)) %>% 
  mutate(`LIST 1` = rowSums(.[1:4])) %>% 
  select(`LIST 1`,A:D)

list2df <- tibble(`E` = c(2,3),
                  `F` = c(5,2)) %>% 
  mutate(`LIST 2` = rowSums(.[1,2])) %>% 
  select(`LIST 2`,E:F)

Any help or hint is very appreciated

szaki
  • 3
  • 1

1 Answers1

0
Map(
  function(x){
    my_tibble[, x, drop = FALSE]
  },
  list(
    LETTERS[seq_len(which(LETTERS == "D"))], 
    LETTERS[which(LETTERS == "E"):which(LETTERS == "F")]
  )
)
hello_friend
  • 5,682
  • 1
  • 11
  • 15