Example 4: Nest"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

This vignette displays how to use nesting in tidyfst. It has referred to tidyrs vignette in https://tidyr.tidyverse.org/articles/nest.html. Now fist, we nest the "mtcars" data.frame by "cyl" column.

library(tidyfst)

# nest by "cyl" column
mtcars_nested <- mtcars %>% 
  nest_dt(cyl) # you can use "cyl" too, very flexible

# inspect the output data.table
mtcars_nested

Now, we want to do a regression within the nested group "cyl". We'll use the famous lapply to complete this:

mtcars_nested2 <- mtcars_nested %>% 
  mutate_dt(model = lapply(ndt,function(df) lm(mpg ~ wt, data = df)))

mtcars_nested2

We could see that the model is stored in the column "model". Now, we try to get the fitted value in the model.

mtcars_nested3 <- mtcars_nested2 %>% 
  mutate_dt(model_predict = lapply(model, predict))
mtcars_nested3$model_predict

We could find that the "model_predict" is a list of numeric vectors. Let's try to unnest the target column "model_predict".

mtcars_nested3 %>% unnest_dt(model_predict)

This process would remove all the other list column automatically. For instance, in our case, the column "ndt" is removed.



Try the tidyfst package in your browser

Any scripts or data that you put into this service are public.

tidyfst documentation built on July 26, 2023, 5:20 p.m.