DJH
DJH

Reputation: 13

Create a list column with ranges set by existing columns

I am trying to create a list column within a data frame, specifying the range using existing columns, something like:

# A tibble: 3 x 3
      A     B C         
  <dbl> <dbl> <list>    
1     1     6 c(1, 2, 3, 4, 5, 6)
2     2     5 c(2, 3, 4, 5)
3     3     4 c(3, 4)

The catch is that it would need to be created as follows:

df %>% mutate(C = c(A:B))

I have a dataset containing integers entered as ranges, i.e someone has entered "7 to 26". I've separated the ranges into two columns A & B, or "start" and "end", and was hoping to use c(A:B) to create a list, but using dplyr I keep getting:

Warning messages:
1: In a:b : numerical expression has 3 elements: only the first used
2: In a:b : numerical expression has 3 elements: only the first used 

Which gives:

# A tibble: 3 x 3
      A     B C         
  <dbl> <dbl> <list>    
1     1     6 list(1:6)
2     2     5 list(1:6)
3     3     4 list(1:6)

Has anyone had a similar issue and found a workaround?

Upvotes: 1

Views: 348

Answers (1)

Darren Tsai
Darren Tsai

Reputation: 35584

You can use map2() in purrr

library(dplyr)

df %>%
  mutate(C = purrr::map2(A, B, seq))

or do rowwise() before mutate()

df %>%
  rowwise() %>% 
  mutate(C = list(A:B)) %>%
  ungroup()

Both methods give

# # A tibble: 3 x 3
#       A     B C        
#   <int> <int> <list>   
# 1     1     6 <int [6]>
# 2     2     5 <int [4]>
# 3     3     4 <int [2]>

Data

df <- tibble::tibble(A = 1:3, B = 6:4)

Upvotes: 2

Related Questions