### Install tidyft R Package Source: https://github.com/hope-data-science/tidyft/blob/master/README.md Instructions to install the released version of the tidyft package from CRAN. ```R install.packages("tidyft") ``` -------------------------------- ### Install tidyft R Package Source: https://github.com/hope-data-science/tidyft/blob/master/docs/index.html This snippet demonstrates how to install the tidyft package from CRAN using the `install.packages()` function in R. ```R install.packages("tidyft") ``` -------------------------------- ### R tidyft lead/lag Usage Examples Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/lag_lead.html Practical R code examples demonstrating how to use the `lead` and `lag` functions from the `tidyft` package, showcasing default behavior and custom `n` and `fill` arguments. ```R lead(1:5) #> [1] 2 3 4 5 NA lag(1:5) #> [1] NA 1 2 3 4 lead(1:5,2) #> [1] 3 4 5 NA NA lead(1:5,n = 2,fill = 0) #> [1] 3 4 5 0 0 ``` -------------------------------- ### R tidyft slice Examples Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/slice.html Illustrative examples demonstrating how to use `slice`, `slice_head`, and `slice_tail` functions in R with `data.table` to subset rows by position or proportion. ```R a = as.data.table(iris) slice(a,1,2) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1: 5.1 3.5 1.4 0.2 setosa #> 2: 4.9 3.0 1.4 0.2 setosa slice(a,2:3) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1: 4.9 3.0 1.4 0.2 setosa #> 2: 4.7 3.2 1.3 0.2 setosa slice_head(a,5) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1: 5.1 3.5 1.4 0.2 setosa #> 2: 4.9 3.0 1.4 0.2 setosa #> 3: 4.7 3.2 1.3 0.2 setosa #> 4: 4.6 3.1 1.5 0.2 setosa #> 5: 5.0 3.6 1.4 0.2 setosa slice_head(a,0.1) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1: 5.1 3.5 1.4 0.2 setosa #> 2: 4.9 3.0 1.4 0.2 setosa #> 3: 4.7 3.2 1.3 0.2 setosa #> 4: 4.6 3.1 1.5 0.2 setosa #> 5: 5.0 3.6 1.4 0.2 setosa #> 6: 5.4 3.9 1.7 0.4 setosa #> 7: 4.6 3.4 1.4 0.3 setosa #> 8: 5.0 3.4 1.5 0.2 setosa #> 9: 4.4 2.9 1.4 0.2 setosa #> 10: 4.9 3.1 1.5 0.1 setosa #> 11: 5.4 3.7 1.5 0.2 setosa #> 12: 4.8 3.4 1.6 0.2 setosa #> 13: 4.8 3.0 1.4 0.1 setosa #> 14: 4.3 3.0 1.1 0.1 setosa #> 15: 5.8 4.0 1.2 0.2 setosa slice_tail(a,5) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1: 6.7 3.0 5.2 2.3 virginica #> 2: 6.3 2.5 5.0 1.9 virginica #> 3: 6.5 3.0 5.2 2.0 virginica #> 4: 6.2 3.4 5.4 2.3 virginica #> 5: 5.9 3.0 5.1 1.8 virginica slice_tail(a,0.1) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> ``` -------------------------------- ### R Example for `unnest` Function in tidyft Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/nest.html An incomplete example demonstrating the initial step of unnesting a previously nested `data.table` column using the `unnest` function. ```R mtcars %>% nest("cyl|vs") %>% ``` -------------------------------- ### R: Basic Column Relocation with tidyft::relocate() Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/relocate.html Examples demonstrating how to initialize a `data.table` and use `relocate()` to move specific columns to the first or last position. ```R df <- data.table(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a") df ``` ```R df %>% relocate(f) ``` ```R df %>% relocate(a,how = "last") ``` -------------------------------- ### R Examples: Using dummy function for variable creation Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/dummy.html Demonstrates how to use the `dummy` function with different datasets and options, including `longname = FALSE` for shorter column names, and applying it to multiple columns. ```R iris = as.data.table(iris) iris %>% dummy(Species) ``` ```R iris %>% dummy(Species,longname = FALSE) ``` ```R mtcars = as.data.table(mtcars) mtcars %>% head() %>% dummy(vs,am) ``` -------------------------------- ### Handle No Matched Columns in tidyft select_fst Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/fst.html This example demonstrates the behavior of `select_fst` when no columns match the provided pattern. It shows that the function issues a warning and lists the available column names in the `fst_table`, guiding the user to correct patterns. ```R ft %>% select_fst("nothing") ``` -------------------------------- ### Prepare `data.table` for `replace_vars` Examples in R Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/replace_vars.html Initializes a `data.table` named `new_iris` from the `iris` dataset, converting `Species` to character type, to be used in subsequent `replace_vars` examples. ```R iris %>% as.data.table() %>% mutate(Species = as.character(Species))-> new_iris ``` -------------------------------- ### R Examples: Applying Mutating and Filtering Joins with tidyft Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/join.html Illustrates practical usage of `tidyft` join functions in R. This includes examples for `inner_join`, `left_join`, `right_join`, `full_join`, `anti_join`, and `semi_join` using `data.table` objects. It also demonstrates how to suppress join messages and handle joins with differently named columns using the `by` argument. ```R workers = fread(" name company Nick Acme John Ajax Daniela Ajax ") positions = fread(" name position John designer Daniela engineer Cathie manager ") workers %>% inner_join(positions) #> Joining by: name #> Key: #> name company position #> #> 1: Daniela Ajax engineer #> 2: John Ajax designer workers %>% left_join(positions) #> Joining by: name #> Key: #> name company position #> #> 1: Daniela Ajax engineer #> 2: John Ajax designer #> 3: Nick Acme workers %>% right_join(positions) #> Joining by: name #> Key: #> name company position #> #> 1: Cathie manager #> 2: Daniela Ajax engineer #> 3: John Ajax designer workers %>% full_join(positions) #> Joining by: name #> Key: #> name company position #> #> 1: Cathie manager #> 2: Daniela Ajax engineer #> 3: John Ajax designer #> 4: Nick Acme # filtering joins workers %>% anti_join(positions) #> Joining by: name #> name company #> #> 1: Nick Acme workers %>% semi_join(positions) #> Joining by: name #> name company #> #> 1: John Ajax #> 2: Daniela Ajax # To suppress the message, supply 'by' argument workers %>% left_join(positions, by = "name") #> Key: #> name company position #> #> 1: Daniela Ajax engineer #> 2: John Ajax designer #> 3: Nick Acme # Use a named 'by' if the join variables have different names positions2 = setNames(positions, c("worker", "position")) # rename first column in 'positions' workers %>% inner_join(positions2, by = c("name" = "worker")) #> Key: #> name company position #> #> 1: Daniela Ajax engineer #> 2: John Ajax designer # the syntax of 'on' could be a bit different workers %>% inner_join(positions2,on = "name==worker") #> name company position #> #> 1: John Ajax designer ``` -------------------------------- ### Examples of Filtering Data Frames with tidyft::filter in R Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/filter.html Practical R code examples demonstrating how to use `tidyft::filter` to subset `data.table` objects. Examples cover basic filtering, combining multiple conditions, and using functions like `max` within the filter expression. ```R iris = as.data.table(iris) iris %>% filter(Sepal.Length > 7) ``` ```R iris %>% filter(Sepal.Length > 7,Sepal.Width > 3) ``` ```R iris %>% filter(Sepal.Length > 7 & Sepal.Width > 3) ``` ```R iris %>% filter(Sepal.Length == max(Sepal.Length)) ``` -------------------------------- ### Demonstrating fst file parsing and data manipulation in R Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/fst.html Provides practical R examples for writing data to an `fst` file, parsing it with `parse_fst`, and then using `slice_fst`, `select_fst`, `class`, `lapply`, `names`, `dim`, and `summary_fst` to inspect and manipulate the `fst_table` object. ```R # write the file first path = tempfile(fileext = ".fst") fst::write_fst(iris,path) # parse the file but not reading it parse_fst(path) -> ft ft #> #> 150 rows, 5 columns (file333c73e02ce5.fst) #> #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> -- -- -- -- -- -- #> 146 6.7 3.0 5.2 2.3 virginica #> 147 6.3 2.5 5.0 1.9 virginica #> 148 6.5 3.0 5.2 2.0 virginica #> 149 6.2 3.4 5.4 2.3 virginica #> 150 5.9 3.0 5.1 1.8 virginica class(ft) #> [1] "fst_table" lapply(ft,class) #> $Sepal.Length #> [1] "numeric" #> #> $Sepal.Width #> [1] "numeric" #> #> $Petal.Length #> [1] "numeric" #> #> $Petal.Width #> [1] "numeric" #> #> $Species #> [1] "factor" #> names(ft) #> [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species" dim(ft) #> [1] 150 5 summary_fst(ft) #> #> 150 rows, 5 columns (file333c73e02ce5.fst) #> #> * 'Sepal.Length': double #> * 'Sepal.Width' : double #> * 'Petal.Length': double #> * 'Petal.Width' : double #> * 'Species' : factor # get the data by query ft %>% slice_fst(1:3) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1: 5.1 3.5 1.4 0.2 setosa #> 2: 4.9 3.0 1.4 0.2 setosa #> 3: 4.7 3.2 1.3 0.2 setosa ft %>% slice_fst(c(1,3)) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1: 5.1 3.5 1.4 0.2 setosa #> 2: 4.7 3.2 1.3 0.2 setosa ft %>% select_fst(Sepal.Length) #> Sepal.Length #> #> 1: 5.1 #> 2: 4.9 #> 3: 4.7 #> 4: 4.6 #> 5: 5.0 #> --- #> 146: 6.7 #> 147: 6.3 #> 148: 6.5 #> 149: 6.2 #> 150: 5.9 ft %>% select_fst(Sepal.Length,Sepal.Width) #> Sepal.Length Sepal.Width #> #> 1: 5.1 3.5 #> 2: 4.9 3.0 #> 3: 4.7 3.2 #> 4: 4.6 3.1 #> 5: 5.0 3.6 #> ----------------------------- #> 146: 6.7 3.0 #> 147: 6.3 2.5 #> 148: 6.5 3.0 #> 149: 6.2 3.4 #> 150: 5.9 3.0 ft %>% select_fst("Sepal.Length") #> Sepal.Length #> #> 1: 5.1 #> 2: 4.9 #> 3: 4.7 #> 4: 4.6 #> 5: 5.0 ``` -------------------------------- ### Arrange data.table by multiple columns using 'cols' argument in R Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/arrange.html Illustrates sorting a data.table by multiple columns (`Sepal.Width`, `Petal.Length`) using the `cols` argument of `tidyft::arrange`. Each example is self-contained, starting with data initialization. ```R a = as.data.table(iris) a %>% arrange(cols = c("Sepal.Width","Petal.Length")) ``` -------------------------------- ### R Example: Convert Matrix to Tidy Data Frame and Reconstruct Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/tidymat.html Demonstrates the conversion of a named matrix to a tidy data frame using `mat_df` and then reconstructing the matrix from the tidy data frame using `df_mat`. This example verifies the round-trip conversion. ```R mm = matrix(c(1:8,NA),ncol = 3,dimnames = list(letters[1:3],LETTERS[1:3])) mm #> A B C #> a 1 4 7 #> b 2 5 8 #> c 3 6 NA tdf = mat_df(mm) tdf #> row col value #> 1 a A 1 #> 2 b A 2 #> 3 c A 3 #> 4 a B 4 #> 5 b B 5 #> 6 c B 6 #> 7 a C 7 #> 8 b C 8 #> 9 c C NA mat = df_mat(tdf,row,col,value) setequal(mm,mat) #> [1] TRUE ``` -------------------------------- ### R tidyft Data Summarization Examples Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/summarise.html Examples demonstrating how to use `summarise`, `summarise_when`, and `summarise_vars` functions from the `tidyft` package to aggregate data.tables, including basic summarization, conditional summarization, and summarization across multiple columns. ```R a = as.data.table(iris) a %>% summarise(sum = sum(Sepal.Length),avg = mean(Sepal.Length)) #> sum avg #> #> 1: 876.5 5.843333 a %>% summarise_when(Sepal.Length > 5, avg = mean(Sepal.Length), by = Species) #> Species avg #> #> 1: setosa 5.313636 #> 2: versicolor 5.997872 #> 3: virginica 6.622449 a %>% summarise_vars(is.numeric, min, by = Species) #> Species Sepal.Length Sepal.Width Petal.Length Petal.Width #> #> 1: setosa 4.3 2.3 1.0 0.1 #> 2: versicolor 4.9 2.0 3.0 1.0 #> 3: virginica 4.9 2.2 4.5 1.4 ``` -------------------------------- ### R tidyft::complete Function Usage Examples Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/complete.html Demonstrates various ways to use the `complete` function in R with `data.table` objects. Examples show expanding combinations of existing columns, filling missing values with a specified atomic value, and expanding based on new column value ranges. ```R df <- data.table( group = c(1:2, 1), item_id = c(1:2, 2), item_name = c("a", "b", "b"), value1 = 1:3, value2 = 4:6 ) ``` ```R df %>% complete(item_id,item_name) #> Key: #> item_id item_name group value1 value2 #> #> 1: 1 a 1 1 4 #> 2: 1 b NA NA NA #> 3: 2 a NA NA NA #> 4: 2 b 2 2 5 #> 5: 2 b 1 3 6 ``` ```R df %>% complete(item_id,item_name,fill = 0) #> Key: #> item_id item_name group value1 value2 #> #> 1: 1 a 1 1 4 #> 2: 1 b 0 0 0 #> 3: 2 a 0 0 0 #> 4: 2 b 2 2 5 #> 5: 2 b 1 3 6 ``` ```R df %>% complete("item") #> Key: #> item_id item_name group value1 value2 #> #> 1: 1 a 1 1 4 #> 2: 1 b NA NA NA #> 3: 2 a NA NA NA #> 4: 2 b 2 2 5 #> 5: 2 b 1 3 6 ``` ```R df %>% complete(item_id=1:3) #> Key: #> item_id group item_name value1 value2 #> #> 1: 1 1 a 1 4 #> 2: 2 2 b 2 5 #> 3: 2 1 b 3 6 #> 4: 3 NA NA NA ``` ```R df %>% complete(item_id=1:3,group=1:2) #> Key: #> item_id group item_name value1 value2 #> #> 1: 1 1 a 1 4 #> 2: 1 2 NA NA #> 3: 2 1 b 3 6 #> 4: 2 2 b 2 5 #> 5: 3 1 NA NA #> 6: 3 2 NA NA ``` ```R df %>% complete(item_id=1:3,group=1:3,item_name=c("a","b","c")) #> Key: #> item_id group item_name value1 value2 #> #> 1: 1 1 a 1 4 #> 2: 1 1 b NA NA #> 3: 1 1 c NA NA #> 4: 1 2 a NA NA #> 5: 1 2 b NA NA #> 6: 1 2 c NA NA #> 7: 1 3 a NA NA #> 8: 1 3 b NA NA #> 9: 1 3 c NA NA #> 10: 2 1 a NA NA #> 11: 2 1 b 3 6 #> 12: 2 1 c NA NA #> 13: 2 2 a NA NA #> 14: 2 2 b 2 5 #> 15: 2 2 c NA NA #> 16: 2 3 a NA NA #> 17: 2 3 b NA NA ``` -------------------------------- ### Perform Basic Data Selection with tidyft in R Source: https://github.com/hope-data-science/tidyft/blob/master/docs/index.html This example illustrates basic data manipulation using `tidyft` in R. It demonstrates loading the package, converting `iris` to a `data.table`, selecting columns, and highlights that `select` performs in-place modification, altering the original data object. ```R library(tidyft) # get first 5 rows of iris as.data.table(iris)[1:5] -> a #show a #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1: 5.1 3.5 1.4 0.2 setosa #> 2: 4.9 3.0 1.4 0.2 setosa #> 3: 4.7 3.2 1.3 0.2 setosa #> 4: 4.6 3.1 1.5 0.2 setosa #> 5: 5.0 3.6 1.4 0.2 setosa # if you select a %>% select(1:3) #> Sepal.Length Sepal.Width Petal.Length #> 1: 5.1 3.5 1.4 #> 2: 4.9 3.0 1.4 #> 3: 4.7 3.2 1.3 #> 4: 4.6 3.1 1.5 #> 5: 5.0 3.6 1.4 # you lose the unselected columns forever a #> Sepal.Length Sepal.Width Petal.Length #> 1: 5.1 3.5 1.4 #> 2: 4.9 3.0 1.4 #> 3: 4.7 3.2 1.3 #> 4: 4.6 3.1 1.5 #> 5: 5.0 3.6 1.4 ``` -------------------------------- ### Unnesting Nested Data Frames in R Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/nest.html Demonstrates how to use the `unnest()` function to expand a nested data frame. The first example shows a generic unnesting of a pre-nested data frame `ndt`. The second example provides a complete pipeline, starting with the `mtcars` dataset, nesting specific columns (`cyl` and `vs`) into a new column named `ndt`, and then unnesting `ndt` to restore the original structure. ```R unnest(ndt) ``` ```R mtcars %>% nest("cyl|vs") %>% unnest("ndt") ``` -------------------------------- ### R Example: Unite All Columns in a Data Frame Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/unite.html Shows how to unite all columns in a data frame into a single new column by passing `'.'` as the column selection argument. Uses the `iris` dataset as an example to demonstrate combining all its columns. ```R iris %>% as.data.table %>% unite("merged_name",".") ``` -------------------------------- ### Setup Environment for tidyft Performance Benchmarking in R Source: https://github.com/hope-data-science/tidyft/blob/master/docs/articles/Introduction.html This code prepares the R environment for performance comparisons between `tidyft` and other data manipulation packages like `data.table`, `dplyr`, and `dtplyr`. It clears the workspace, loads necessary libraries, creates a large `iris` dataset, converts it to an `fst` table, and removes the original data.frame from RAM to simulate a large-scale scenario. ```R rm(list = ls()) library(data.table) library(dplyr) library(dtplyr) library(tidyft) # make a large data.frame iris[rep(1:nrow(iris),1e4),] -> dt # size: 1500000 rows, 5 columns dim(dt) # save as fst table as_fst(dt) -> ft # remove the data.frame from RAM rm(dt) ``` -------------------------------- ### R Example: Writing and Reading fst Files with tidyft Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/fst_io.html Demonstrates how to use `export_fst` to write an R data frame (iris) to an `fst` file and `import_fst` to read it back into a `data.table` object. The example also includes cleanup by unlinking the created file, ensuring no residual files are left. ```R # \donttest{ export_fst(iris,"iris_fst_test.fst") iris_dt = import_fst("iris_fst_test.fst") iris_dt #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1: 5.1 3.5 1.4 0.2 setosa #> 2: 4.9 3.0 1.4 0.2 setosa #> 3: 4.7 3.2 1.3 0.2 setosa #> 4: 4.6 3.1 1.5 0.2 setosa #> 5: 5.0 3.6 1.4 0.2 setosa #> --- #> 146: 6.7 3.0 5.2 2.3 virginica #> 147: 6.3 2.5 5.0 1.9 virginica #> 148: 6.5 3.0 5.2 2.0 virginica #> 149: 6.2 3.4 5.4 2.3 virginica #> 150: 5.9 3.0 5.1 1.8 virginica unlink("iris_fst_test.fst") # } ``` -------------------------------- ### Example Usage: Estimating Object Size in R Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/object_size.html Demonstrates how to use the `object_size` function with the `iris` dataset to estimate its memory allocation and print it in a human-readable format. ```R iris %>% object_size() #> 7.1 Kb ``` -------------------------------- ### R Example: Grouping and Ungrouping Data with tidyft Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/group.html Demonstrates how to use `group_by` to group a `data.table` by a variable, apply operations per group using `group_exe`, and manage group information with `groups` and `ungroup` functions in R, using the `iris` dataset. ```R a = as.data.table(iris) a #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1: 5.1 3.5 1.4 0.2 setosa #> 2: 4.9 3.0 1.4 0.2 setosa #> 3: 4.7 3.2 1.3 0.2 setosa #> 4: 4.6 3.1 1.5 0.2 setosa #> 5: 5.0 3.6 1.4 0.2 setosa #> --- #> 146: 6.7 3.0 5.2 2.3 virginica #> 147: 6.3 2.5 5.0 1.9 virginica #> 148: 6.5 3.0 5.2 2.0 virginica #> 149: 6.2 3.4 5.4 2.3 virginica #> 150: 5.9 3.0 5.1 1.8 virginica a %>% group_by(Species) %>% group_exe( head(3) ) #> Key: #> Species Sepal.Length Sepal.Width Petal.Length Petal.Width #> #> 1: setosa 5.1 3.5 1.4 0.2 #> 2: setosa 4.9 3.0 1.4 0.2 #> 3: setosa 4.7 3.2 1.3 0.2 #> 4: versicolor 7.0 3.2 4.7 1.4 #> 5: versicolor 6.4 3.2 4.5 1.5 #> 6: versicolor 6.9 3.1 4.9 1.5 #> 7: virginica 6.3 3.3 6.0 2.5 #> 8: virginica 5.8 2.7 5.1 1.9 #> 9: virginica 7.1 3.0 5.9 2.1 groups(a) #> [1] "Species" ungroup(a) groups(a) #> NULL ``` -------------------------------- ### Manage large R datasets with fst and tidyft Source: https://github.com/hope-data-science/tidyft/blob/master/docs/articles/Introduction.html This example illustrates how to efficiently handle large datasets by saving them as fst files using tidyft's as_fst function, thereby reducing RAM usage. It showcases tidyft's powerful integration with fst for out-of-memory data operations. ```R rm(list = ls()) library(tidyft) # make a large data.frame iris[rep(1:nrow(iris),1e4),] -> dt # size: 1500000 rows, 5 columns dim(dt) #> [1] 1500000 5 # save as fst table as_fst(dt) -> ft # remove the data.frame from RAM rm(dt) # inspect the fst table of large iris ft #> #> 1500000 rows, 5 columns (dt50e0459618f8.fst) #> #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> -- -- -- -- -- -- #> 1499996 6.7 3.0 5.2 2.3 virginica #> 1499997 6.3 2.5 5.0 1.9 virginica ``` -------------------------------- ### Convert R data.frame to fst_table with as_fst Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/as_fst.html An R example demonstrating the usage of the `as_fst` function. It shows how to pipe the `iris` dataset into `as_fst` to create an `fst_table` and then displays the structure and content of the resulting `iris_fst` object, including the console output. ```R iris %>% as_fst() -> iris_fst iris_fst #> #> 150 rows, 5 columns (.333c77b27e8.fst) #> #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> -- -- -- -- -- -- #> 146 6.7 3.0 5.2 2.3 virginica #> 147 6.3 2.5 5.0 1.9 virginica #> 148 6.5 3.0 5.2 2.0 virginica #> 149 6.2 3.4 5.4 2.3 virginica #> 150 5.9 3.0 5.1 1.8 virginica ``` -------------------------------- ### R Examples for `nest` Function in tidyft Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/nest.html Demonstrates various ways to use the `nest` function with `data.table` objects, including nesting by single columns, multiple columns, ranges, regex patterns, and direct column lists. Also shows nesting multiple columns into named list-columns using `mcols` for flexible data restructuring. ```R mtcars = as.data.table(mtcars) iris = as.data.table(iris) # examples for nest # nest by which columns? mtcars %>% nest(cyl) mtcars %>% nest("cyl") mtcars %>% nest(cyl,vs) mtcars %>% nest(vs:am) mtcars %>% nest("cyl|vs") mtcars %>% nest(c("cyl","vs")) # nest two columns directly iris %>% nest(mcols = list(petal="^Pe",sepal="^Se")) # nest more flexibly iris %>% nest(mcols = list(ndt1 = 1:3, ndt2 = "Pe", ndt3 = Sepal.Length:Sepal.Width)) ``` -------------------------------- ### R Example: Deleting Rows or Columns with NAs using delete_na Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/drop_delete_na.html Demonstrates how to use the `delete_na` function in R with `data.table` to remove rows or columns based on the proportion or count of NA values. Examples show deleting columns based on different NA thresholds and deleting rows. ```R x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4)) x x %>% delete_na(2,0.75) ``` ```R x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4)) x %>% delete_na(2,0.5) ``` ```R x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4)) x %>% delete_na(2,0.24) ``` ```R x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4)) x %>% delete_na(2,2) ``` ```R x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4)) x %>% delete_na(1,0.6) ``` ```R x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4)) x %>% delete_na(1,2) ``` -------------------------------- ### R: Relocating Columns by Type or Regex Pattern with tidyft::relocate() Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/relocate.html Examples showing how to use `relocate()` with column selection helpers like `is.character`, `is.numeric`, or regular expressions to reorder columns based on their properties. ```R df %>% relocate(is.character) ``` ```R df %>% relocate(is.numeric, how = "last") ``` ```R df %>% relocate("[aeiou]") ``` -------------------------------- ### Perform Basic Data Selection with tidyft in R Source: https://github.com/hope-data-science/tidyft/blob/master/README.md This example demonstrates how to load tidyft, convert an R data frame (iris) to a data.table, and perform column selection using tidyft's `select` function. It highlights the in-place modification behavior of data.table/tidyft, where selecting columns directly modifies the original data.table, causing unselected columns to be lost. ```R library(tidyft) # get first 5 rows of iris as.data.table(iris)[1:5] -> a #show a #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1: 5.1 3.5 1.4 0.2 setosa #> 2: 4.9 3.0 1.4 0.2 setosa #> 3: 4.7 3.2 1.3 0.2 setosa #> 4: 4.6 3.1 1.5 0.2 setosa #> 5: 5.0 3.6 1.4 0.2 setosa # if you select a %>% select(1:3) #> Sepal.Length Sepal.Width Petal.Length #> 1: 5.1 3.5 1.4 #> 2: 4.9 3.0 1.4 #> 3: 4.7 3.2 1.3 #> 4: 4.6 3.1 1.5 #> 5: 5.0 3.6 1.4 # you lose the unselected columns forever a #> Sepal.Length Sepal.Width Petal.Length #> 1: 5.1 3.5 1.4 #> 2: 4.9 3.0 1.4 #> 3: 4.7 3.2 1.3 #> 4: 4.6 3.1 1.5 #> 5: 5.0 3.6 1.4 ``` -------------------------------- ### Select Columns by Vector of Names with tidyft Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/fst.html This example demonstrates selecting columns by providing an explicit vector of column names to the `select_fst` function. This is useful when you have a predefined list of column names to select from the `fst_table`. ```R ft %>% select_fst(cols = names(iris)[2:3]) ``` -------------------------------- ### Example Output: Dummy Variable for 'am' Column in R Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/dummy.html This snippet shows the console output of a `dummy` function call, likely from the `tidyft` package, applied to the 'am' (transmission) column of a dataset. It illustrates how a categorical variable is transformed into a binary dummy variable (e.g., 'am_1'). ```R #> 2: 18.7 8 360 175 3.15 3.440 17.02 3 2 1 0 1 #> 3: 21.0 6 160 110 3.90 2.620 16.46 4 4 1 0 0 #> 4: 21.0 6 160 110 3.90 2.875 17.02 4 4 1 0 0 #> 5: 21.4 6 258 110 3.08 3.215 19.44 3 1 0 1 1 #> 6: 22.8 4 108 93 3.85 2.320 18.61 4 1 0 1 0 #> am_1 #> #> 1: 0 #> 2: 0 #> 3: 1 #> 4: 1 #> 5: 0 #> 6: 1 ``` -------------------------------- ### R Example: Convert Tidy Data Frame to Matrix with Piped Renaming Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/tidymat.html Illustrates how to convert a tidy data frame to a named matrix using `df_mat` in a piped operation, demonstrating the use of `setNames` to adjust column names before conversion. ```R tdf %>% setNames(c("A","B","C")) %>% df_mat(A,B,C) #> A B C #> a 1 4 7 #> b 2 5 8 #> c 3 6 NA ``` -------------------------------- ### R Example: Unite Columns and Remove Originals Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/unite.html Illustrates how to use the `unite` function to combine columns and simultaneously remove the original input columns from the data frame using `remove = TRUE`. ```R df %>% unite("xy", x:y,remove = TRUE) ``` -------------------------------- ### R Example: Extracting nth value from a vector Source: https://github.com/hope-data-science/tidyft/blob/master/docs/reference/nth.html Demonstrates how to use the `nth` function in R to retrieve elements from a vector using positive and negative indices. ```R x = 1:10 nth(x, 1) nth(x, 5) nth(x, -2) ```