Search code examples
rsplitrows

separate one row into multiple based on value in row


I am take a data frame similar to this:

GISBLong GISBLat B Flyway B Month Count of Birds
-85.41667 42.41667 3 6 3
-97.25000 36.0833 2 7 2

And change it into this:

GISBLong GISBLat B Flyway B Month Bird ID
-85.41667 42.41667 3 6 1.1
-85.41667 42.41667 3 6 1.2
-85.41667 42.41667 3 6 1.3
-97.25000 36.0833 2 7 2.1
-97.25000 36.0833 2 7 2.2

So split the rows by the number in the "Count of Birds". Currently 1 row represents multiple birds and I would like to have 1 row for each bird. Any thoughts or suggestions on a function to use? Thanks so much.


Solution

  • You could use dplyr and tidyr. Let df be your data.frame.

    library(dplyr)
    library(tidyr)
    
    df %>%
      mutate(id=row_number()) %>%
      uncount(Count_of_Birds) %>%
      group_by(id) %>%
      mutate(Bird_ID = paste0(id, ".", row_number())) %>%
      ungroup() %>%
      select(-id)
    

    returns

    # A tibble: 5 x 5
      GISBLong GISBLat B_Flyway B_Month Bird_ID
         <dbl>   <dbl>    <dbl>   <dbl> <chr>  
    1    -85.4    42.4        3       6 1.1    
    2    -85.4    42.4        3       6 1.2    
    3    -85.4    42.4        3       6 1.3    
    4    -97.2    36.1        2       7 2.1    
    5    -97.2    36.1        2       7 2.2  
    

    Data

    df <- readr::read_table2("GISBLong  GISBLat     B_Flyway    B_Month     Count_of_Birds
                             -85.41667  42.41667    3   6   3
                             -97.25000  36.0833     2   7   2")