How to expand rows by a sequence created from one column

Hi! no reprex yet cause i don't have any code.. :slight_smile: just seeking help in understand witch function i should use...
I have this simple DB with 3 columns: Tree Species, Diameter minimum and Diameter max.

I want to create a dataset (usually i use expand.grid) that include 100 numbers (for specie) between Dmin and Dmax (equally spaced wuold be nice) in a new column Dpoints (considering that every species have his own Dmin and Dmax)
i think i need a nested approach?
Thanks for your time!
Michel

Here is one method. I only put in 4 points so the result can be displayed.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
DF <- data.frame(Tree=c("A","B"),
                 Dmin=c(1,25),
                 Dmax=c(10,60))
DF
#>   Tree Dmin Dmax
#> 1    A    1   10
#> 2    B   25   60
DF2 <- DF |> group_by(Tree) |> 
  summarize(Dpoints=seq(Dmin,Dmax,length.out=4))
#> `summarise()` has grouped output by 'Tree'. You can override using the `.groups`
#> argument.
DF2
#> # A tibble: 8 x 2
#> # Groups:   Tree [2]
#>   Tree  Dpoints
#>   <chr>   <dbl>
#> 1 A         1  
#> 2 A         4  
#> 3 A         7  
#> 4 A        10  
#> 5 B        25  
#> 6 B        36.7
#> 7 B        48.3
#> 8 B        60
#If you want to append DF2 to the original data
DF <- inner_join(DF,DF2,by="Tree")

Created on 2022-06-29 by the reprex package (v2.0.1)

Looks like a good use case for tidyr functions complete() and fill(). Here's a post from SO with a similar problem as yours:

Thank you FJCC, most helpful!!!

Interesting!! thanks... hard but helpful reading!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.