Home > Software engineering >  Create a dataframe from two vectors of unequal length in R
Create a dataframe from two vectors of unequal length in R

Time:12-17

I have two vectors of unequal length. Let's call them group1 and group2.

group1 <- c(1,2,3)
group2 <- c(6,7,8,9,10)

I want to create a dataframe like:

key value
group1 1
group2 6
group2 7
group1 2

...So on

I know how to do it if the length of the vectors is the same: just create a tibble and then gather it. But what to do in this case?

CodePudding user response:

You may try

group1 <- 1:3
group2 <- 6:10
n <- length(group2) - length(group1)
group1 <- c(group1, rep(NA, n))

tibble(group1,group2) %>%
  rownames_to_column("idx") %>%
  gather(group1,group2, key = 'key', value = 'value') %>%
  mutate(idx = as.numeric(idx)) %>%
  mutate(idx = ifelse(key == 'group1', floor(idx/2), floor((idx-1)/2))) %>%
  arrange(idx, key) %>%
  na.omit %>% select(-idx)


  key    value
  <chr>  <int>
1 group1     1
2 group2     6
3 group2     7
4 group1     2
5 group1     3
6 group2     8
7 group2     9
8 group2    10

CodePudding user response:

I'm assuming that there was no significance to the row order in your question. Here is one general way to do it.

vals1 = c(1, 2, 3)
vals2 = c(6, 7, 8, 9, 10)

list(group1 = vals1, group2 = vals2) %>%
  imap(~ tibble(key = .y, value = .x)) %>%
  reduce(bind_rows)

# A tibble: 8 x 2
  key    value
  <chr>  <dbl>
1 group1     1
2 group1     2
3 group1     3
4 group2     6
5 group2     7
6 group2     8
7 group2     9
8 group2    10

The strategy was to create a separate table for each vector and then bind their rows. I created a named list so that I could pass the names into imap() to generate the key column. After creating each separate tibble, I reduced them with bind_rows().

  • Related