From 93b5e3a93355230dbb681b00a7caf0e1348c0fb5 Mon Sep 17 00:00:00 2001 From: Hope <32947502+hope-data-science@users.noreply.github.com> Date: Mon, 23 Sep 2024 12:58:49 +0800 Subject: [PATCH] Add files via upload --- doc/Introduction.R | 141 + doc/Introduction.Rmd | 202 + doc/Introduction.html | 4834 +++++++++++++++++ docs/404.html | 275 +- docs/LICENSE-text.html | 124 +- docs/LICENSE.html | 124 +- docs/articles/Introduction.html | 808 +-- .../htmlwidgets-1.6.1/htmlwidgets.js | 901 +++ .../profvis-0.3.6.9000/profvis.css | 696 +++ .../profvis-0.3.6.9000/profvis.js | 2656 +++++++++ .../profvis-0.3.6.9000/scroll.js | 69 + .../profvis-binding-0.3.7/profvis.js | 23 + docs/articles/index.html | 127 +- docs/authors.html | 158 +- docs/bootstrap-toc.css | 60 + docs/bootstrap-toc.js | 159 + docs/index.html | 420 +- docs/pkgdown.css | 208 +- docs/pkgdown.js | 9 +- docs/pkgdown.yml | 6 +- docs/reference/Rplot001.png | Bin 0 -> 1011 bytes docs/reference/arrange.html | 321 +- docs/reference/as_fst.html | 207 +- docs/reference/complete.html | 359 +- docs/reference/count.html | 343 +- docs/reference/cummean.html | 169 +- docs/reference/distinct.html | 227 +- docs/reference/drop_delete_na.html | 285 +- docs/reference/dummy.html | 387 +- docs/reference/fill.html | 297 +- docs/reference/filter.html | 257 +- docs/reference/fst.html | 818 ++- docs/reference/fst_io.html | 305 +- docs/reference/group.html | 261 +- docs/reference/index.html | 288 +- docs/reference/join.html | 355 +- docs/reference/lag_lead.html | 207 +- docs/reference/long_wide.html | 505 +- docs/reference/mutate.html | 789 ++- docs/reference/nest.html | 729 ++- docs/reference/nth.html | 189 +- docs/reference/object_size.html | 171 +- docs/reference/pull.html | 183 +- docs/reference/read_csv.html | 178 +- docs/reference/reexports.html | 147 +- docs/reference/relocate.html | 293 +- docs/reference/replace_vars.html | 351 +- docs/reference/rowwise.html | 243 +- docs/reference/select.html | 433 +- docs/reference/separate.html | 246 +- docs/reference/slice.html | 519 +- docs/reference/summarise.html | 259 +- docs/reference/sys_time_print.html | 217 +- docs/reference/tidymat.html | 257 +- docs/reference/uncount.html | 219 +- docs/reference/unite.html | 345 +- docs/reference/utf8_encoding.html | 205 +- docs/sitemap.xml | 46 + 58 files changed, 15348 insertions(+), 8262 deletions(-) create mode 100644 doc/Introduction.R create mode 100644 doc/Introduction.Rmd create mode 100644 doc/Introduction.html create mode 100644 docs/articles/Introduction_files/htmlwidgets-1.6.1/htmlwidgets.js create mode 100644 docs/articles/Introduction_files/profvis-0.3.6.9000/profvis.css create mode 100644 docs/articles/Introduction_files/profvis-0.3.6.9000/profvis.js create mode 100644 docs/articles/Introduction_files/profvis-0.3.6.9000/scroll.js create mode 100644 docs/articles/Introduction_files/profvis-binding-0.3.7/profvis.js create mode 100644 docs/bootstrap-toc.css create mode 100644 docs/bootstrap-toc.js create mode 100644 docs/reference/Rplot001.png create mode 100644 docs/sitemap.xml diff --git a/doc/Introduction.R b/doc/Introduction.R new file mode 100644 index 0000000..53a9e4a --- /dev/null +++ b/doc/Introduction.R @@ -0,0 +1,141 @@ +## ---- include = FALSE--------------------------------------------------------- +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) + +## ----setup-------------------------------------------------------------------- +library(tidyft) + +# make copies +copy(iris) -> a +copy(mtcars) -> b + +# before +class(a) +class(b) + +# convert codes +lapply(ls(),get) %>% + lapply(setDT) %>% + invisible() + +# after +class(a) +class(b) + +## ----------------------------------------------------------------------------- +rm(list = ls()) + +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) + +# inspect the fst table of large iris +ft +summary_fst(ft) + +# list the variables in the environment +ls() # only the ft exists + +## ----------------------------------------------------------------------------- +ft %>% + slice_fst(5555:6666) # get 5555 to 6666 row + +## ----------------------------------------------------------------------------- + +sys_time_print({ + res = ft %>% + select_fst(Species,Sepal.Length,Sepal.Width) %>% + rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% + arrange(group,sl) %>% + filter(sl > 5) %>% + distinct(sl,.keep_all = TRUE) %>% + summarise(sw = max(sw),by = group) +}) + +res + + +## ----------------------------------------------------------------------------- + +rm(list = ls()) + +library(profvis) +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) + + +profvis({ + + res1 = ft %>% + select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>% + dplyr::select(-Petal.Length) %>% + dplyr::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% + dplyr::arrange(group,sl) %>% + dplyr::filter(sl > 5) %>% + dplyr::distinct(sl,.keep_all = TRUE) %>% + dplyr::group_by(group) %>% + dplyr::summarise(sw = max(sw)) + + res2 = ft %>% + select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>% + lazy_dt() %>% + dplyr::as_tibble() %>% + dplyr::select(-Petal.Length) %>% + dplyr::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% + dplyr::arrange(group,sl) %>% + dplyr::filter(sl > 5) %>% + dplyr::distinct(sl,.keep_all = TRUE) %>% + dplyr::group_by(group) %>% + dplyr::summarise(sw = max(sw)) %>% + as.data.table() + + res3 = ft[,c("Species","Sepal.Length","Sepal.Width","Petal.Length")] %>% + setDT() %>% + .[,.SD,.SDcols = -"Petal.Length"] %>% + setnames(old =c("Species","Sepal.Length","Sepal.Width"), + new = c("group","sl","sw")) %>% + setorder(group,sl) %>% + .[sl>5] %>% unique(by = "sl") %>% + .[,.(sw = max(sw)),by = group] + + + res4 = ft %>% + tidyft::select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>% + tidyft::select(-Petal.Length) %>% + tidyft::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% + tidyft::arrange(group,sl) %>% + tidyft::filter(sl > 5) %>% + tidyft::distinct(sl,.keep_all = TRUE) %>% + tidyft::summarise(sw = max(sw),by = group) + + +}) + +setequal(res1,res2) +setequal(res2,res3) +setequal(res3,res4) + + +## ----------------------------------------------------------------------------- +sessionInfo() + diff --git a/doc/Introduction.Rmd b/doc/Introduction.Rmd new file mode 100644 index 0000000..dc9ff94 --- /dev/null +++ b/doc/Introduction.Rmd @@ -0,0 +1,202 @@ +--- +title: "Fastest data operations with least memory in tidy syntax" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Introduction} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +``` + +## Why tidyft? + +Before [tidyft](https://github.com/hope-data-science/tidyft), I've designed a package named [tidyfst](https://github.com/hope-data-science/tidyfst). Backed by *data.table*, it is fast and convenient. By then, I was not so interested in modification by reference, which always causes trouble in my workflow. Therefore, I use a lot of functions to make copies so as to suppress the in place replacement. However, when it comes to big data, simply making a new copy of the original data set could be time consuming and memory inefficient. So I tried to write some functions using the feature of modification by reference. This ends up in inconsistency of many functions in the *tidyfst* package. In the end, I removed all the in place replacement functions in *tidyfst* and build a new package instead. This is how *tidyft* comes into being. + +## The philosophy of tidyft + +> You cannot step into the same river twice, for other waters are continually flowing on.
+>
+> [—— Heraclitus]{style="float:right"} +>
+ +If you try to do data operations on any data.table(s), never use it again for futher analysis, because it is not the data you know before. And you might never figure out what have happened and what has been changed in that process. If you really want to use it again, try make a copy first using `copy()`, which might take extra time and space (that's why tidyft avoid doing this all the time).

+ +Another rule is, tidyft only deals with data.table(s), the raw data.frame and other formats such as tibble could not work. If you already have lots of data.frames in the environment, try these codes. + +```{r setup} +library(tidyft) + +# make copies +copy(iris) -> a +copy(mtcars) -> b + +# before +class(a) +class(b) + +# convert codes +lapply(ls(),get) %>% + lapply(setDT) %>% + invisible() + +# after +class(a) +class(b) +``` + +One last thing, while modifications are carried out in place, doesn't mean that the results could not be showed after operation. The data.table package would return it invisibly, but in tidyft, the final results are always printed if possible. This brings no reduction to the computation performance. + +## Working with fst + +tidyft would not be so powerful without [fst](https://github.com/fstpackage/fst). I first introduce this workflow into [tidyfst](https://hope-data-science.github.io/tidyfst/articles/example5_fst.html). In such workflow, you do not have to read all data into memory, only import the needed data when necessary. tidyft is not so convenient for in-memory operations, but it works very well (if not best) with the fst workflow. Here we'll make some examples. + +```{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) +# save as fst table +as_fst(dt) -> ft +# remove the data.frame from RAM +rm(dt) + +# inspect the fst table of large iris +ft +summary_fst(ft) + +# list the variables in the environment +ls() # only the ft exists +``` + +The `as_fst` could save any data.frame as ".fst" file in temporary file and parse it back as fst table. Fst table is small in RAM, but if you want to get any part of the data.frame, you can get it in almost no time: + +```{r} +ft %>% + slice_fst(5555:6666) # get 5555 to 6666 row +``` + + + +Except for `slice_fst`, there are also other functions for subsetting the data, such as `select_fst`,`filter_fst`. Good practice is: Make subsets of the data and use the least needy data to do operations. For very large data sets, you may try to do tests on a sample of the data (using `slice` or `select` to get several rows or columns) first before you implement a huge operation. Now let's do a slightly complex manipulation. We'll use `sys_time_print` to measure the running time. + +```{r} + +sys_time_print({ + res = ft %>% + select_fst(Species,Sepal.Length,Sepal.Width) %>% + rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% + arrange(group,sl) %>% + filter(sl > 5) %>% + distinct(sl,.keep_all = TRUE) %>% + summarise(sw = max(sw),by = group) +}) + +res + +``` + +This should be pretty fast. Becasue when we use the data in fst table, we never get them until using the "_fst" suffix functions, so the tidyft functions never modify the data in the fst file or fst table. That is to say, we do not have to worry about the modification by reference any more. No copies made, fastest ever. + +## Performance +The fst workflow could also be working with other tools, though less efficient. Now let's compare the performance of tidyft, data.table, dtplyr and dplyr. + +```{r} + +rm(list = ls()) + +library(profvis) +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) + + +profvis({ + + res1 = ft %>% + select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>% + dplyr::select(-Petal.Length) %>% + dplyr::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% + dplyr::arrange(group,sl) %>% + dplyr::filter(sl > 5) %>% + dplyr::distinct(sl,.keep_all = TRUE) %>% + dplyr::group_by(group) %>% + dplyr::summarise(sw = max(sw)) + + res2 = ft %>% + select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>% + lazy_dt() %>% + dplyr::as_tibble() %>% + dplyr::select(-Petal.Length) %>% + dplyr::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% + dplyr::arrange(group,sl) %>% + dplyr::filter(sl > 5) %>% + dplyr::distinct(sl,.keep_all = TRUE) %>% + dplyr::group_by(group) %>% + dplyr::summarise(sw = max(sw)) %>% + as.data.table() + + res3 = ft[,c("Species","Sepal.Length","Sepal.Width","Petal.Length")] %>% + setDT() %>% + .[,.SD,.SDcols = -"Petal.Length"] %>% + setnames(old =c("Species","Sepal.Length","Sepal.Width"), + new = c("group","sl","sw")) %>% + setorder(group,sl) %>% + .[sl>5] %>% unique(by = "sl") %>% + .[,.(sw = max(sw)),by = group] + + + res4 = ft %>% + tidyft::select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>% + tidyft::select(-Petal.Length) %>% + tidyft::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% + tidyft::arrange(group,sl) %>% + tidyft::filter(sl > 5) %>% + tidyft::distinct(sl,.keep_all = TRUE) %>% + tidyft::summarise(sw = max(sw),by = group) + + +}) + +setequal(res1,res2) +setequal(res2,res3) +setequal(res3,res4) + +``` + +Because tidyft is based on data.table, therefore, if you always use data.table correctly, then tidyft should not perform better than data.table (I do use some tricks, by never do column selection but delete the unselected ones instead, which is faster and more memory efficient than using `.SDcols` in data.table). However, tidyft has a very different syntax, which might be more readable. And lots of complex operations of data.table has been wrapped in it. This could save your day to write the correct codes sometimes. I hope all my time devoted to this work could possibly save some of your valuable time on data operations of big datasets. + + +## Session Information +```{r} +sessionInfo() +``` + + + + + + + + + + diff --git a/doc/Introduction.html b/doc/Introduction.html new file mode 100644 index 0000000..e3000c5 --- /dev/null +++ b/doc/Introduction.html @@ -0,0 +1,4834 @@ + + + + + + + + + + + + + + +Fastest data operations with least memory in tidy syntax + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Fastest data operations with least memory in tidy syntax

+ + + +
+

Why tidyft?

+

Before tidyft, I’ve designed a package named tidyfst. Backed by data.table, it is fast and convenient. By then, I was not so interested in modification by reference, which always causes trouble in my workflow. Therefore, I use a lot of functions to make copies so as to suppress the in place replacement. However, when it comes to big data, simply making a new copy of the original data set could be time consuming and memory inefficient. So I tried to write some functions using the feature of modification by reference. This ends up in inconsistency of many functions in the tidyfst package. In the end, I removed all the in place replacement functions in tidyfst and build a new package instead. This is how tidyft comes into being.

+
+
+

The philosophy of tidyft

+
+

You cannot step into the same river twice, for other waters are continually flowing on.

—— Heraclitus

+
+

If you try to do data operations on any data.table(s), never use it again for futher analysis, because it is not the data you know before. And you might never figure out what have happened and what has been changed in that process. If you really want to use it again, try make a copy first using copy(), which might take extra time and space (that’s why tidyft avoid doing this all the time).

+

Another rule is, tidyft only deals with data.table(s), the raw data.frame and other formats such as tibble could not work. If you already have lots of data.frames in the environment, try these codes.

+
library(tidyft)
+#> 
+#> Life's short, use R.
+#> 
+#> Attaching package: 'tidyft'
+#> The following objects are masked from 'package:stats':
+#> 
+#>     filter, lag
+
+# make copies
+copy(iris) -> a
+copy(mtcars) -> b
+
+# before
+class(a)
+#> [1] "data.frame"
+class(b)
+#> [1] "data.frame"
+
+# convert codes
+lapply(ls(),get) %>%
+  lapply(setDT) %>% 
+  invisible()
+
+# after
+class(a)
+#> [1] "data.table" "data.frame"
+class(b)
+#> [1] "data.table" "data.frame"
+

One last thing, while modifications are carried out in place, doesn’t mean that the results could not be showed after operation. The data.table package would return it invisibly, but in tidyft, the final results are always printed if possible. This brings no reduction to the computation performance.

+
+
+

Working with fst

+

tidyft would not be so powerful without fst. I first introduce this workflow into tidyfst. In such workflow, you do not have to read all data into memory, only import the needed data when necessary. tidyft is not so convenient for in-memory operations, but it works very well (if not best) with the fst workflow. Here we’ll make some examples.

+
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
+#> <fst file>
+#> 1500000 rows, 5 columns (dt636472463ef3.fst)
+#> 
+#>         Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>             <double>    <double>     <double>    <double>  <factor>
+#> 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
+#> 1499998          6.5         3.0          5.2         2.0 virginica
+#> 1499999          6.2         3.4          5.4         2.3 virginica
+#> 1500000          5.9         3.0          5.1         1.8 virginica
+summary_fst(ft)
+#> <fst file>
+#> 1500000 rows, 5 columns (dt636472463ef3.fst)
+#> 
+#> * 'Sepal.Length': double
+#> * 'Sepal.Width' : double
+#> * 'Petal.Length': double
+#> * 'Petal.Width' : double
+#> * 'Species'     : factor
+
+# list the variables in the environment
+ls() # only the ft exists
+#> [1] "ft"
+

The as_fst could save any data.frame as “.fst” file in temporary file and parse it back as fst table. Fst table is small in RAM, but if you want to get any part of the data.frame, you can get it in almost no time:

+
ft %>% 
+  slice_fst(5555:6666)  # get 5555 to 6666 row
+#>       Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
+#>              <num>       <num>        <num>       <num>     <fctr>
+#>    1:          5.0         3.6          1.4         0.2     setosa
+#>    2:          5.4         3.9          1.7         0.4     setosa
+#>    3:          4.6         3.4          1.4         0.3     setosa
+#>    4:          5.0         3.4          1.5         0.2     setosa
+#>    5:          4.4         2.9          1.4         0.2     setosa
+#>   ---                                                             
+#> 1108:          5.9         3.0          4.2         1.5 versicolor
+#> 1109:          6.0         2.2          4.0         1.0 versicolor
+#> 1110:          6.1         2.9          4.7         1.4 versicolor
+#> 1111:          5.6         2.9          3.6         1.3 versicolor
+#> 1112:          6.7         3.1          4.4         1.4 versicolor
+

Except for slice_fst, there are also other functions for subsetting the data, such as select_fst,filter_fst. Good practice is: Make subsets of the data and use the least needy data to do operations. For very large data sets, you may try to do tests on a sample of the data (using slice or select to get several rows or columns) first before you implement a huge operation. Now let’s do a slightly complex manipulation. We’ll use sys_time_print to measure the running time.

+

+sys_time_print({
+  res =  ft %>% 
+   select_fst(Species,Sepal.Length,Sepal.Width) %>% 
+   rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% 
+   arrange(group,sl) %>% 
+   filter(sl > 5) %>% 
+   distinct(sl,.keep_all = TRUE) %>% 
+   summarise(sw = max(sw),by = group)
+})
+#> [1] "Finished in 0.480s elapsed (0.500s cpu)"
+
+res
+#>         group    sw
+#>        <fctr> <num>
+#> 1:     setosa   4.4
+#> 2: versicolor   3.3
+#> 3:  virginica   3.8
+

This should be pretty fast. Becasue when we use the data in fst table, we never get them until using the "_fst" suffix functions, so the tidyft functions never modify the data in the fst file or fst table. That is to say, we do not have to worry about the modification by reference any more. No copies made, fastest ever.

+
+
+

Performance

+

The fst workflow could also be working with other tools, though less efficient. Now let’s compare the performance of tidyft, data.table, dtplyr and dplyr.

+

+rm(list = ls())
+
+library(profvis)
+library(data.table)
+library(dplyr)
+#> 
+#> Attaching package: 'dplyr'
+#> The following objects are masked from 'package:data.table':
+#> 
+#>     between, first, last
+#> The following objects are masked from 'package:tidyft':
+#> 
+#>     add_count, anti_join, arrange, count, cummean, distinct, filter,
+#>     full_join, group_by, groups, inner_join, lag, lead, left_join,
+#>     mutate, nth, pull, rename, right_join, select, select_vars,
+#>     semi_join, slice, summarise, transmute, ungroup
+#> The following objects are masked from 'package:stats':
+#> 
+#>     filter, lag
+#> The following objects are masked from 'package:base':
+#> 
+#>     intersect, setdiff, setequal, union
+library(dtplyr)
+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)
+  
+
+profvis({
+  
+  res1 = ft %>% 
+    select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>% 
+    dplyr::select(-Petal.Length) %>% 
+    dplyr::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% 
+    dplyr::arrange(group,sl) %>% 
+    dplyr::filter(sl > 5) %>% 
+    dplyr::distinct(sl,.keep_all = TRUE) %>% 
+    dplyr::group_by(group) %>% 
+    dplyr::summarise(sw = max(sw))
+  
+  res2 = ft %>% 
+    select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>% 
+    lazy_dt() %>% 
+    dplyr::as_tibble() %>% 
+    dplyr::select(-Petal.Length) %>% 
+    dplyr::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% 
+    dplyr::arrange(group,sl) %>% 
+    dplyr::filter(sl > 5) %>% 
+    dplyr::distinct(sl,.keep_all = TRUE) %>% 
+    dplyr::group_by(group) %>% 
+    dplyr::summarise(sw = max(sw)) %>% 
+    as.data.table()
+  
+  res3 = ft[,c("Species","Sepal.Length","Sepal.Width","Petal.Length")] %>%  
+    setDT() %>%
+    .[,.SD,.SDcols = -"Petal.Length"] %>% 
+    setnames(old =c("Species","Sepal.Length","Sepal.Width"),
+             new = c("group","sl","sw")) %>% 
+    setorder(group,sl) %>% 
+    .[sl>5] %>% unique(by = "sl") %>% 
+    .[,.(sw = max(sw)),by = group]
+  
+  
+  res4 =  ft %>% 
+    tidyft::select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>% 
+    tidyft::select(-Petal.Length) %>% 
+    tidyft::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>% 
+    tidyft::arrange(group,sl) %>% 
+    tidyft::filter(sl > 5) %>% 
+    tidyft::distinct(sl,.keep_all = TRUE) %>% 
+    tidyft::summarise(sw = max(sw),by = group)
+  
+  
+})
+#> Warning: You are using a dplyr method on a raw data.table, which will call the
+#> * data frame implementation, and is likely to be inefficient.
+#> * 
+#> * To suppress this message, either generate a data.table translation with
+#> * `lazy_dt()` or convert to a data frame or tibble with
+#> * `as.data.frame()`/`as_tibble()`.
+
+#> Warning: You are using a dplyr method on a raw data.table, which will call the
+#> * data frame implementation, and is likely to be inefficient.
+#> * 
+#> * To suppress this message, either generate a data.table translation with
+#> * `lazy_dt()` or convert to a data frame or tibble with
+#> * `as.data.frame()`/`as_tibble()`.
+
+ +

+setequal(res1,res2)
+#> [1] TRUE
+setequal(res2,res3)
+#> [1] TRUE
+setequal(res3,res4)
+#> [1] TRUE
+

Because tidyft is based on data.table, therefore, if you always use data.table correctly, then tidyft should not perform better than data.table (I do use some tricks, by never do column selection but delete the unselected ones instead, which is faster and more memory efficient than using .SDcols in data.table). However, tidyft has a very different syntax, which might be more readable. And lots of complex operations of data.table has been wrapped in it. This could save your day to write the correct codes sometimes. I hope all my time devoted to this work could possibly save some of your valuable time on data operations of big datasets.

+
+
+

Session Information

+
sessionInfo()
+#> R version 3.6.3 (2020-02-29)
+#> Platform: x86_64-w64-mingw32/x64 (64-bit)
+#> Running under: Windows 10 x64 (build 18362)
+#> 
+#> Matrix products: default
+#> 
+#> locale:
+#> [1] LC_COLLATE=Chinese (Simplified)_China.936 
+#> [2] LC_CTYPE=Chinese (Simplified)_China.936   
+#> [3] LC_MONETARY=Chinese (Simplified)_China.936
+#> [4] LC_NUMERIC=C                              
+#> [5] LC_TIME=Chinese (Simplified)_China.936    
+#> 
+#> attached base packages:
+#> [1] stats     graphics  grDevices utils     datasets  methods   base     
+#> 
+#> other attached packages:
+#> [1] dtplyr_1.0.1      dplyr_0.8.5       data.table_1.12.8 profvis_0.3.6    
+#> [5] tidyft_0.4.5     
+#> 
+#> loaded via a namespace (and not attached):
+#>  [1] Rcpp_1.0.3        pillar_1.4.3      compiler_3.6.3    prettyunits_1.1.1
+#>  [5] remotes_2.1.1     tools_3.6.3       testthat_2.3.2    digest_0.6.25    
+#>  [9] pkgbuild_1.0.6    pkgload_1.0.2     jsonlite_1.6.1    tibble_2.1.3     
+#> [13] memoise_1.1.0     evaluate_0.14     pkgconfig_2.0.3   rlang_0.4.5      
+#> [17] cli_2.0.2         rstudioapi_0.11   parallel_3.6.3    yaml_2.2.1       
+#> [21] xfun_0.12         withr_2.1.2       stringr_1.4.0     knitr_1.28       
+#> [25] vctrs_0.2.4       htmlwidgets_1.5.1 desc_1.2.0        fs_1.3.1         
+#> [29] devtools_2.2.2    tidyselect_1.0.0  rprojroot_1.3-2   glue_1.3.2       
+#> [33] R6_2.4.1          processx_3.4.2    fansi_0.4.1       rmarkdown_2.1    
+#> [37] sessioninfo_1.1.1 purrr_0.3.3       callr_3.4.2       magrittr_1.5     
+#> [41] backports_1.1.5   ps_1.3.2          ellipsis_0.3.0    htmltools_0.4.0  
+#> [45] usethis_1.5.1     fst_0.9.0         assertthat_0.2.1  stringi_1.4.6    
+#> [49] crayon_1.3.4
+
+ + + + + + + + + + + diff --git a/docs/404.html b/docs/404.html index c20cbe1..8eb158a 100644 --- a/docs/404.html +++ b/docs/404.html @@ -1,156 +1,119 @@ - - - - - - - - -Page not found (404) • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - -
- -
-
- - -Content not found. Please use links in the navbar. - -
- -
- - - - -
- - - - - - - - + + + + + + + +Page not found (404) • tidyft + + + + + + + + + + + + + + + + + + +
+
+ + + + +
+
+ + +Content not found. Please use links in the navbar. + +
+ + + +
+ + + + +
+ + + + + + + + diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index cefb781..e78c129 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -1,71 +1,12 @@ - - - - - - - -License • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -License • tidyft + - - - -
-
- - -
-
+
+ +
-
- +
+ + - - - + diff --git a/docs/LICENSE.html b/docs/LICENSE.html index d392500..a2833ec 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -1,71 +1,12 @@ - - - - - - - -MIT License • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -MIT License • tidyft + - - - -
-
- - -
-
+
+ +
-
- +
+ + - - - + diff --git a/docs/articles/Introduction.html b/docs/articles/Introduction.html index dbf2302..62b38d1 100644 --- a/docs/articles/Introduction.html +++ b/docs/articles/Introduction.html @@ -12,400 +12,444 @@ - - - - + + + + + - - - - - -
-
+
+ + + + +
+

Why tidyft? +

+

Before tidyft, I’ve +designed a package named tidyfst. Backed +by data.table, it is fast and convenient. By then, I was not so +interested in modification by reference, which always causes trouble in +my workflow. Therefore, I use a lot of functions to make copies so as to +suppress the in place replacement. However, when it comes to big data, +simply making a new copy of the original data set could be time +consuming and memory inefficient. So I tried to write some functions +using the feature of modification by reference. This ends up in +inconsistency of many functions in the tidyfst package. In the +end, I removed all the in place replacement functions in +tidyfst and build a new package instead. This is how +tidyft comes into being.

+
+
+

The philosophy of tidyft +

+
+

You cannot step into the same river twice, for other waters are +continually flowing on.

—— +Heraclitus

+
+

If you try to do data operations on any data.table(s), never use it +again for futher analysis, because it is not the data you know before. +And you might never figure out what have happened and what has been +changed in that process. If you really want to use it again, try make a +copy first using copy(), which might take extra time and +space (that’s why tidyft avoid doing this all the time).

+

Another rule is, tidyft only deals with data.table(s), the raw +data.frame and other formats such as tibble could not work. If you +already have lots of data.frames in the environment, try these +codes.

+
+library(tidyft)
+#> 
+#> Life's short, use R.
+#> 
+#> Attaching package: 'tidyft'
+#> The following objects are masked from 'package:stats':
+#> 
+#>     filter, lag
+
+# make copies
+copy(iris) -> a
+copy(mtcars) -> b
+
+# before
+class(a)
+#> [1] "data.frame"
+class(b)
+#> [1] "data.frame"
+
+# convert codes
+lapply(ls(),get) %>%
+  lapply(setDT) %>%
+  invisible()
+
+# after
+class(a)
+#> [1] "data.table" "data.frame"
+class(b)
+#> [1] "data.table" "data.frame"
+

One last thing, while modifications are carried out in place, doesn’t +mean that the results could not be showed after operation. The +data.table package would return it invisibly, but in tidyft, the final +results are always printed if possible. This brings no reduction to the +computation performance.

+
+
+

Working with fst +

+

tidyft would not be so powerful without fst. I first introduce this +workflow into tidyfst. +In such workflow, you do not have to read all data into memory, only +import the needed data when necessary. tidyft is not so convenient for +in-memory operations, but it works very well (if not best) with the fst +workflow. Here we’ll make some examples.

+
+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
+#> <fst file>
+#> 1500000 rows, 5 columns (dt7d0852606272.fst)
+#> 
+#>         Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>             <double>    <double>     <double>    <double>  <factor>
+#> 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
+#> 1499998          6.5         3.0          5.2         2.0 virginica
+#> 1499999          6.2         3.4          5.4         2.3 virginica
+#> 1500000          5.9         3.0          5.1         1.8 virginica
+summary_fst(ft)
+#> <fst file>
+#> 1500000 rows, 5 columns (dt7d0852606272.fst)
+#> 
+#> * 'Sepal.Length': double
+#> * 'Sepal.Width' : double
+#> * 'Petal.Length': double
+#> * 'Petal.Width' : double
+#> * 'Species'     : factor
+
+# list the variables in the environment
+ls() # only the ft exists
+#> [1] "ft"
+

The as_fst could save any data.frame as “.fst” file in +temporary file and parse it back as fst table. Fst table is small in +RAM, but if you want to get any part of the data.frame, you can get it +in almost no time:

+
+ft %>%
+  slice_fst(5555:6666)  # get 5555 to 6666 row
+#>       Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
+#>              <num>       <num>        <num>       <num>     <fctr>
+#>    1:          5.0         3.6          1.4         0.2     setosa
+#>    2:          5.4         3.9          1.7         0.4     setosa
+#>    3:          4.6         3.4          1.4         0.3     setosa
+#>    4:          5.0         3.4          1.5         0.2     setosa
+#>    5:          4.4         2.9          1.4         0.2     setosa
+#>   ---                                                             
+#> 1108:          5.9         3.0          4.2         1.5 versicolor
+#> 1109:          6.0         2.2          4.0         1.0 versicolor
+#> 1110:          6.1         2.9          4.7         1.4 versicolor
+#> 1111:          5.6         2.9          3.6         1.3 versicolor
+#> 1112:          6.7         3.1          4.4         1.4 versicolor
+

Except for slice_fst, there are also other functions for +subsetting the data, such as +select_fst,filter_fst. Good practice is: Make +subsets of the data and use the least needy data to do operations. For +very large data sets, you may try to do tests on a sample of the data +(using slice or select to get several rows or +columns) first before you implement a huge operation. Now let’s do a +slightly complex manipulation. We’ll use sys_time_print to +measure the running time.

+
+
+sys_time_print({
+  res =  ft %>%
+   select_fst(Species,Sepal.Length,Sepal.Width) %>%
+   rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>%
+   arrange(group,sl) %>%
+   filter(sl > 5) %>%
+   distinct(sl,.keep_all = TRUE) %>%
+   summarise(sw = max(sw),by = group)
+})
+#> [1] "Finished in 0.310s elapsed (0.140s cpu)"
+
+res
+#>         group    sw
+#>        <fctr> <num>
+#> 1:     setosa   4.4
+#> 2: versicolor   3.3
+#> 3:  virginica   3.8
+

This should be pretty fast. Becasue when we use the data in fst +table, we never get them until using the “_fst” suffix functions, so the +tidyft functions never modify the data in the fst file or fst table. +That is to say, we do not have to worry about the modification by +reference any more. No copies made, fastest ever.

+
+
+

Performance +

+

The fst workflow could also be working with other tools, though less +efficient. Now let’s compare the performance of tidyft, data.table, +dtplyr and dplyr.

+
+
+rm(list = ls())
+
+library(data.table)
+library(dplyr)
+#> 
+#> Attaching package: 'dplyr'
+#> The following objects are masked from 'package:data.table':
+#> 
+#>     between, first, last
+#> The following objects are masked from 'package:tidyft':
+#> 
+#>     add_count, anti_join, arrange, count, cummean, distinct, filter,
+#>     full_join, group_by, groups, inner_join, lag, lead, left_join,
+#>     mutate, nth, pull, relocate, rename, right_join, select,
+#>     select_vars, semi_join, slice, slice_head, slice_max, slice_min,
+#>     slice_sample, slice_tail, summarise, transmute, ungroup
+#> The following objects are masked from 'package:stats':
+#> 
+#>     filter, lag
+#> The following objects are masked from 'package:base':
+#> 
+#>     intersect, setdiff, setequal, union
+library(dtplyr)
+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)
+
+
+bench::mark(
+
+  dplyr = ft %>%
+    select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>%
+    dplyr::select(-Petal.Length) %>%
+    dplyr::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>%
+    dplyr::arrange(group,sl) %>%
+    dplyr::filter(sl > 5) %>%
+    dplyr::distinct(sl,.keep_all = TRUE) %>%
+    dplyr::group_by(group) %>%
+    dplyr::summarise(sw = max(sw)),
+
+  dtplyr = ft %>%
+    select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>%
+    lazy_dt() %>%
+    dplyr::select(-Petal.Length) %>%
+    dplyr::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>%
+    dplyr::arrange(group,sl) %>%
+    dplyr::filter(sl > 5) %>%
+    dplyr::distinct(sl,.keep_all = TRUE) %>%
+    dplyr::group_by(group) %>%
+    dplyr::summarise(sw = max(sw)) %>%
+    as.data.table(),
+
+  data.table = ft[,c("Species","Sepal.Length","Sepal.Width","Petal.Length")] %>%
+    setDT() %>%
+    .[,.SD,.SDcols = -"Petal.Length"] %>%
+    setnames(old =c("Species","Sepal.Length","Sepal.Width"),
+             new = c("group","sl","sw")) %>%
+    setorder(group,sl) %>%
+    .[sl>5] %>% unique(by = "sl") %>%
+    .[,.(sw = max(sw)),by = group],
+
+
+  tidyft =  ft %>%
+    tidyft::select_fst(Species,Sepal.Length,Sepal.Width,Petal.Length) %>%
+    tidyft::select(-Petal.Length) %>%
+    tidyft::rename(group = Species,sl = Sepal.Length,sw = Sepal.Width) %>%
+    tidyft::arrange(group,sl) %>%
+    tidyft::filter(sl > 5) %>%
+    tidyft::distinct(sl,.keep_all = TRUE) %>%
+    tidyft::summarise(sw = max(sw),by = group),
+
+  check = setequal
+)
+#> Warning: Some expressions had a GC in every iteration; so filtering is
+#> disabled.
+#> # A tibble: 4 × 6
+#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
+#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
+#> 1 dplyr       106.8ms    146ms      7.08     179MB     19.5
+#> 2 dtplyr        183ms    186ms      5.39     200MB     10.8
+#> 3 data.table   93.3ms    112ms      8.74     130MB     12.2
+#> 4 tidyft       92.8ms     97ms      9.50     102MB     12.7
+

Because tidyft is based on data.table, therefore, if you always use +data.table correctly, then tidyft should not perform better than +data.table (I do use some tricks, by never do column selection but +delete the unselected ones instead, which is faster and more memory +efficient than using .SDcols in data.table). However, +tidyft has a very different syntax, which might be more readable. And +lots of complex operations of data.table has been wrapped in it. This +could save your day to write the correct codes sometimes. I hope all my +time devoted to this work could possibly save some of your valuable time +on data operations of big datasets.

+
+
+

Session Information +

+
+sessionInfo()
+#> R version 4.4.1 (2024-06-14 ucrt)
+#> Platform: x86_64-w64-mingw32/x64
+#> Running under: Windows 11 x64 (build 22631)
+#> 
+#> Matrix products: default
+#> 
+#> 
+#> locale:
+#> [1] LC_COLLATE=Chinese (Simplified)_China.utf8 
+#> [2] LC_CTYPE=Chinese (Simplified)_China.utf8   
+#> [3] LC_MONETARY=Chinese (Simplified)_China.utf8
+#> [4] LC_NUMERIC=C                               
+#> [5] LC_TIME=Chinese (Simplified)_China.utf8    
+#> 
+#> time zone: Asia/Shanghai
+#> tzcode source: internal
+#> 
+#> attached base packages:
+#> [1] stats     graphics  grDevices utils     datasets  methods   base     
+#> 
+#> other attached packages:
+#> [1] dtplyr_1.3.1      dplyr_1.1.4       data.table_1.16.0 fstcore_0.9.18   
+#> [5] tidyft_0.9.20    
+#> 
+#> loaded via a namespace (and not attached):
+#>  [1] jsonlite_1.8.8    compiler_4.4.1    crayon_1.5.3      tidyselect_1.2.1 
+#>  [5] Rcpp_1.0.13       stringr_1.5.1     parallel_4.4.1    jquerylib_0.1.4  
+#>  [9] systemfonts_1.1.0 textshaping_0.4.0 yaml_2.3.9        fastmap_1.2.0    
+#> [13] R6_2.5.1          generics_0.1.3    knitr_1.48        htmlwidgets_1.6.4
+#> [17] tibble_3.2.1      desc_1.4.3        bslib_0.7.0       pillar_1.9.0     
+#> [21] rlang_1.1.4       utf8_1.2.4        cachem_1.1.0      stringi_1.8.4    
+#> [25] xfun_0.46         fs_1.6.4          sass_0.4.9        cli_3.6.3        
+#> [29] withr_3.0.1       pkgdown_2.1.0     magrittr_2.0.3    digest_0.6.36    
+#> [33] rstudioapi_0.16.0 fst_0.9.8         lifecycle_1.0.4   vctrs_0.6.5      
+#> [37] bench_1.1.3       evaluate_0.24.0   glue_1.7.0        ragg_1.3.2       
+#> [41] profmem_0.6.0     fansi_1.0.6       rmarkdown_2.27    tools_4.4.1      
+#> [45] pkgconfig_2.0.3   htmltools_0.5.8.1
+
+
+ + + +
+ + + +
+ +
+

+

Site built with pkgdown 2.1.0.

+
+
-
- - - - +
+ + + + + + diff --git a/docs/articles/Introduction_files/htmlwidgets-1.6.1/htmlwidgets.js b/docs/articles/Introduction_files/htmlwidgets-1.6.1/htmlwidgets.js new file mode 100644 index 0000000..1067d02 --- /dev/null +++ b/docs/articles/Introduction_files/htmlwidgets-1.6.1/htmlwidgets.js @@ -0,0 +1,901 @@ +(function() { + // If window.HTMLWidgets is already defined, then use it; otherwise create a + // new object. This allows preceding code to set options that affect the + // initialization process (though none currently exist). + window.HTMLWidgets = window.HTMLWidgets || {}; + + // See if we're running in a viewer pane. If not, we're in a web browser. + var viewerMode = window.HTMLWidgets.viewerMode = + /\bviewer_pane=1\b/.test(window.location); + + // See if we're running in Shiny mode. If not, it's a static document. + // Note that static widgets can appear in both Shiny and static modes, but + // obviously, Shiny widgets can only appear in Shiny apps/documents. + var shinyMode = window.HTMLWidgets.shinyMode = + typeof(window.Shiny) !== "undefined" && !!window.Shiny.outputBindings; + + // We can't count on jQuery being available, so we implement our own + // version if necessary. + function querySelectorAll(scope, selector) { + if (typeof(jQuery) !== "undefined" && scope instanceof jQuery) { + return scope.find(selector); + } + if (scope.querySelectorAll) { + return scope.querySelectorAll(selector); + } + } + + function asArray(value) { + if (value === null) + return []; + if ($.isArray(value)) + return value; + return [value]; + } + + // Implement jQuery's extend + function extend(target /*, ... */) { + if (arguments.length == 1) { + return target; + } + for (var i = 1; i < arguments.length; i++) { + var source = arguments[i]; + for (var prop in source) { + if (source.hasOwnProperty(prop)) { + target[prop] = source[prop]; + } + } + } + return target; + } + + // IE8 doesn't support Array.forEach. + function forEach(values, callback, thisArg) { + if (values.forEach) { + values.forEach(callback, thisArg); + } else { + for (var i = 0; i < values.length; i++) { + callback.call(thisArg, values[i], i, values); + } + } + } + + // Replaces the specified method with the return value of funcSource. + // + // Note that funcSource should not BE the new method, it should be a function + // that RETURNS the new method. funcSource receives a single argument that is + // the overridden method, it can be called from the new method. The overridden + // method can be called like a regular function, it has the target permanently + // bound to it so "this" will work correctly. + function overrideMethod(target, methodName, funcSource) { + var superFunc = target[methodName] || function() {}; + var superFuncBound = function() { + return superFunc.apply(target, arguments); + }; + target[methodName] = funcSource(superFuncBound); + } + + // Add a method to delegator that, when invoked, calls + // delegatee.methodName. If there is no such method on + // the delegatee, but there was one on delegator before + // delegateMethod was called, then the original version + // is invoked instead. + // For example: + // + // var a = { + // method1: function() { console.log('a1'); } + // method2: function() { console.log('a2'); } + // }; + // var b = { + // method1: function() { console.log('b1'); } + // }; + // delegateMethod(a, b, "method1"); + // delegateMethod(a, b, "method2"); + // a.method1(); + // a.method2(); + // + // The output would be "b1", "a2". + function delegateMethod(delegator, delegatee, methodName) { + var inherited = delegator[methodName]; + delegator[methodName] = function() { + var target = delegatee; + var method = delegatee[methodName]; + + // The method doesn't exist on the delegatee. Instead, + // call the method on the delegator, if it exists. + if (!method) { + target = delegator; + method = inherited; + } + + if (method) { + return method.apply(target, arguments); + } + }; + } + + // Implement a vague facsimilie of jQuery's data method + function elementData(el, name, value) { + if (arguments.length == 2) { + return el["htmlwidget_data_" + name]; + } else if (arguments.length == 3) { + el["htmlwidget_data_" + name] = value; + return el; + } else { + throw new Error("Wrong number of arguments for elementData: " + + arguments.length); + } + } + + // http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex + function escapeRegExp(str) { + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); + } + + function hasClass(el, className) { + var re = new RegExp("\\b" + escapeRegExp(className) + "\\b"); + return re.test(el.className); + } + + // elements - array (or array-like object) of HTML elements + // className - class name to test for + // include - if true, only return elements with given className; + // if false, only return elements *without* given className + function filterByClass(elements, className, include) { + var results = []; + for (var i = 0; i < elements.length; i++) { + if (hasClass(elements[i], className) == include) + results.push(elements[i]); + } + return results; + } + + function on(obj, eventName, func) { + if (obj.addEventListener) { + obj.addEventListener(eventName, func, false); + } else if (obj.attachEvent) { + obj.attachEvent(eventName, func); + } + } + + function off(obj, eventName, func) { + if (obj.removeEventListener) + obj.removeEventListener(eventName, func, false); + else if (obj.detachEvent) { + obj.detachEvent(eventName, func); + } + } + + // Translate array of values to top/right/bottom/left, as usual with + // the "padding" CSS property + // https://developer.mozilla.org/en-US/docs/Web/CSS/padding + function unpackPadding(value) { + if (typeof(value) === "number") + value = [value]; + if (value.length === 1) { + return {top: value[0], right: value[0], bottom: value[0], left: value[0]}; + } + if (value.length === 2) { + return {top: value[0], right: value[1], bottom: value[0], left: value[1]}; + } + if (value.length === 3) { + return {top: value[0], right: value[1], bottom: value[2], left: value[1]}; + } + if (value.length === 4) { + return {top: value[0], right: value[1], bottom: value[2], left: value[3]}; + } + } + + // Convert an unpacked padding object to a CSS value + function paddingToCss(paddingObj) { + return paddingObj.top + "px " + paddingObj.right + "px " + paddingObj.bottom + "px " + paddingObj.left + "px"; + } + + // Makes a number suitable for CSS + function px(x) { + if (typeof(x) === "number") + return x + "px"; + else + return x; + } + + // Retrieves runtime widget sizing information for an element. + // The return value is either null, or an object with fill, padding, + // defaultWidth, defaultHeight fields. + function sizingPolicy(el) { + var sizingEl = document.querySelector("script[data-for='" + el.id + "'][type='application/htmlwidget-sizing']"); + if (!sizingEl) + return null; + var sp = JSON.parse(sizingEl.textContent || sizingEl.text || "{}"); + if (viewerMode) { + return sp.viewer; + } else { + return sp.browser; + } + } + + // @param tasks Array of strings (or falsy value, in which case no-op). + // Each element must be a valid JavaScript expression that yields a + // function. Or, can be an array of objects with "code" and "data" + // properties; in this case, the "code" property should be a string + // of JS that's an expr that yields a function, and "data" should be + // an object that will be added as an additional argument when that + // function is called. + // @param target The object that will be "this" for each function + // execution. + // @param args Array of arguments to be passed to the functions. (The + // same arguments will be passed to all functions.) + function evalAndRun(tasks, target, args) { + if (tasks) { + forEach(tasks, function(task) { + var theseArgs = args; + if (typeof(task) === "object") { + theseArgs = theseArgs.concat([task.data]); + task = task.code; + } + var taskFunc = tryEval(task); + if (typeof(taskFunc) !== "function") { + throw new Error("Task must be a function! Source:\n" + task); + } + taskFunc.apply(target, theseArgs); + }); + } + } + + // Attempt eval() both with and without enclosing in parentheses. + // Note that enclosing coerces a function declaration into + // an expression that eval() can parse + // (otherwise, a SyntaxError is thrown) + function tryEval(code) { + var result = null; + try { + result = eval("(" + code + ")"); + } catch(error) { + if (!(error instanceof SyntaxError)) { + throw error; + } + try { + result = eval(code); + } catch(e) { + if (e instanceof SyntaxError) { + throw error; + } else { + throw e; + } + } + } + return result; + } + + function initSizing(el) { + var sizing = sizingPolicy(el); + if (!sizing) + return; + + var cel = document.getElementById("htmlwidget_container"); + if (!cel) + return; + + if (typeof(sizing.padding) !== "undefined") { + document.body.style.margin = "0"; + document.body.style.padding = paddingToCss(unpackPadding(sizing.padding)); + } + + if (sizing.fill) { + document.body.style.overflow = "hidden"; + document.body.style.width = "100%"; + document.body.style.height = "100%"; + document.documentElement.style.width = "100%"; + document.documentElement.style.height = "100%"; + cel.style.position = "absolute"; + var pad = unpackPadding(sizing.padding); + cel.style.top = pad.top + "px"; + cel.style.right = pad.right + "px"; + cel.style.bottom = pad.bottom + "px"; + cel.style.left = pad.left + "px"; + el.style.width = "100%"; + el.style.height = "100%"; + + return { + getWidth: function() { return cel.getBoundingClientRect().width; }, + getHeight: function() { return cel.getBoundingClientRect().height; } + }; + + } else { + el.style.width = px(sizing.width); + el.style.height = px(sizing.height); + + return { + getWidth: function() { return cel.getBoundingClientRect().width; }, + getHeight: function() { return cel.getBoundingClientRect().height; } + }; + } + } + + // Default implementations for methods + var defaults = { + find: function(scope) { + return querySelectorAll(scope, "." + this.name); + }, + renderError: function(el, err) { + var $el = $(el); + + this.clearError(el); + + // Add all these error classes, as Shiny does + var errClass = "shiny-output-error"; + if (err.type !== null) { + // use the classes of the error condition as CSS class names + errClass = errClass + " " + $.map(asArray(err.type), function(type) { + return errClass + "-" + type; + }).join(" "); + } + errClass = errClass + " htmlwidgets-error"; + + // Is el inline or block? If inline or inline-block, just display:none it + // and add an inline error. + var display = $el.css("display"); + $el.data("restore-display-mode", display); + + if (display === "inline" || display === "inline-block") { + $el.hide(); + if (err.message !== "") { + var errorSpan = $("").addClass(errClass); + errorSpan.text(err.message); + $el.after(errorSpan); + } + } else if (display === "block") { + // If block, add an error just after the el, set visibility:none on the + // el, and position the error to be on top of the el. + // Mark it with a unique ID and CSS class so we can remove it later. + $el.css("visibility", "hidden"); + if (err.message !== "") { + var errorDiv = $("
").addClass(errClass).css("position", "absolute") + .css("top", el.offsetTop) + .css("left", el.offsetLeft) + // setting width can push out the page size, forcing otherwise + // unnecessary scrollbars to appear and making it impossible for + // the element to shrink; so use max-width instead + .css("maxWidth", el.offsetWidth) + .css("height", el.offsetHeight); + errorDiv.text(err.message); + $el.after(errorDiv); + + // Really dumb way to keep the size/position of the error in sync with + // the parent element as the window is resized or whatever. + var intId = setInterval(function() { + if (!errorDiv[0].parentElement) { + clearInterval(intId); + return; + } + errorDiv + .css("top", el.offsetTop) + .css("left", el.offsetLeft) + .css("maxWidth", el.offsetWidth) + .css("height", el.offsetHeight); + }, 500); + } + } + }, + clearError: function(el) { + var $el = $(el); + var display = $el.data("restore-display-mode"); + $el.data("restore-display-mode", null); + + if (display === "inline" || display === "inline-block") { + if (display) + $el.css("display", display); + $(el.nextSibling).filter(".htmlwidgets-error").remove(); + } else if (display === "block"){ + $el.css("visibility", "inherit"); + $(el.nextSibling).filter(".htmlwidgets-error").remove(); + } + }, + sizing: {} + }; + + // Called by widget bindings to register a new type of widget. The definition + // object can contain the following properties: + // - name (required) - A string indicating the binding name, which will be + // used by default as the CSS classname to look for. + // - initialize (optional) - A function(el) that will be called once per + // widget element; if a value is returned, it will be passed as the third + // value to renderValue. + // - renderValue (required) - A function(el, data, initValue) that will be + // called with data. Static contexts will cause this to be called once per + // element; Shiny apps will cause this to be called multiple times per + // element, as the data changes. + window.HTMLWidgets.widget = function(definition) { + if (!definition.name) { + throw new Error("Widget must have a name"); + } + if (!definition.type) { + throw new Error("Widget must have a type"); + } + // Currently we only support output widgets + if (definition.type !== "output") { + throw new Error("Unrecognized widget type '" + definition.type + "'"); + } + // TODO: Verify that .name is a valid CSS classname + + // Support new-style instance-bound definitions. Old-style class-bound + // definitions have one widget "object" per widget per type/class of + // widget; the renderValue and resize methods on such widget objects + // take el and instance arguments, because the widget object can't + // store them. New-style instance-bound definitions have one widget + // object per widget instance; the definition that's passed in doesn't + // provide renderValue or resize methods at all, just the single method + // factory(el, width, height) + // which returns an object that has renderValue(x) and resize(w, h). + // This enables a far more natural programming style for the widget + // author, who can store per-instance state using either OO-style + // instance fields or functional-style closure variables (I guess this + // is in contrast to what can only be called C-style pseudo-OO which is + // what we required before). + if (definition.factory) { + definition = createLegacyDefinitionAdapter(definition); + } + + if (!definition.renderValue) { + throw new Error("Widget must have a renderValue function"); + } + + // For static rendering (non-Shiny), use a simple widget registration + // scheme. We also use this scheme for Shiny apps/documents that also + // contain static widgets. + window.HTMLWidgets.widgets = window.HTMLWidgets.widgets || []; + // Merge defaults into the definition; don't mutate the original definition. + var staticBinding = extend({}, defaults, definition); + overrideMethod(staticBinding, "find", function(superfunc) { + return function(scope) { + var results = superfunc(scope); + // Filter out Shiny outputs, we only want the static kind + return filterByClass(results, "html-widget-output", false); + }; + }); + window.HTMLWidgets.widgets.push(staticBinding); + + if (shinyMode) { + // Shiny is running. Register the definition with an output binding. + // The definition itself will not be the output binding, instead + // we will make an output binding object that delegates to the + // definition. This is because we foolishly used the same method + // name (renderValue) for htmlwidgets definition and Shiny bindings + // but they actually have quite different semantics (the Shiny + // bindings receive data that includes lots of metadata that it + // strips off before calling htmlwidgets renderValue). We can't + // just ignore the difference because in some widgets it's helpful + // to call this.renderValue() from inside of resize(), and if + // we're not delegating, then that call will go to the Shiny + // version instead of the htmlwidgets version. + + // Merge defaults with definition, without mutating either. + var bindingDef = extend({}, defaults, definition); + + // This object will be our actual Shiny binding. + var shinyBinding = new Shiny.OutputBinding(); + + // With a few exceptions, we'll want to simply use the bindingDef's + // version of methods if they are available, otherwise fall back to + // Shiny's defaults. NOTE: If Shiny's output bindings gain additional + // methods in the future, and we want them to be overrideable by + // HTMLWidget binding definitions, then we'll need to add them to this + // list. + delegateMethod(shinyBinding, bindingDef, "getId"); + delegateMethod(shinyBinding, bindingDef, "onValueChange"); + delegateMethod(shinyBinding, bindingDef, "onValueError"); + delegateMethod(shinyBinding, bindingDef, "renderError"); + delegateMethod(shinyBinding, bindingDef, "clearError"); + delegateMethod(shinyBinding, bindingDef, "showProgress"); + + // The find, renderValue, and resize are handled differently, because we + // want to actually decorate the behavior of the bindingDef methods. + + shinyBinding.find = function(scope) { + var results = bindingDef.find(scope); + + // Only return elements that are Shiny outputs, not static ones + var dynamicResults = results.filter(".html-widget-output"); + + // It's possible that whatever caused Shiny to think there might be + // new dynamic outputs, also caused there to be new static outputs. + // Since there might be lots of different htmlwidgets bindings, we + // schedule execution for later--no need to staticRender multiple + // times. + if (results.length !== dynamicResults.length) + scheduleStaticRender(); + + return dynamicResults; + }; + + // Wrap renderValue to handle initialization, which unfortunately isn't + // supported natively by Shiny at the time of this writing. + + shinyBinding.renderValue = function(el, data) { + Shiny.renderDependencies(data.deps); + // Resolve strings marked as javascript literals to objects + if (!(data.evals instanceof Array)) data.evals = [data.evals]; + for (var i = 0; data.evals && i < data.evals.length; i++) { + window.HTMLWidgets.evaluateStringMember(data.x, data.evals[i]); + } + if (!bindingDef.renderOnNullValue) { + if (data.x === null) { + el.style.visibility = "hidden"; + return; + } else { + el.style.visibility = "inherit"; + } + } + if (!elementData(el, "initialized")) { + initSizing(el); + + elementData(el, "initialized", true); + if (bindingDef.initialize) { + var rect = el.getBoundingClientRect(); + var result = bindingDef.initialize(el, rect.width, rect.height); + elementData(el, "init_result", result); + } + } + bindingDef.renderValue(el, data.x, elementData(el, "init_result")); + evalAndRun(data.jsHooks.render, elementData(el, "init_result"), [el, data.x]); + }; + + // Only override resize if bindingDef implements it + if (bindingDef.resize) { + shinyBinding.resize = function(el, width, height) { + // Shiny can call resize before initialize/renderValue have been + // called, which doesn't make sense for widgets. + if (elementData(el, "initialized")) { + bindingDef.resize(el, width, height, elementData(el, "init_result")); + } + }; + } + + Shiny.outputBindings.register(shinyBinding, bindingDef.name); + } + }; + + var scheduleStaticRenderTimerId = null; + function scheduleStaticRender() { + if (!scheduleStaticRenderTimerId) { + scheduleStaticRenderTimerId = setTimeout(function() { + scheduleStaticRenderTimerId = null; + window.HTMLWidgets.staticRender(); + }, 1); + } + } + + // Render static widgets after the document finishes loading + // Statically render all elements that are of this widget's class + window.HTMLWidgets.staticRender = function() { + var bindings = window.HTMLWidgets.widgets || []; + forEach(bindings, function(binding) { + var matches = binding.find(document.documentElement); + forEach(matches, function(el) { + var sizeObj = initSizing(el, binding); + + var getSize = function(el) { + if (sizeObj) { + return {w: sizeObj.getWidth(), h: sizeObj.getHeight()} + } else { + var rect = el.getBoundingClientRect(); + return {w: rect.width, h: rect.height} + } + }; + + if (hasClass(el, "html-widget-static-bound")) + return; + el.className = el.className + " html-widget-static-bound"; + + var initResult; + if (binding.initialize) { + var size = getSize(el); + initResult = binding.initialize(el, size.w, size.h); + elementData(el, "init_result", initResult); + } + + if (binding.resize) { + var lastSize = getSize(el); + var resizeHandler = function(e) { + var size = getSize(el); + if (size.w === 0 && size.h === 0) + return; + if (size.w === lastSize.w && size.h === lastSize.h) + return; + lastSize = size; + binding.resize(el, size.w, size.h, initResult); + }; + + on(window, "resize", resizeHandler); + + // This is needed for cases where we're running in a Shiny + // app, but the widget itself is not a Shiny output, but + // rather a simple static widget. One example of this is + // an rmarkdown document that has runtime:shiny and widget + // that isn't in a render function. Shiny only knows to + // call resize handlers for Shiny outputs, not for static + // widgets, so we do it ourselves. + if (window.jQuery) { + window.jQuery(document).on( + "shown.htmlwidgets shown.bs.tab.htmlwidgets shown.bs.collapse.htmlwidgets", + resizeHandler + ); + window.jQuery(document).on( + "hidden.htmlwidgets hidden.bs.tab.htmlwidgets hidden.bs.collapse.htmlwidgets", + resizeHandler + ); + } + + // This is needed for the specific case of ioslides, which + // flips slides between display:none and display:block. + // Ideally we would not have to have ioslide-specific code + // here, but rather have ioslides raise a generic event, + // but the rmarkdown package just went to CRAN so the + // window to getting that fixed may be long. + if (window.addEventListener) { + // It's OK to limit this to window.addEventListener + // browsers because ioslides itself only supports + // such browsers. + on(document, "slideenter", resizeHandler); + on(document, "slideleave", resizeHandler); + } + } + + var scriptData = document.querySelector("script[data-for='" + el.id + "'][type='application/json']"); + if (scriptData) { + var data = JSON.parse(scriptData.textContent || scriptData.text); + // Resolve strings marked as javascript literals to objects + if (!(data.evals instanceof Array)) data.evals = [data.evals]; + for (var k = 0; data.evals && k < data.evals.length; k++) { + window.HTMLWidgets.evaluateStringMember(data.x, data.evals[k]); + } + binding.renderValue(el, data.x, initResult); + evalAndRun(data.jsHooks.render, initResult, [el, data.x]); + } + }); + }); + + invokePostRenderHandlers(); + } + + + function has_jQuery3() { + if (!window.jQuery) { + return false; + } + var $version = window.jQuery.fn.jquery; + var $major_version = parseInt($version.split(".")[0]); + return $major_version >= 3; + } + + /* + / Shiny 1.4 bumped jQuery from 1.x to 3.x which means jQuery's + / on-ready handler (i.e., $(fn)) is now asyncronous (i.e., it now + / really means $(setTimeout(fn)). + / https://jquery.com/upgrade-guide/3.0/#breaking-change-document-ready-handlers-are-now-asynchronous + / + / Since Shiny uses $() to schedule initShiny, shiny>=1.4 calls initShiny + / one tick later than it did before, which means staticRender() is + / called renderValue() earlier than (advanced) widget authors might be expecting. + / https://github.com/rstudio/shiny/issues/2630 + / + / For a concrete example, leaflet has some methods (e.g., updateBounds) + / which reference Shiny methods registered in initShiny (e.g., setInputValue). + / Since leaflet is privy to this life-cycle, it knows to use setTimeout() to + / delay execution of those methods (until Shiny methods are ready) + / https://github.com/rstudio/leaflet/blob/18ec981/javascript/src/index.js#L266-L268 + / + / Ideally widget authors wouldn't need to use this setTimeout() hack that + / leaflet uses to call Shiny methods on a staticRender(). In the long run, + / the logic initShiny should be broken up so that method registration happens + / right away, but binding happens later. + */ + function maybeStaticRenderLater() { + if (shinyMode && has_jQuery3()) { + window.jQuery(window.HTMLWidgets.staticRender); + } else { + window.HTMLWidgets.staticRender(); + } + } + + if (document.addEventListener) { + document.addEventListener("DOMContentLoaded", function() { + document.removeEventListener("DOMContentLoaded", arguments.callee, false); + maybeStaticRenderLater(); + }, false); + } else if (document.attachEvent) { + document.attachEvent("onreadystatechange", function() { + if (document.readyState === "complete") { + document.detachEvent("onreadystatechange", arguments.callee); + maybeStaticRenderLater(); + } + }); + } + + + window.HTMLWidgets.getAttachmentUrl = function(depname, key) { + // If no key, default to the first item + if (typeof(key) === "undefined") + key = 1; + + var link = document.getElementById(depname + "-" + key + "-attachment"); + if (!link) { + throw new Error("Attachment " + depname + "/" + key + " not found in document"); + } + return link.getAttribute("href"); + }; + + window.HTMLWidgets.dataframeToD3 = function(df) { + var names = []; + var length; + for (var name in df) { + if (df.hasOwnProperty(name)) + names.push(name); + if (typeof(df[name]) !== "object" || typeof(df[name].length) === "undefined") { + throw new Error("All fields must be arrays"); + } else if (typeof(length) !== "undefined" && length !== df[name].length) { + throw new Error("All fields must be arrays of the same length"); + } + length = df[name].length; + } + var results = []; + var item; + for (var row = 0; row < length; row++) { + item = {}; + for (var col = 0; col < names.length; col++) { + item[names[col]] = df[names[col]][row]; + } + results.push(item); + } + return results; + }; + + window.HTMLWidgets.transposeArray2D = function(array) { + if (array.length === 0) return array; + var newArray = array[0].map(function(col, i) { + return array.map(function(row) { + return row[i] + }) + }); + return newArray; + }; + // Split value at splitChar, but allow splitChar to be escaped + // using escapeChar. Any other characters escaped by escapeChar + // will be included as usual (including escapeChar itself). + function splitWithEscape(value, splitChar, escapeChar) { + var results = []; + var escapeMode = false; + var currentResult = ""; + for (var pos = 0; pos < value.length; pos++) { + if (!escapeMode) { + if (value[pos] === splitChar) { + results.push(currentResult); + currentResult = ""; + } else if (value[pos] === escapeChar) { + escapeMode = true; + } else { + currentResult += value[pos]; + } + } else { + currentResult += value[pos]; + escapeMode = false; + } + } + if (currentResult !== "") { + results.push(currentResult); + } + return results; + } + // Function authored by Yihui/JJ Allaire + window.HTMLWidgets.evaluateStringMember = function(o, member) { + var parts = splitWithEscape(member, '.', '\\'); + for (var i = 0, l = parts.length; i < l; i++) { + var part = parts[i]; + // part may be a character or 'numeric' member name + if (o !== null && typeof o === "object" && part in o) { + if (i == (l - 1)) { // if we are at the end of the line then evalulate + if (typeof o[part] === "string") + o[part] = tryEval(o[part]); + } else { // otherwise continue to next embedded object + o = o[part]; + } + } + } + }; + + // Retrieve the HTMLWidget instance (i.e. the return value of an + // HTMLWidget binding's initialize() or factory() function) + // associated with an element, or null if none. + window.HTMLWidgets.getInstance = function(el) { + return elementData(el, "init_result"); + }; + + // Finds the first element in the scope that matches the selector, + // and returns the HTMLWidget instance (i.e. the return value of + // an HTMLWidget binding's initialize() or factory() function) + // associated with that element, if any. If no element matches the + // selector, or the first matching element has no HTMLWidget + // instance associated with it, then null is returned. + // + // The scope argument is optional, and defaults to window.document. + window.HTMLWidgets.find = function(scope, selector) { + if (arguments.length == 1) { + selector = scope; + scope = document; + } + + var el = scope.querySelector(selector); + if (el === null) { + return null; + } else { + return window.HTMLWidgets.getInstance(el); + } + }; + + // Finds all elements in the scope that match the selector, and + // returns the HTMLWidget instances (i.e. the return values of + // an HTMLWidget binding's initialize() or factory() function) + // associated with the elements, in an array. If elements that + // match the selector don't have an associated HTMLWidget + // instance, the returned array will contain nulls. + // + // The scope argument is optional, and defaults to window.document. + window.HTMLWidgets.findAll = function(scope, selector) { + if (arguments.length == 1) { + selector = scope; + scope = document; + } + + var nodes = scope.querySelectorAll(selector); + var results = []; + for (var i = 0; i < nodes.length; i++) { + results.push(window.HTMLWidgets.getInstance(nodes[i])); + } + return results; + }; + + var postRenderHandlers = []; + function invokePostRenderHandlers() { + while (postRenderHandlers.length) { + var handler = postRenderHandlers.shift(); + if (handler) { + handler(); + } + } + } + + // Register the given callback function to be invoked after the + // next time static widgets are rendered. + window.HTMLWidgets.addPostRenderHandler = function(callback) { + postRenderHandlers.push(callback); + }; + + // Takes a new-style instance-bound definition, and returns an + // old-style class-bound definition. This saves us from having + // to rewrite all the logic in this file to accomodate both + // types of definitions. + function createLegacyDefinitionAdapter(defn) { + var result = { + name: defn.name, + type: defn.type, + initialize: function(el, width, height) { + return defn.factory(el, width, height); + }, + renderValue: function(el, x, instance) { + return instance.renderValue(x); + }, + resize: function(el, width, height, instance) { + return instance.resize(width, height); + } + }; + + if (defn.find) + result.find = defn.find; + if (defn.renderError) + result.renderError = defn.renderError; + if (defn.clearError) + result.clearError = defn.clearError; + + return result; + } +})(); diff --git a/docs/articles/Introduction_files/profvis-0.3.6.9000/profvis.css b/docs/articles/Introduction_files/profvis-0.3.6.9000/profvis.css new file mode 100644 index 0000000..2f63924 --- /dev/null +++ b/docs/articles/Introduction_files/profvis-0.3.6.9000/profvis.css @@ -0,0 +1,696 @@ +.profvis { + position: relative; +} + +.profvis * { + /* Need to disable box-sizing:border-box if enabled from other CSS (like + Bootstrap) */ + -webkit-box-sizing: content-box; + -moz-box-sizing: content-box; + box-sizing: content-box; +} + +.profvis-footer .info-label { + cursor: default; +} + +.profvis-panel1 { + left: 0px; + top: 23px; + position: absolute; +} + +.profvis-panel1-vertical { + bottom: 0px; + width: 500px; + margin-bottom: 20px; +} + +.profvis-panel1-horizontal { + height: 378px; + right: 0px; +} + +.profvis-panel2 { + position: absolute; + right: 0px; + bottom: 0px; +} + +.profvis-panel2-vertical { + top: 23px; + left: 508px; + margin-bottom: 20px; +} + +.profvis-panel2-horizontal { + left: 0px; + top: 408px; + margin-bottom: 20px; +} + +.profvis-splitbar { + position: absolute; + background-color: rgb(224, 224, 224); + border-color: #ddd; + border-style: solid; + background-repeat: no-repeat; + background-position: center; +} + +.profvis-splitbar-vertical { + cursor: col-resize; + left: 500px; + width: 8px; + top: 23px; + bottom: 0px; + border-width: 0 1px; + background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAHCAYAAADNufepAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrIsAHvgABeNMLCADATZvAMByH/w/qQplcAYCEAcB0kThLCIAUAEB6jkKmAEBGAYCdmCZTAKAEAGDLY2LjAFAtAGAnf+bTAICd+Jl7AQBblCEVAaCRACATZYhEAGg7AKzPVopFAFgwABRmS8Q5ANgtADBJV2ZIALC3AMDOEAuyAAgMADBRiIUpAAR7AGDIIyN4AISZABRG8lc88SuuEOcqAAB4mbI8uSQ5RYFbCC1xB1dXLh4ozkkXKxQ2YQJhmkAuwnmZGTKBNA/g88wAAKCRFRHgg/P9eM4Ors7ONo62Dl8t6r8G/yJiYuP+5c+rcEAAAOF0ftH+LC+zGoA7BoBt/qIl7gRoXgugdfeLZrIPQLUAoOnaV/Nw+H48PEWhkLnZ2eXk5NhKxEJbYcpXff5nwl/AV/1s+X48/Pf14L7iJIEyXYFHBPjgwsz0TKUcz5IJhGLc5o9H/LcL//wd0yLESWK5WCoU41EScY5EmozzMqUiiUKSKcUl0v9k4t8s+wM+3zUAsGo+AXuRLahdYwP2SycQWHTA4vcAAPK7b8HUKAgDgGiD4c93/+8//UegJQCAZkmScQAAXkQkLlTKsz/HCAAARKCBKrBBG/TBGCzABhzBBdzBC/xgNoRCJMTCQhBCCmSAHHJgKayCQiiGzbAdKmAv1EAdNMBRaIaTcA4uwlW4Dj1wD/phCJ7BKLyBCQRByAgTYSHaiAFiilgjjggXmYX4IcFIBBKLJCDJiBRRIkuRNUgxUopUIFVIHfI9cgI5h1xGupE7yAAygvyGvEcxlIGyUT3UDLVDuag3GoRGogvQZHQxmo8WoJvQcrQaPYw2oefQq2gP2o8+Q8cwwOgYBzPEbDAuxsNCsTgsCZNjy7EirAyrxhqwVqwDu4n1Y8+xdwQSgUXACTYEd0IgYR5BSFhMWE7YSKggHCQ0EdoJNwkDhFHCJyKTqEu0JroR+cQYYjIxh1hILCPWEo8TLxB7iEPENyQSiUMyJ7mQAkmxpFTSEtJG0m5SI+ksqZs0SBojk8naZGuyBzmULCAryIXkneTD5DPkG+Qh8lsKnWJAcaT4U+IoUspqShnlEOU05QZlmDJBVaOaUt2ooVQRNY9aQq2htlKvUYeoEzR1mjnNgxZJS6WtopXTGmgXaPdpr+h0uhHdlR5Ol9BX0svpR+iX6AP0dwwNhhWDx4hnKBmbGAcYZxl3GK+YTKYZ04sZx1QwNzHrmOeZD5lvVVgqtip8FZHKCpVKlSaVGyovVKmqpqreqgtV81XLVI+pXlN9rkZVM1PjqQnUlqtVqp1Q61MbU2epO6iHqmeob1Q/pH5Z/YkGWcNMw09DpFGgsV/jvMYgC2MZs3gsIWsNq4Z1gTXEJrHN2Xx2KruY/R27iz2qqaE5QzNKM1ezUvOUZj8H45hx+Jx0TgnnKKeX836K3hTvKeIpG6Y0TLkxZVxrqpaXllirSKtRq0frvTau7aedpr1Fu1n7gQ5Bx0onXCdHZ4/OBZ3nU9lT3acKpxZNPTr1ri6qa6UbobtEd79up+6Ynr5egJ5Mb6feeb3n+hx9L/1U/W36p/VHDFgGswwkBtsMzhg8xTVxbzwdL8fb8VFDXcNAQ6VhlWGX4YSRudE8o9VGjUYPjGnGXOMk423GbcajJgYmISZLTepN7ppSTbmmKaY7TDtMx83MzaLN1pk1mz0x1zLnm+eb15vft2BaeFostqi2uGVJsuRaplnutrxuhVo5WaVYVVpds0atna0l1rutu6cRp7lOk06rntZnw7Dxtsm2qbcZsOXYBtuutm22fWFnYhdnt8Wuw+6TvZN9un2N/T0HDYfZDqsdWh1+c7RyFDpWOt6azpzuP33F9JbpL2dYzxDP2DPjthPLKcRpnVOb00dnF2e5c4PziIuJS4LLLpc+Lpsbxt3IveRKdPVxXeF60vWdm7Obwu2o26/uNu5p7ofcn8w0nymeWTNz0MPIQ+BR5dE/C5+VMGvfrH5PQ0+BZ7XnIy9jL5FXrdewt6V3qvdh7xc+9j5yn+M+4zw33jLeWV/MN8C3yLfLT8Nvnl+F30N/I/9k/3r/0QCngCUBZwOJgUGBWwL7+Hp8Ib+OPzrbZfay2e1BjKC5QRVBj4KtguXBrSFoyOyQrSH355jOkc5pDoVQfujW0Adh5mGLw34MJ4WHhVeGP45wiFga0TGXNXfR3ENz30T6RJZE3ptnMU85ry1KNSo+qi5qPNo3ujS6P8YuZlnM1VidWElsSxw5LiquNm5svt/87fOH4p3iC+N7F5gvyF1weaHOwvSFpxapLhIsOpZATIhOOJTwQRAqqBaMJfITdyWOCnnCHcJnIi/RNtGI2ENcKh5O8kgqTXqS7JG8NXkkxTOlLOW5hCepkLxMDUzdmzqeFpp2IG0yPTq9MYOSkZBxQqohTZO2Z+pn5mZ2y6xlhbL+xW6Lty8elQfJa7OQrAVZLQq2QqboVFoo1yoHsmdlV2a/zYnKOZarnivN7cyzytuQN5zvn//tEsIS4ZK2pYZLVy0dWOa9rGo5sjxxedsK4xUFK4ZWBqw8uIq2Km3VT6vtV5eufr0mek1rgV7ByoLBtQFr6wtVCuWFfevc1+1dT1gvWd+1YfqGnRs+FYmKrhTbF5cVf9go3HjlG4dvyr+Z3JS0qavEuWTPZtJm6ebeLZ5bDpaql+aXDm4N2dq0Dd9WtO319kXbL5fNKNu7g7ZDuaO/PLi8ZafJzs07P1SkVPRU+lQ27tLdtWHX+G7R7ht7vPY07NXbW7z3/T7JvttVAVVN1WbVZftJ+7P3P66Jqun4lvttXa1ObXHtxwPSA/0HIw6217nU1R3SPVRSj9Yr60cOxx++/p3vdy0NNg1VjZzG4iNwRHnk6fcJ3/ceDTradox7rOEH0x92HWcdL2pCmvKaRptTmvtbYlu6T8w+0dbq3nr8R9sfD5w0PFl5SvNUyWna6YLTk2fyz4ydlZ19fi753GDborZ752PO32oPb++6EHTh0kX/i+c7vDvOXPK4dPKy2+UTV7hXmq86X23qdOo8/pPTT8e7nLuarrlca7nuer21e2b36RueN87d9L158Rb/1tWeOT3dvfN6b/fF9/XfFt1+cif9zsu72Xcn7q28T7xf9EDtQdlD3YfVP1v+3Njv3H9qwHeg89HcR/cGhYPP/pH1jw9DBY+Zj8uGDYbrnjg+OTniP3L96fynQ89kzyaeF/6i/suuFxYvfvjV69fO0ZjRoZfyl5O/bXyl/erA6xmv28bCxh6+yXgzMV70VvvtwXfcdx3vo98PT+R8IH8o/2j5sfVT0Kf7kxmTk/8EA5jz/GMzLdsAAAAgY0hSTQAAeiUAAICDAAD5/wAAgOkAAHUwAADqYAAAOpgAABdvkl/FRgAAADdJREFUeNpMi7ENADAIwwz/H8TEhMQz/JAupWqmyHGI7JoZIrscxMbBbhX+MMbTpG+x/UgCgzMA+a4RzyZ2yIIAAAAASUVORK5CYII='); +} + +.profvis-splitbar-horizontal { + cursor: row-resize; + top: 400px; + height: 8px; + left: 0px; + right: 0px; + border-width: 0px 0; + background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAADCAYAAABfwxXFAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrIsAHvgABeNMLCADATZvAMByH/w/qQplcAYCEAcB0kThLCIAUAEB6jkKmAEBGAYCdmCZTAKAEAGDLY2LjAFAtAGAnf+bTAICd+Jl7AQBblCEVAaCRACATZYhEAGg7AKzPVopFAFgwABRmS8Q5ANgtADBJV2ZIALC3AMDOEAuyAAgMADBRiIUpAAR7AGDIIyN4AISZABRG8lc88SuuEOcqAAB4mbI8uSQ5RYFbCC1xB1dXLh4ozkkXKxQ2YQJhmkAuwnmZGTKBNA/g88wAAKCRFRHgg/P9eM4Ors7ONo62Dl8t6r8G/yJiYuP+5c+rcEAAAOF0ftH+LC+zGoA7BoBt/qIl7gRoXgugdfeLZrIPQLUAoOnaV/Nw+H48PEWhkLnZ2eXk5NhKxEJbYcpXff5nwl/AV/1s+X48/Pf14L7iJIEyXYFHBPjgwsz0TKUcz5IJhGLc5o9H/LcL//wd0yLESWK5WCoU41EScY5EmozzMqUiiUKSKcUl0v9k4t8s+wM+3zUAsGo+AXuRLahdYwP2SycQWHTA4vcAAPK7b8HUKAgDgGiD4c93/+8//UegJQCAZkmScQAAXkQkLlTKsz/HCAAARKCBKrBBG/TBGCzABhzBBdzBC/xgNoRCJMTCQhBCCmSAHHJgKayCQiiGzbAdKmAv1EAdNMBRaIaTcA4uwlW4Dj1wD/phCJ7BKLyBCQRByAgTYSHaiAFiilgjjggXmYX4IcFIBBKLJCDJiBRRIkuRNUgxUopUIFVIHfI9cgI5h1xGupE7yAAygvyGvEcxlIGyUT3UDLVDuag3GoRGogvQZHQxmo8WoJvQcrQaPYw2oefQq2gP2o8+Q8cwwOgYBzPEbDAuxsNCsTgsCZNjy7EirAyrxhqwVqwDu4n1Y8+xdwQSgUXACTYEd0IgYR5BSFhMWE7YSKggHCQ0EdoJNwkDhFHCJyKTqEu0JroR+cQYYjIxh1hILCPWEo8TLxB7iEPENyQSiUMyJ7mQAkmxpFTSEtJG0m5SI+ksqZs0SBojk8naZGuyBzmULCAryIXkneTD5DPkG+Qh8lsKnWJAcaT4U+IoUspqShnlEOU05QZlmDJBVaOaUt2ooVQRNY9aQq2htlKvUYeoEzR1mjnNgxZJS6WtopXTGmgXaPdpr+h0uhHdlR5Ol9BX0svpR+iX6AP0dwwNhhWDx4hnKBmbGAcYZxl3GK+YTKYZ04sZx1QwNzHrmOeZD5lvVVgqtip8FZHKCpVKlSaVGyovVKmqpqreqgtV81XLVI+pXlN9rkZVM1PjqQnUlqtVqp1Q61MbU2epO6iHqmeob1Q/pH5Z/YkGWcNMw09DpFGgsV/jvMYgC2MZs3gsIWsNq4Z1gTXEJrHN2Xx2KruY/R27iz2qqaE5QzNKM1ezUvOUZj8H45hx+Jx0TgnnKKeX836K3hTvKeIpG6Y0TLkxZVxrqpaXllirSKtRq0frvTau7aedpr1Fu1n7gQ5Bx0onXCdHZ4/OBZ3nU9lT3acKpxZNPTr1ri6qa6UbobtEd79up+6Ynr5egJ5Mb6feeb3n+hx9L/1U/W36p/VHDFgGswwkBtsMzhg8xTVxbzwdL8fb8VFDXcNAQ6VhlWGX4YSRudE8o9VGjUYPjGnGXOMk423GbcajJgYmISZLTepN7ppSTbmmKaY7TDtMx83MzaLN1pk1mz0x1zLnm+eb15vft2BaeFostqi2uGVJsuRaplnutrxuhVo5WaVYVVpds0atna0l1rutu6cRp7lOk06rntZnw7Dxtsm2qbcZsOXYBtuutm22fWFnYhdnt8Wuw+6TvZN9un2N/T0HDYfZDqsdWh1+c7RyFDpWOt6azpzuP33F9JbpL2dYzxDP2DPjthPLKcRpnVOb00dnF2e5c4PziIuJS4LLLpc+Lpsbxt3IveRKdPVxXeF60vWdm7Obwu2o26/uNu5p7ofcn8w0nymeWTNz0MPIQ+BR5dE/C5+VMGvfrH5PQ0+BZ7XnIy9jL5FXrdewt6V3qvdh7xc+9j5yn+M+4zw33jLeWV/MN8C3yLfLT8Nvnl+F30N/I/9k/3r/0QCngCUBZwOJgUGBWwL7+Hp8Ib+OPzrbZfay2e1BjKC5QRVBj4KtguXBrSFoyOyQrSH355jOkc5pDoVQfujW0Adh5mGLw34MJ4WHhVeGP45wiFga0TGXNXfR3ENz30T6RJZE3ptnMU85ry1KNSo+qi5qPNo3ujS6P8YuZlnM1VidWElsSxw5LiquNm5svt/87fOH4p3iC+N7F5gvyF1weaHOwvSFpxapLhIsOpZATIhOOJTwQRAqqBaMJfITdyWOCnnCHcJnIi/RNtGI2ENcKh5O8kgqTXqS7JG8NXkkxTOlLOW5hCepkLxMDUzdmzqeFpp2IG0yPTq9MYOSkZBxQqohTZO2Z+pn5mZ2y6xlhbL+xW6Lty8elQfJa7OQrAVZLQq2QqboVFoo1yoHsmdlV2a/zYnKOZarnivN7cyzytuQN5zvn//tEsIS4ZK2pYZLVy0dWOa9rGo5sjxxedsK4xUFK4ZWBqw8uIq2Km3VT6vtV5eufr0mek1rgV7ByoLBtQFr6wtVCuWFfevc1+1dT1gvWd+1YfqGnRs+FYmKrhTbF5cVf9go3HjlG4dvyr+Z3JS0qavEuWTPZtJm6ebeLZ5bDpaql+aXDm4N2dq0Dd9WtO319kXbL5fNKNu7g7ZDuaO/PLi8ZafJzs07P1SkVPRU+lQ27tLdtWHX+G7R7ht7vPY07NXbW7z3/T7JvttVAVVN1WbVZftJ+7P3P66Jqun4lvttXa1ObXHtxwPSA/0HIw6217nU1R3SPVRSj9Yr60cOxx++/p3vdy0NNg1VjZzG4iNwRHnk6fcJ3/ceDTradox7rOEH0x92HWcdL2pCmvKaRptTmvtbYlu6T8w+0dbq3nr8R9sfD5w0PFl5SvNUyWna6YLTk2fyz4ydlZ19fi753GDborZ752PO32oPb++6EHTh0kX/i+c7vDvOXPK4dPKy2+UTV7hXmq86X23qdOo8/pPTT8e7nLuarrlca7nuer21e2b36RueN87d9L158Rb/1tWeOT3dvfN6b/fF9/XfFt1+cif9zsu72Xcn7q28T7xf9EDtQdlD3YfVP1v+3Njv3H9qwHeg89HcR/cGhYPP/pH1jw9DBY+Zj8uGDYbrnjg+OTniP3L96fynQ89kzyaeF/6i/suuFxYvfvjV69fO0ZjRoZfyl5O/bXyl/erA6xmv28bCxh6+yXgzMV70VvvtwXfcdx3vo98PT+R8IH8o/2j5sfVT0Kf7kxmTk/8EA5jz/GMzLdsAAAAgY0hSTQAAeiUAAICDAAD5/wAAgOkAAHUwAADqYAAAOpgAABdvkl/FRgAAADZJREFUeNpi3LHv+BEGBgYGBob/EIqBkQEGGN+8ecOADfz//5+B5cylW0cgqjF1AgAAAP//AwCtfA4GqIXcdgAAAABJRU5ErkJggg=='); +} + +.profvis-status-bar { + position: absolute; + padding: 0px 0px; + top: 0; + left: 0; + right: 0; + height: 22px; + line-height: 18px; + border-bottom: 1px solid rgb(196, 201, 204); + background-color: rgb(248, 249, 248); + color: #444; + font-family: "Lucida Sans", "DejaVu Sans", "Lucida Grande", "Segoe UI", Verdana, Helvetica, sans-serif; + font-size: 11px; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + overflow: hidden; +} + +.profvis-footer { + position: absolute; + padding: 0px 0px; + bottom: 0px; + left: 0; + right: 0; + height: 19px; + line-height: 18px; + border-top: 1px solid rgb(196, 201, 204); + background-color: rgb(248, 249, 248); + color: #444; + font-family: "Lucida Sans", "DejaVu Sans", "Lucida Grande", "Segoe UI", Verdana, Helvetica, sans-serif; + font-size: 11px; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + overflow: hidden; +} + +.profvis-status-bar .info-block { + display: inline-block; + vertical-align: top; + width: 140px; + padding: 2px 11px 2px 11px; +} + +.profvis-footer .info-block { + display: inline-block; + vertical-align: top; + padding: 1px 11px 1px 11px; +} + +.profvis-footer .info-block-right { + display: inline-block; + vertical-align: top; + padding: 1px 11px 1px 11px; + float: right; +} + +.profvis-status-bar .result-block { + width: auto; + cursor: pointer; +} + +.profvis-status-bar .result-block-active { + width: auto; + background: rgb(227, 229, 230); +} + +.profvis-status-bar .spacing-block { + display: inline-block; + width: 25px; +} + +.profvis-status-bar .separator-block { + display: inline-block; + width: 1px; + text-align: center; +} + +.profvis-status-bar .separator-block .separator-image { + width: 2px; + height: 26px; + margin-top: -2px; +} + +.profvis-status-bar .options-button { + float: right; + color: #444; + text-decoration: none; + cursor: pointer; + padding: 2px 11px 2px 11px; +} + + +.profvis-options-panel { + float: left; + position: absolute; + right: 0; + top: 21px; + padding: 3px 6px; + border: 1px solid #999; + background-color: #fff; + font-family: "Lucida Sans", "DejaVu Sans", "Lucida Grande", "Segoe UI", Verdana, Helvetica, sans-serif; + font-size: 11px; + line-height: 170%; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + cursor: pointer; +} + +.profvis-code { + position: absolute; + top: 0px; + left: 0; + bottom: 0; + right: 0; + overflow-y: auto; + border: 0px solid #ddd; +} + +.profvis-flamegraph { + position: absolute; + right: 0; + top: 0; + bottom: 0; + left: 0; + overflow: hidden; + border: 0px solid #ddd; + background: rgb(249, 249, 250); +} + +table.profvis-table { + border-collapse: collapse; + font-family: "Lucida Sans", "DejaVu Sans", "Lucida Grande", "Segoe UI", Verdana, Helvetica, sans-serif; + font-size: 11px; + width: 100%; + text-align: left; + cursor: default; +} + +table.profvis-table th { + background-color: rgb(249, 249, 250); + width: 14%; +} + +table.profvis-table th.spacing { + width: 20px; +} + +table.profvis-table th.filename { + font-family: monospace; + padding-left: 10px; + width: auto; +} + +table.profvis-table th.percent { + text-align: center; +} + +table.profvis-table tr { + border-top: 1px solid transparent; + border-bottom: 1px solid transparent; + vertical-align: top; +} + +/* Need to use td for locked cells, so that the border color overrides the + border color from the row above. */ +table.profvis-table tr.locked > td { + border-top: 1px solid #444; + border-bottom: 1px solid #444; +} + +table.profvis-table tr > td { + padding-top: 0; + padding-bottom: 0; +} + +table.profvis-table tr.active { + background-color: #fdb; +} + +table.profvis-table .linenum { + color: #aaa; + padding: 0 15px; + font-family: monospace; + width: 25px; +} + +/* For unselectable and uncopyable text elements in table */ +table.profvis-table [data-pseudo-content]::before { + content: attr(data-pseudo-content); +} + +table.profvis-table .code { + white-space: pre-wrap; + margin: 0; + min-height: 1.25em; + line-height: 1.25; + width: auto; + font-family: monospace; + background: transparent; +} + + +table.profvis-table .time, table.profvis-table .percent { + padding: 0 5px; + text-align: right; + + min-width: 2em; + max-width: 2em; + overflow: hidden; + + padding-right: 10px; +} + +table.profvis-table .memory { + padding-right: 5px; +} + +table.profvis-table th.time { + text-align: center; +} + +table.profvis-table .memory { + padding: 0 5px; + text-align: right; + + min-width: 2em; + max-width: 4em; + overflow: hidden; + + padding-right: 10px; +} + +table.profvis-table .memory-right { + text-align: left; +} + +table.profvis-table th.memory { + text-align: center; +} + +table.profvis-table .timebar-cell { + padding-left: 0; + border-left: 1px solid #444; + min-width: 3em; + width: 3em; +} + +table.profvis-table .timebar-cell > .timebar { + background-color: #5A5A5A; + border-radius: 0px 2px 2px 0px; + line-height: 15px; +} + +table.profvis-table .membar-left-cell { + padding-left: 0; + padding-right: 0; + border-left: 0px solid black; + min-width: 0.5em; + width: 0.5em; +} + +table.profvis-table .membar-left-cell > .membar { + background-color: #A7A7A7; + float: right; + border-radius: 2px 0px 0px 2px; + line-height: 15px; +} + +table.profvis-table .membar-right-cell { + padding-left: 0; + border-left: 1px solid black; + min-width: 1em; + width: 1em; +} + +table.profvis-table .membar-right-cell > .membar { + background-color: #5A5A5A; + border-radius: 0px 2px 2px 0px; + line-height: 15px; +} + +.profvis-flamegraph .background { + fill: rgb(249, 249, 250); +} + +.profvis-flamegraph .cell .rect { + stroke: #000; + stroke-width: 0.25px; + fill: #fff; +} + +.profvis-flamegraph .cell.active .rect { + stroke-width: 0.75px; + fill: #ddd; +} + +.profvis-flamegraph .cell.highlighted .rect { + fill: #ffc; +} + +.profvis-flamegraph .cell.highlighted.active .rect { + fill: #fdb; +} + +.profvis-flamegraph .cell.output .rect { + fill: #eef; +} + +.profvis-flamegraph .cell.gc .rect { + fill: #ccc; +} + +.profvis-flamegraph .cell.stacktrace .rect { + fill: #eee; +} +.profvis-flamegraph .cell.stacktrace .profvis-label { + fill: #666; +} + +.profvis-flamegraph .cell.locked { + font-weight: bold; +} + +.profvis-flamegraph .cell.locked .rect { + stroke-width: 2px; +} + +.profvis-flamegraph .cell .profvis-label { + font-family: monospace; + font-size: 11px; + cursor: default; +} + + +.profvis-flamegraph .axis text { + font: 10px sans-serif; +} + +.profvis-flamegraph .axis path, +.profvis-flamegraph .axis line { + fill: none; + stroke: #000; + shape-rendering: crispEdges; +} + + +.profvis-flamegraph .profvis-tooltip rect { + fill: rgb(249, 249, 250); + stroke: #000; + opacity: 0.75; + stroke-opacity: 0.75; + stroke-width: 0.5; +} + +.profvis-flamegraph .profvis-tooltip text { + text-anchor: middle; + font-family: monospace; + font-size: 11px; +} + +.profvis-infobox { + position: absolute; + left: 8px; + top: 8px; + opacity: 0.8; + border-radius: 3px; + color: #f8f8f8; + background-color: #333; + padding: 5px 10px; + font-family: "Lucida Sans", "DejaVu Sans", "Lucida Grande", "Segoe UI", Verdana, Helvetica, sans-serif; + font-size: 11px; + line-height: 100%; + pointer-events: none; + min-width: 280px; +} + +.profvis-infobox table { + border-collapse: separate; + border-spacing: 2px; +} + + +.profvis-infobox table td { + padding: 1px; +} + +.profvis-infobox .infobox-title { + font-weight: bold; +} + +.profvis-message { + width: 100%; + height: 100%; + display: -ms-flexbox; + display: -webkit-flex; + display: flex; + -ms-flex-pack: center; + -webkit-justify-content: center; + justify-content: center; + -ms-flex-align: center; + -webkit-align-items: center; + align-items: center; + font-family: "Lucida Sans", "DejaVu Sans", "Lucida Grande", "Segoe UI", Verdana, Helvetica, sans-serif; + font-size: 11px; +} + +.profvis-message div { + color: #444; + height: 25%; +} + +.profvis-treetable { + top: 0px; + bottom: 0px; + left: 0px; + right: 0px; + position: absolute; + margin-top: 23px; + margin-bottom: 21px; + margin-right: 0px; + overflow: hidden; + font: 10px sans-serif; + color: #161616; + font-family: "Lucida Sans", "DejaVu Sans", "Lucida Grande", "Segoe UI", Verdana, Helvetica, sans-serif; + font-size: 11px; + overflow-y: auto; +} + +.profvis-treetable .results { + width: 100%; + table-layout: fixed; +} + +.profvis-treetable th { + font-weight: normal; + height: 18px; + background-color: #F8F9F8; + border-bottom: solid 1px #E4E4E4; + border-right: solid 1px #E4E4E4; + padding-left: 3px; + padding-right: 3px; +} + +.profvis-treetable td { + height: 18px; + border-bottom: solid 1px #E4E4E4; + border-right: solid 1px #E4E4E4; + padding-left: 3px; + padding-right: 3px; + overflow: hidden; + text-overflow: ellipsis; +} + +.profvis-treetable .action { + width: 18px; +} + +.profvis-treetable .memory { + width: 100px; + text-align: right; +} + +.profvis-treetable th { + cursor: default; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + overflow: hidden; +} + +.profvis-treetable th.memory { + text-align: center; +} + +.profvis-treetable .time { + width: 100px; + text-align: right; +} + +.profvis-treetable th.time { + text-align: center; +} + +.profvis-treetable .count { + text-align: right; +} + +.profvis-treetable th.count { + text-align: center; +} + +.profvis-treetable th.code-label { + text-align: left; + padding-left: 5px; +} + +.profvis-treetable td.label-pointer { + cursor: pointer; +} + +.profvis-treetable .label-text { + display: inline-block; + -webkit-user-select: none; + -khtml-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; +} + +.profvis-treetable .path { + text-align: left; +} + +.profvis-treetable .treetable-expand > div { + display: inline-block; + width: 15px; +} + +.profvis-treetable .treetable-expand > div > div{ + width: 0px; + height: 0px; + margin-left: 3px; + margin-right: 6px; + border-top: 4px solid transparent; + border-left: 6px solid #161616; + border-bottom: 4px solid transparent; +} + +.profvis-treetable .treetable-collapse > div { + display: inline-block; + width: 15px; +} + +.profvis-treetable .treetable-collapse > div > div { + width: 0px; + height: 0px; + margin-left: 3px; + margin-right: 6px; + border-right: 4px solid transparent; + border-top: 6px solid #161616; + border-left: 4px solid transparent; + display: inline-block; +} + +.profvis-treetable .time-info { + padding: 0px; + padding-right: 2px; + text-align: right; +} + +.profvis-treetable .timebar { + width: 0px; + height: 14px; + background-color: #5A5A5A; + float: left; + border-radius: 2px 2px 2px 2px; + padding-top: 0px; + margin-top: 1px; +} + +.profvis-treetable .timecell { + padding-top: 2px; + padding-right: 3px; +} + +.profvis-treetable .memory-info { + padding-left: 0px; + padding-right: 2px; + text-align: right; +} + +.profvis-treetable .memory-info-right { + padding-left: 2px; + padding-right: 0px; + text-align: left; +} + +.profvis-treetable .memory-leftbar-wrapper { + float: left; + height: 14px; + width: 5px; +} + +.profvis-treetable .memory-leftbar { + width: 2px; + height: 14px; + background-color: #A7A7A7; + float: right; + border-radius: 1px 0px 0px 1px; +} + +.profvis-treetable .memory-rightbar { + width: 2px; + float: left; + height: 14px; + background-color: #5A5A5A; + border-radius: 0px 1px 1px 0px; +} + +.profvis-treetable .memory-cell { +} + +.profvis-treetable .memory-bar-container { + margin-right: 3px; +} + +.profvis-treetable .time-bar-container { + margin-right: 3px; +} diff --git a/docs/articles/Introduction_files/profvis-0.3.6.9000/profvis.js b/docs/articles/Introduction_files/profvis-0.3.6.9000/profvis.js new file mode 100644 index 0000000..af3e3a3 --- /dev/null +++ b/docs/articles/Introduction_files/profvis-0.3.6.9000/profvis.js @@ -0,0 +1,2656 @@ +/*jshint + undef:true, + browser:true, + devel: true, + jquery:true, + strict:false, + curly:false, + indent:2 +*/ +/*global profvis:true, d3, hljs */ + +profvis = (function() { + var profvis = {}; + + profvis.render = function(el, message) { + + function generateStatusBarButton(id, caption, active) { + var spacerImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAAUCAYAAACnOeyiAAAAXklEQVR42mNgAIL///8zMYSGhjIDGYIMIiIMvECGMwMDN4M4kFEDUqIIZKwDMdSBjAsghj6Q8QPEMAAy/lOBoQekv4AYKkDGfgZeXl4RICOLQUtLiw3IUAJJMQIZ7AC2tU2tXJxOYgAAAABJRU5ErkJggg=='; + + var buttonHtml = + '
' + caption + '
' + + '
'; + + return buttonHtml; + } + + function generateStatusBar(el, onToogle) { + var $el = $(el); + + el.innerHTML = + generateStatusBarButton('flameGraphButton', 'Flame Graph', true) + + generateStatusBarButton('treetableButton', 'Data', false) + + 'Options ▾'; + + $el.find("span.options-button").on("click", function(e) { + e.preventDefault(); + e.stopPropagation(); + + vis.optionsPanel.toggleVisibility(); + }); + + var setStatusBarButtons = function(e) { + $(".info-block").removeClass("result-block-active"); + $(".info-block").addClass("result-block"); + e.addClass("result-block-active"); + }; + + $el.find("#flameGraphButton").on("click", function() { + setStatusBarButtons($(this)); + onToogle("flamegraph"); + }); + + $el.find("#treetableButton").on("click", function() { + setStatusBarButtons($(this)); + onToogle("treetable"); + }); + + return { + el: el + }; + } + + function generateFooter(el, onToogle) { + var $el = $(el); + + el.innerHTML = + '
Sample Interval: ' + + vis.interval + 'ms
' + + '
' + + // '' + (Math.round(vis.totalMem * 100) / 100) + 'MB' + + // ' / ' + + '' + vis.totalTime + 'ms' + + '
'; + + return { + el: el + }; + } + + function generateOptionsPanel(el, onOptionsChange) { + var $el = $(el); + + el.innerHTML = + '
' + + '' + + (vis.splitDir === "h" ? '☒' : '☐') + + ' Split horizontally' + + '
' + + '
' + + ' Hide internal function calls' + + '
' + + '
' + + ' Hide lines of code with zero time' + + '
' + + '
' + + ' Hide memory results' + + '
'; + + // Toggle the appearance of a checkbox and return the new checked state. + function toggleCheckbox($checkbox) { + // Use attr() instead of data(), because the latter tries to coerce to + // numbers, which complicates our comparisons. + var checked = $checkbox.attr("data-checked"); + + if (checked === "0") { + $checkbox.attr("data-checked", "1"); + $checkbox.html("☒"); + return true; + + } else { + $checkbox.attr("data-checked", "0"); + $checkbox.html("☐"); + return false; + } + } + + $el.find(".split-horizontal") + .on("click", function() { + var checked = toggleCheckbox($(this).find(".options-checkbox")); + onOptionsChange("split", checked); + }); + + $el.find(".hide-internal") + .on("click", function() { + var checked = toggleCheckbox($(this).find(".options-checkbox")); + onOptionsChange("internals", checked); + }); + + $el.find(".hide-memory") + .on("click", function() { + var checked = toggleCheckbox($(this).find(".options-checkbox")); + onOptionsChange("memory", checked); + }); + + // Make the "hide internal" option available or unavailable to users + function enableHideInternal() { + $el.find(".hide-internal").css("display", ""); + } + function disableHideInternal() { + $el.find(".hide-internal").css("display", "none"); + } + // By default, start with it unavailable; it's only relevant for Shiny + // apps. + disableHideInternal(); + + + $el.find(".hide-zero-row") + .on("click", function() { + var checked = toggleCheckbox($(this).find(".options-checkbox")); + + if (checked) { + vis.codeTable.hideZeroTimeRows(); + } else { + vis.codeTable.showZeroTimeRows(); + } + }); + + el.style.visibility = "hidden"; + function toggleVisibility(offset) { + if (el.style.visibility === "visible") { + el.style.visibility = "hidden"; + } else { + el.style.visibility = "visible"; + $(document).on("click", hideOnClickOutside); + } + } + + // Hide the panel when a click happens outside. This handler also removes + // itself after it fires. + function hideOnClickOutside(e) { + var $el = $(el); + if (!$el.is(e.target) && $el.has(e.target).length === 0) { + el.style.visibility = "hidden"; + // Unregister this event listener + $(document).off("click", hideOnClickOutside); + } + } + + return { + el: el, + toggleVisibility: toggleVisibility, + enableHideInternal: enableHideInternal, + disableHideInternal: disableHideInternal + }; + } + + function notifySourceFileMessage(d, details) { + if (window.parent.postMessage) { + window.parent.postMessage({ + source: "profvis", + message: "sourcefile", + file: d.filename, + normpath: d.normpath ? d.normpath : getNormPath(vis.files, d.filename), + line: d.linenum, + details: details + }, window.location.origin); + } + } + + function roundOneDecimalNum(number, decimals) { + return Math.round(number * 10) / 10; + } + + function roundOneDecimal(number, decimals) { + if (!number) return 0; + return roundOneDecimalNum(number).toFixed(1); + } + + // Generate the code table ---------------------------------------- + function generateCodeTable(el) { + var useMemory = false; + var content = d3.select(el); + + if (vis.fileLineStats.length === 0) { + content.append("div") + .attr("class", "profvis-message") + .append("div") + .text("(Sources not available)"); + } + + // One table for each file + var tables = content.selectAll("table") + .data(vis.fileLineStats) + .enter() + .append("table") + .attr("class", "profvis-table"); + + // Table headers + var headerRows = tables.append("tr"); + headerRows.append("th") + .attr("colspan", "2") + .attr("class", "filename") + .text(function(d) { return d.filename; }); + + var percentTooltip = "Percentage of tracked execution time"; + var percentMemTooltip = "Percentage of peak memory deallocation and allocation"; + + headerRows.append("th") + .attr("class", "table-memory memory") + .attr("colspan", "4") + .text("Memory"); + + headerRows.append("th") + .attr("class", "time") + .attr("colspan", "2") + .text("Time"); + + headerRows.append("th") + .attr("class", "spacing") + .attr("data-pseudo-content", "\u00a0"); + + // Insert each line of code + var rows = tables.selectAll("tr.code-row") + .data(function(d) { return d.lineData; }) + .enter() + .append("tr") + .attr("class", "code-row"); + + // Use pseudo-content and CSS content rule to make text unselectable and + // uncopyable. See https://danoc.me/blog/css-prevent-copy/ + rows.append("td") + .attr("class", "linenum") + .attr("data-pseudo-content", function(d) { return d.linenum; }); + + rows.append("td") + .attr("class", "code r") + .text(function(d) { return d.content; }) + .each(function() { hljs.highlightBlock(this); }); + + rows.append("td") + .attr("class", "table-memory memory") + .attr("title", "Memory deallocation (MB)") + .attr("data-pseudo-content", + function(d) { return roundOneDecimalNum(d.sumMemDealloc) !== 0 ? roundOneDecimal(d.sumMemDealloc) : ""; }); + + rows.append("td") + .attr("class", "table-memory membar-left-cell") + .append("div") + .attr("class", "membar") + .attr("title", percentMemTooltip) + .style("width", function(d) { + var p = Math.min(Math.abs(Math.min(Math.round(d.propMemDealloc * 100), 0)), 100); + + // 8% is the minimal size that looks visually appealing while drawing an almost empty bar + p = roundOneDecimalNum(d.sumMemDealloc) !== 0 ? Math.max(p, 8) : 0; + return p + "%"; + }) + // Add the equivalent of   to be added with CSS content + .attr("data-pseudo-content", "\u00a0"); + + rows.append("td") + .attr("class", "table-memory membar-right-cell") + .append("div") + .attr("class", "membar") + .attr("title", percentMemTooltip) + .style("width", function(d) { + var p = Math.min(Math.max(Math.round(d.propMemAlloc * 100), 0), 100); + + // 4% is the minimal size that looks visually appealing while drawing an almost empty bar + p = roundOneDecimalNum(d.sumMemAlloc) !== 0 ? Math.max(p, 4) : 0; + return p + "%"; + }) + // Add the equivalent of   to be added with CSS content + .attr("data-pseudo-content", "\u00a0"); + + rows.append("td") + .attr("class", "table-memory memory memory-right") + .attr("title", "Memory allocation (MB)") + .attr("data-pseudo-content", + function(d) { return roundOneDecimalNum(d.sumMemAlloc) !== 0 ? roundOneDecimal(d.sumMemAlloc) : ""; }); + + rows.append("td") + .attr("class", "time") + .attr("title", "Total time (ms)") + .attr("data-pseudo-content", + function(d) { return Math.round(d.sumTime * 100) !== 0 ? (Math.round(d.sumTime * 100) / 100) : ""; }); + + rows.append("td") + .attr("class", "timebar-cell") + .append("div") + .attr("class", "timebar") + .attr("title", percentTooltip) + .style("width", function(d) { + return Math.round(d.propTime * 100) + "%"; + }) + // Add the equivalent of   to be added with CSS content + .attr("data-pseudo-content", "\u00a0"); + + rows.append("td") + .attr("class", "spacing") + .attr("data-pseudo-content", "\u00a0"); + + rows + .on("click", function(d) { + // Info box is only relevant when mousing over flamegraph + vis.infoBox.hide(); + highlighter.click(d); + notifySourceFileMessage(d, "select"); + }) + .on("mouseover", function(d) { + if (highlighter.isLocked()) return; + + // Info box is only relevant when mousing over flamegraph + vis.infoBox.hide(); + highlighter.hover(d); + }) + .on("mouseout", function(d) { + if (highlighter.isLocked()) return; + + highlighter.hover(null); + }) + .on("dblclick", function(d) { + notifySourceFileMessage(d, "open"); + }); + + function hideZeroTimeRows() { + rows + .filter(function(d) { return d.sumTime === 0; }) + .style("display", "none"); + } + + function showZeroTimeRows() { + rows + .filter(function(d) { return d.sumTime === 0; }) + .style("display", ""); + } + + function addLockHighlight(d) { + var target = d; + rows + .filter(function(d) { return d === target; } ) + .classed({ locked: true }); + } + + function clearLockHighlight() { + rows + .filter(".locked") + .classed({ locked: false }); + } + + function addActiveHighlight(d) { + // If we have filename and linenum, search for cells that match, and + // set them as "active". + var target = d; + if (target.filename && target.linenum) { + var tr = rows + .filter(function(d) { + return d.linenum === target.linenum && + d.filename === target.filename; + }) + .classed({ active: true }); + + tr.node().scrollIntoViewIfNeeded(); + } + } + + function clearActiveHighlight() { + rows + .filter(".active") + .classed({ active: false }); + } + + function enableScroll() { + // TODO: implement this + } + + function disableScroll() { + } + + function useMemoryResults() { + d3.selectAll(".table-memory").style("display", vis.hideMemory ? "none" : ""); + } + + return { + el: el, + hideZeroTimeRows: hideZeroTimeRows, + showZeroTimeRows: showZeroTimeRows, + addLockHighlight: addLockHighlight, + clearLockHighlight: clearLockHighlight, + addActiveHighlight: addActiveHighlight, + clearActiveHighlight: clearActiveHighlight, + enableScroll: enableScroll, + disableScroll: disableScroll, + useMemoryResults: useMemoryResults + }; + } + + + var highlighter = (function() { + // D3 data objects for the currently locked and active items + var lockItem = null; + var activeItem = null; + + function isLocked() { + return lockItem !== null; + } + + function currentLock() { + return lockItem; + } + + function currentActive() { + return activeItem; + } + + + // This is called when a flamegraph cell or a line of code is clicked on. + // Clicks also should trigger hover events. + function click(d) { + // If d is null (background is clicked), or if locked and this click + // is on the currently locked selection, just unlock and return. + if (d === null || (lockItem && d === lockItem)) { + lockItem = null; + vis.flameGraph.clearLockHighlight(); + vis.codeTable.clearLockHighlight(); + return; + } + + // If nothing currently locked, or if locked and this click is on + // something other than the currently locked selection, then lock the + // current selection. + lockItem = d; + + vis.flameGraph.clearLockHighlight(); + vis.codeTable.clearLockHighlight(); + hover(null); + + vis.flameGraph.addLockHighlight(d); + vis.codeTable.addLockHighlight(d); + hover(d); + } + + + function hover(d) { + activeItem = d; + + if (activeItem) { + vis.flameGraph.addActiveHighlight(activeItem); + vis.codeTable.addActiveHighlight(activeItem); + return; + } + + vis.flameGraph.clearActiveHighlight(); + vis.codeTable.clearActiveHighlight(); + } + + return { + isLocked: isLocked, + currentLock: currentLock, + currentActive: currentActive, + + click: click, + hover: hover + }; + })(); + + + // Generate the flame graph ----------------------------------------------- + function generateFlameGraph(el) { + el.innerHTML = ""; + + var stackHeight = 15; // Height of each layer on the stack, in pixels + var zoomMargin = 0.02; // Extra margin on sides when zooming to fit + + // Dimensions ----------------------------------------------------------- + + // Margin inside the svg where the plotting occurs + var dims = { + margin: { top: 0, right: 0, left: 0, bottom: 30 } + }; + dims.width = el.clientWidth - dims.margin.left - dims.margin.right; + dims.height = el.clientHeight - dims.margin.top - dims.margin.bottom; + + var domains = { + x: [ + d3.min(vis.prof, function(d) { return d.startTime; }), + d3.max(vis.prof, function(d) { return d.endTime; }) + ], + y: [ + d3.min(vis.prof, function(d) { return d.depth; }) - 1, + d3.max(vis.prof, function(d) { return d.depth; }) + ] + }; + // Slightly expand x domain + domains.x = expandRange(domains.x, zoomMargin); + + // Scales --------------------------------------------------------------- + var scales = { + x: d3.scale.linear() + .domain(domains.x) + .range([0, dims.width]), + + y: d3.scale.linear() + .domain(domains.y) + .range([dims.height, dims.height - (domains.y[1] - domains.y[0]) * stackHeight]), + + // This will be a function that, given a data point, returns the depth. + // This function can change; sometimes it returns the original depth, + // and sometimes it returns the collapsed depth. This isn't exactly a + // scale function, but it's close enough for our purposes. + getDepth: null + }; + + function useCollapsedDepth() { + scales.getDepth = function(d) { return d.depthCollapsed; }; + } + function useUncollapsedDepth() { + scales.getDepth = function(d) { return d.depth; }; + } + + useCollapsedDepth(); + + // SVG container objects ------------------------------------------------ + var svg = d3.select(el).append('svg'); + + var clipRect = svg.append("clipPath") + .attr("id", "clip-" + vis.el.id) + .append("rect"); + + var container = svg.append('g') + .attr("transform", "translate(" + dims.margin.left + "," + dims.margin.top + ")") + .attr("clip-path", "url(" + urlNoHash() + "#clip-" + vis.el.id + ")"); + + // Add a background rect so we have something to grab for zooming/panning + var backgroundRect = container.append("rect") + .attr("class", "background"); + + // Axes ------------------------------------------------------------ + var xAxis = d3.svg.axis() + .scale(scales.x) + .orient("bottom"); + + svg.append("g") + .attr("class", "x axis") + .call(xAxis); + + // Container sizing ----------------------------------------------------- + // Update dimensions of various container elements, based on the overall + // dimensions of the containing div. + function updateContainerSize() { + dims.width = el.clientWidth - dims.margin.left - dims.margin.right; + dims.height = el.clientHeight - dims.margin.top - dims.margin.bottom; + + svg + .attr('width', dims.width + dims.margin.left + dims.margin.right) + .attr('height', dims.height + dims.margin.top + dims.margin.bottom); + + clipRect + .attr("x", dims.margin.left) + .attr("y", dims.margin.top) + .attr("width", dims.width) + .attr("height", dims.height); + + backgroundRect + .attr("width", dims.width) + .attr("height", dims.height); + + svg.select(".x.axis") + .attr("transform", "translate(" + dims.margin.left + "," + dims.height + ")"); + } + + + // Redrawing ------------------------------------------------------------ + + // Redrawing is a little complicated. For performance reasons, the + // flamegraph cells that are offscreen aren't rendered; they're removed + // from the D3 selection of cells. However, when transitions are + // involved, it may be necssary to add objects in their correct + // off-screen starting locations before the transition, and then do the + // transition. Similarly, it may be necssary to transition objects to + // their correct off-screen ending positions. + // + // In order to handle this, whenever there's a transition, we need to + // have the scales for before the transition, and after. When a function + // invokes a transition, it will generally do the following: (1) save the + // previous scales, (2) modify the current scales, (3) call a redraw + // function. The redraw functions are customized for different types of + // transitions, and they will use the saved previous scales to position + // objects correctly for the transition. When there's no transition, the + // previous scales aren't needed, and the redrawImmediate() function + // should be used. + + // Cache cells for faster access (avoid a d3.select()) + var cells; + + // For a data element, return identifying key + function dataKey(d) { + return d.depth + "-" + d.startTime + "-" + d.endTime; + } + + // For transitions with animation, we need to have a copy of the previous + // scales in addition to the current ones. + var prevScales = {}; + function savePrevScales() { + prevScales = { + x: scales.x.copy(), + y: scales.y.copy(), + getDepth: scales.getDepth + }; + } + savePrevScales(); + + + // Returns a D3 selection of the cells that are within the plotting + // region, using a set of scales. + function selectActiveCells(scales) { + var xScale = scales.x; + var yScale = scales.y; + var depth = scales.getDepth; + var width = dims.width; + var height = dims.height; + + var data = vis.prof.filter(function(d) { + var depthVal = depth(d); + return !(xScale(d.endTime) < 0 || + xScale(d.startTime) > width || + depthVal === null || + yScale(depthVal - 1) < 0 || + yScale(depthVal) > height); + }); + + cells = container.selectAll("g.cell").data(data, dataKey); + + return cells; + } + + // Given an enter selection, add the rect and text objects, but don't + // position them. Returns a selection of the new elements. + // This should usually be called with addItems(sel.enter()) instead + // of sel.enter().call(addItems), because the latter returns the original + // enter selection, not the selection of elements, and can't be + // used for chaining more function calls on the selection. + function addItems(enterSelection) { + var cells = enterSelection.append("g") + .attr("class", "cell") + .classed("highlighted", function(d) { return d.filename !== null; }) + .call(addMouseEventHandlers); + + // Add CSS classes for highlighting cells with labels that match particular + // regex patterns. + var highlightPatterns = d3.entries(message.highlight); + highlightPatterns.map(function(item) { + var cssClass = item.key; + var regexp = new RegExp(item.value); + + cells.classed(cssClass, function(d) { + return d.label.search(regexp) !== -1; + }); + }); + + cells.append("rect") + .attr("class", "rect"); + + cells.append("text") + .attr("class", "profvis-label") + .text(function(d) { return d.label; }); + + return cells; + } + + // Given a selection, position the rects and labels, using a set of + // scales. + function positionItems(cells, scales) { + var xScale = scales.x; + var yScale = scales.y; + var depth = scales.getDepth; + + cells.select("rect") + .attr("width", function(d) { + return xScale(d.endTime) - xScale(d.startTime); + }) + .attr("height", yScale(0) - yScale(1)) + .attr("x", function(d) { return xScale(d.startTime); }) + .attr("y", function(d) { return yScale(depth(d)); }); + + cells.select("text") + .attr("x", function(d) { + // To place the labels, check if there's enough space to fit the + // label plus padding in the rect. (We already know the label fits + // without padding if we got here.) + // * If there's not enough space, simply center the label in the + // rect. + // * If there is enough space, keep the label within the rect, with + // padding. Try to left-align, keeping the label within the + // viewing area if possible. + + // Padding on left and right + var pad = 2; + + var textWidth = getLabelWidth(this, d.label.length); + var rectWidth = xScale(d.endTime) - xScale(d.startTime); + + if (textWidth + pad*2 > rectWidth) { + return xScale(d.startTime) + (rectWidth - textWidth) / 2; + } else { + return Math.min( + Math.max(0, xScale(d.startTime)) + pad, + xScale(d.endTime) - textWidth - pad + ); + } + }) + .attr("y", function(d) { return yScale(depth(d) - 0.8); }); + + return cells; + } + + + // Redraw without a transition (regular panning and zooming) + function redrawImmediate() { + cells = selectActiveCells(scales); + + cells.exit().remove(); + addItems(cells.enter()) + .call(addLockHighlightSelection, highlighter.currentLock()) + .call(addActiveHighlightSelection, highlighter.currentActive()); + cells.call(positionItems, scales); + cells.select('text') + .call(updateLabelVisibility); + svg.select(".x.axis").call(xAxis); + } + + // Redraw for double-click zooming, where there's a transition + function redrawZoom(duration) { + // Figure out if we're zooming in or out. This will determine when we + // recalculate the label visibility: before or after the transition. + var prevExtent = prevScales.x.domain()[1] - prevScales.x.domain()[0]; + var curExtent = scales.x.domain()[1] - scales.x.domain()[0]; + var zoomIn = curExtent < prevExtent; + + cells = selectActiveCells(scales); + + // Phase 1 + // Add the enter items, highlight them, and position them using the + // previous scales + addItems(cells.enter()) + .call(addLockHighlightSelection, highlighter.currentLock()) + .call(addActiveHighlightSelection, highlighter.currentActive()) + .call(positionItems, prevScales); + + // If zooming out, update label visibility. This will hide some labels + // now, before the transition, ensuring that they will never be larger + // than the box. + if (!zoomIn) { + cells.select('text') + .call(updateLabelVisibility); + } + + // Phase 2 + // Position the update (and enter) items using the new scales + cells + .transition().duration(duration) + .call(positionItems, scales); + + // Position the exit items using the new scales + cells.exit() + .transition().duration(duration) + .call(positionItems, scales); + + // Update x axis + svg.select(".x.axis") + .transition().duration(duration) + .call(xAxis); + + // Phase 3 + // If zooming in, update label visibility. This will hide some labels + // now, after the transition, ensuring that they will never be larger + // than the box. + if (zoomIn) { + cells.select('text') + .transition().delay(duration) + .call(updateLabelVisibility); + } + + // Remove the exit items + cells.exit() + .transition().delay(duration) + .remove(); + } + + // Redraw when internal functions are hidden + function redrawCollapse(exitDuration, updateDuration) { + cells = selectActiveCells(scales); + + // There are two subsets of the exit items: + // 1. Those that exit because depth is null. These should fade out. + // 2. Those that exit because they move off screen. These should wait + // for subset 1 to fade out, then move with a transition. + var fadeOutCells = cells.exit() + .filter(function(d) { return scales.getDepth(d) === null; }); + var moveOutCells = cells.exit() + .filter(function(d) { return scales.getDepth(d) !== null; }); + + // Phase 1 + // Add the enter items, highlight them, and position them using the + // previous scales + addItems(cells.enter()) + .call(addLockHighlightSelection, highlighter.currentLock()) + .call(addActiveHighlightSelection, highlighter.currentActive()) + .call(positionItems, prevScales); + + cells.select('text') + .call(updateLabelVisibility); + + // Phase 2 + // Fade out the items that have a null depth + fadeOutCells + .transition().duration(exitDuration) + .style("opacity", 0); + + // Phase 3 + // Position the update (and enter) items using the new scales + cells + .transition().delay(exitDuration).duration(updateDuration) + .call(positionItems, scales); + + // Position the exit items that move out, using the new scales + moveOutCells + .transition().delay(exitDuration).duration(updateDuration) + .call(positionItems, scales); + + // Phase 4 + // Remove all the exit items + cells.exit() + .transition().delay(exitDuration + updateDuration) + .remove(); + } + + // Redraw when internal functions are un-hidden + function redrawUncollapse(updateDuration, enterDuration) { + cells = selectActiveCells(scales); + + var enterCells = addItems(cells.enter()); + // There are two subsets of the enter items: + // 1. Those that enter because they move on screen (but the previous + // depth was not null). These should move with a transition. + // 2. Those that enter because the previous depth was null. These + // should wait for subset 1 to move, then fade in. + var moveInCells = enterCells + .filter(function(d) { return prevScales.getDepth(d) !== null; }); + var fadeInCells = enterCells + .filter(function(d) { return prevScales.getDepth(d) === null; }); + + // Phase 1 + // Highlight and position the move-in items with the old scales + moveInCells + .call(addLockHighlightSelection, highlighter.currentLock()) + .call(addActiveHighlightSelection, highlighter.currentActive()) + .call(positionItems, prevScales); + + cells.select('text') + .call(updateLabelVisibility); + + // Phase 2 + // Position the move-in, update, and exit items with a transition + moveInCells + .transition().duration(updateDuration) + .call(positionItems, scales); + cells + .transition().duration(updateDuration) + .call(positionItems, scales); + cells.exit() + .transition().duration(updateDuration) + .call(positionItems, scales); + + // Phase 3 + // Highlight and position the fade-in items, then fade in + fadeInCells + .call(addLockHighlightSelection, highlighter.currentLock()) + .call(addActiveHighlightSelection, highlighter.currentActive()) + .call(positionItems, scales) + .style("opacity", 0) + .transition().delay(updateDuration).duration(enterDuration) + .style("opacity", 1); + + // Phase 4 + // Remove the exit items + cells.exit() + .transition().delay(updateDuration + enterDuration) + .remove(); + } + + + // Calculate whether to display label in each cell ---------------------- + + // Finding the dimensions of SVG elements is expensive. We'll reduce the + // calls getBoundingClientRect() by caching the dimensions. + + // Cache the width of labels. This is a lookup table which, given the + // number of characters, gives the number of pixels. The label width + // never changes, so we can keep it outside of updateLabelVisibility(). + var labelWidthTable = {}; + function getLabelWidth(el, nchar) { + // Add entry if it doesn't already exist + if (labelWidthTable[nchar] === undefined) { + // If the text isn't displayed, then we can't get its width. Make + // sure it's visible, get the width, and then restore original + // display state. + var oldDisplay = el.style.display; + el.style.display = "inline"; + labelWidthTable[nchar] = el.getBoundingClientRect().width; + el.style.display = oldDisplay; + } + return labelWidthTable[nchar]; + } + + // Show labels that fit in the corresponding rectangle, and hide others. + function updateLabelVisibility(labels) { + // Cache the width of rects. This is a lookup table which, given the + // timespan (width in data), gives the number of pixels. The width of + // rects changes with the x scale, so we have to rebuild the table each + // time the scale changes. + var rectWidthTable = {}; + var x0 = scales.x(0); + function getRectWidth(time) { + // Add entry if it doesn't already exist + if (rectWidthTable[time] === undefined) { + rectWidthTable[time] = scales.x(time) - x0; + } + return rectWidthTable[time]; + } + + // Now calculate text and rect width for each cell. + labels.style("display", function(d) { + var labelWidth = getLabelWidth(this, d.label.length); + var boxWidth = getRectWidth(d.endTime - d.startTime); + + return (labelWidth <= boxWidth) ? "" : "none"; + }); + + return labels; + } + + + function onResize() { + updateContainerSize(); + + scales.x.range([0, dims.width]); + zoom.x(scales.x); + + // Preserve distance from bottom, instead of from top (which is the + // default behavior). + scales.y.range([ + dims.height, + dims.height - (domains.y[1] - domains.y[0]) * stackHeight + ]); + redrawImmediate(); + } + + // Attach mouse event handlers ------------------------------------ + var dragging = false; + + function addMouseEventHandlers(cells) { + cells + .on("mouseup", function(d) { + if (dragging) return; + + // If it wasn't a drag, treat it as a click + vis.infoBox.show(d); + highlighter.click(d); + notifySourceFileMessage(d, "select"); + }) + .on("mouseover", function(d) { + if (dragging) return; + + // If no label currently shown, display a tooltip + var label = this.querySelector(".profvis-label"); + if (label.style.display === "none") { + var box = this.getBBox(); + showTooltip( + d.label, + box.x + box.width / 2, + box.y - box.height + ); + } + + if (!highlighter.isLocked()) { + vis.infoBox.show(d); + highlighter.hover(d); + } + }) + .on("mouseout", function(d) { + if (dragging) return; + + hideTooltip(); + + if (!highlighter.isLocked()) { + vis.infoBox.hide(); + highlighter.hover(null); + } + }) + .on("dblclick.zoomcell", function(d) { + // When a cell is double-clicked, zoom x to that cell's width. + savePrevScales(); + + scales.x.domain(expandRange([d.startTime, d.endTime], zoomMargin)); + zoom.x(scales.x); + + redrawZoom(250); + + notifySourceFileMessage(d, "open"); + }); + + return cells; + } + + // Tooltip -------------------------------------------------------- + function showTooltip(label, x, y) { + var tooltip = container.append("g").attr("class", "profvis-tooltip"); + var tooltipRect = tooltip.append("rect"); + var tooltipLabel = tooltip.append("text") + .text(label) + .attr("x", x) + .attr("y", y + stackHeight * 0.2); // Shift down slightly for baseline + + // Add box around label + var labelBox = tooltipLabel.node().getBBox(); + var rectWidth = labelBox.width + 10; + var rectHeight = labelBox.height + 4; + tooltipRect + .attr("width", rectWidth) + .attr("height", rectHeight) + .attr("x", x - rectWidth / 2) + .attr("y", y - rectHeight / 2) + .attr("rx", 4) // Rounded corners -- can't set this in CSS + .attr("ry", 4); + } + + function hideTooltip() { + container.select("g.profvis-tooltip").remove(); + } + + + // Highlighting --------------------------------------------------------- + + function addLockHighlight(d) { + addLockHighlightSelection(cells, d); + } + + function clearLockHighlight() { + cells + .filter(".locked") + .classed({ locked: false }); + } + + + function addActiveHighlight(d) { + if (!d) return; + addActiveHighlightSelection(cells, d); + } + + function clearActiveHighlight() { + cells + .filter(".active") + .classed({ active: false }); + } + + // These are versions of addLockHighlight and addActiveHighlight which + // are only internally visible. It must be passed a selection of cells to + // perform the highlighting on. This can be more efficient because it can + // operate on just an enter selection instead of all cells. + function addLockHighlightSelection(selection, d) { + if (!d) return; + + var target = d; + selection + .filter(function(d) { return d === target; } ) + .classed({ locked: true }) + .call(moveToFront); + } + + function addActiveHighlightSelection(selection, d) { + if (!d) return; + + var target = d; + if (target.filename && target.linenum) { + selection + .filter(function(d) { + // Check for filename and linenum match, and if provided, a label match. + var match = d.filename === target.filename && + d.linenum === target.linenum; + if (!!target.label) { + match = match && (d.label === target.label); + } + return match; + }) + .classed({ active: true }); + + } else if (target.label) { + // Don't highlight blocks for these labels + var exclusions = ["", "FUN"]; + if (exclusions.some(function(x) { return target.label === x; })) { + return; + } + + // If we only have the label, search for cells that match, but make sure + // to not select ones that have a filename and linenum. + selection + .filter(function(d) { + return d.label === target.label && + d.filename === null && + d.linenum === null; + }) + .classed({ active: true }); + } + } + + // Move a D3 selection to front. If this is called on a selection, that + // selection should have been created with a data indexing function (e.g. + // data(data, function(d) { return ... })). Otherwise, the wrong object + // may be moved to the front. + function moveToFront(selection) { + return selection.each(function() { + this.parentNode.appendChild(this); + }); + } + + + // Panning and zooming -------------------------------------------- + // For panning and zooming x, d3.behavior.zoom does most of what we want + // automatically. For panning y, we can't use d3.behavior.zoom becuase it + // will also automatically add zooming, which we don't want. Instead, we + // need to use d3.behavior.drag and set the y domain appropriately. + var drag = d3.behavior.drag() + .on("drag", function() { + dragging = true; + var y = scales.y; + var ydom = y.domain(); + var ydiff = y.invert(d3.event.dy) - y.invert(0); + y.domain([ydom[0] - ydiff, ydom[1] - ydiff]); + }); + + + // For mousewheel zooming, we need to limit zoom amount. This is needed + // because in Firefox, zoom increments are too big. To do this, we limit + // scaleExtent before the first zoom event, and after each subsequent + // one. + // + // When zooming out, there's an additional limit: never zoom out past + // the original zoom span. The reason it's necessary to calculate this + // each time, instead of simply setting the scaleExtent() so that the + // lower bound is 1, is because other zoom events (like + // dblclick.zoomcell) are able to change the domain of scales.x, without + // changing the value of zoom.scale(). This means that the relationship + // between the zoom.scale() does not have a fixed linear relationship to + // the span of scales.x, and we have to recalculate it. + var maxZoomPerStep = 1.1; + + function zoomOutLimit() { + var span = scales.x.domain()[1] - scales.x.domain()[0]; + var startSpan = domains.x[1] - domains.x[0]; + return Math.min(maxZoomPerStep, startSpan/span); + } + + var zoom = d3.behavior.zoom() + .x(scales.x) + .on("zoomstart", function() { + zoom.scaleExtent([zoom.scale() / zoomOutLimit(), zoom.scale() * maxZoomPerStep]); + }) + .on("zoom", function(e) { + redrawImmediate(); + zoom.scaleExtent([zoom.scale() / zoomOutLimit(), zoom.scale() * maxZoomPerStep]); + }); + + // Register drag before zooming, because we need the drag to set the y + // scale before the zoom triggers a redraw. + svg + .on("mouseup", function(d) { + dragging = false; + }) + .call(drag); + + // Unlock selection when background is clicked, and zoom out when + // background is double-clicked. + backgroundRect + .on("mouseup", function(d) { + if (dragging) return; + + // If it wasn't a drag, hide info box and unlock. + vis.infoBox.hide(); + highlighter.click(null); + }) + .on("dblclick.zoombackground", function() { + savePrevScales(); + + scales.x.domain(domains.x); + zoom.x(scales.x); + + redrawZoom(250); + }); + + + var zoomEnabled = false; + function disableZoom() { + if (zoomEnabled) { + svg.on(".zoom", null); + zoomEnabled = false; + } + } + function enableZoom() { + if (!zoomEnabled) { + svg + .call(zoom) + .on("dblclick.zoom", null); // Disable zoom's built-in double-click behavior + zoomEnabled = true; + } + } + enableZoom(); + + onResize(); + + return { + el: el, + onResize: onResize, + onUpdateInternals: onResize, + redrawImmediate: redrawImmediate, + redrawZoom: redrawZoom, + redrawCollapse: redrawCollapse, + redrawUncollapse: redrawUncollapse, + savePrevScales: savePrevScales, + useCollapsedDepth: useCollapsedDepth, + useUncollapsedDepth: useUncollapsedDepth, + addLockHighlight: addLockHighlight, + clearLockHighlight: clearLockHighlight, + addActiveHighlight: addActiveHighlight, + clearActiveHighlight: clearActiveHighlight, + disableZoom: disableZoom, + enableZoom: enableZoom + }; + } // generateFlameGraph + + + function initInfoBox(el) { + + function show(d) { + var label = d.label ? d.label : ""; + var ref = (d.filename && d.linenum) ? + (d.filename + "#" + d.linenum) : + "(source unavailable)"; + + el.style.visibility = ""; + + el.innerHTML = + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "
Label" + escapeHTML(label) + "
Called from" + escapeHTML(ref) + "
Total time" + (d.endTime - d.startTime) + "ms
Memory" + + roundOneDecimal(d.sumMemDealloc) + " / " + roundOneDecimal(d.sumMemAlloc) + + " MB
Agg. total time" + vis.aggLabelTimes[label] + "ms
Call stack depth" + d.depth + "
"; + } + + function hide() { + el.style.visibility = "hidden"; + } + + hide(); + + return { + el: el, + show: show, + hide: hide + }; + } + + // Generate the tree table ---------------------------------------- + function generateTreetable(el) { + var content = d3.select(el); + + var table = content.append("table") + .attr("class", "results") + .attr("cellspacing", "0") + .attr("cellpadding", "0"); + + table.append("col"); + table.append("col") + .style("width", "120px"); + table.append("col") + .style("width", "50px") + .attr("class", "treetable-memory"); + table.append("col") + .style("width", "26px") + .attr("class", "treetable-memory"); + table.append("col") + .style("width", "50px") + .attr("class", "treetable-memory"); + table.append("col") + .style("width", "50px"); + table.append("col") + .style("width", "40px"); + + var tableBody = table.append("tbody"); + + var headerRows = tableBody.append("tr"); + + headerRows.append("th") + .attr("class", "code-label") + .text("Code"); + + headerRows.append("th") + .attr("class", "path") + .text("File"); + + headerRows.append("th") + .attr("class", "treetable-memory memory") + .attr("colspan", "3") + .text("Memory (MB)"); + + headerRows.append("th") + .attr("class", "time") + .attr("colspan", "2") + .text("Time (ms)"); + + // Retrieve all nodes (n), recursevely, where check(n) == true. + function allTopNodes(nodes, check) { + var included = []; + nodes = nodes.slice(); + + while (nodes.length > 0) { + var node = nodes.shift(); + + if (check(node)) + included.push(node); + else { + node.sumChildren.forEach(function(c1) { + nodes.unshift(c1); + }); + } + } + return included; + } + + // Is there one node (n), including root, where check(n) == true? + function oneNode(root, check) { + var nodes = [root]; + + while (nodes.length > 0) { + var n = nodes.shift(); + if (check(n)) + return true; + + n.sumChildren.forEach(function(x) { + nodes.unshift(x); + }); + } + + return false; + } + + function updateRowsDisplay(d) { + if (vis.hideInternals && d.isInternal) + return "none"; + else if (!vis.hideInternals && d.isDescendant) + return "none"; + + var collapsed = false; + while (d.parent) { + d = d.parent; + if (d.collapsed) { + collapsed = true; + break; + } + } + return collapsed ? "none" : ""; + } + + function toggleTreeNode(d) { + if (!d.canExpand) + return; + + var collapsed = d.collapsed; + if (collapsed === undefined) { + // Create a copy since we might insert the same node twice: once + // for the normal leaf the other one for a collapsed node. + var sumChildren = d.sumChildren.map(function(x) { + return jQuery.extend({}, x); + }); + + var childNodes = sumChildren.filter(function(x) { + return x.depthCollapsed !== null; + }); + + childNodes.forEach(function(x) { + x.isInternal = d.isInternal ? d.isInternal : false; + x.isDescendant = d.isDescendant ? d.isDescendant : false; + }); + + var internalChildNodes = sumChildren.filter(function(x) { + return x.depthCollapsed === null; + }); + + internalChildNodes.forEach(function(x) { + x.isInternal = true; + x.isDescendant = false; + }); + + var notInternalDescendantNodes = []; + if (!d.isInternal) { + notInternalDescendantNodes = allTopNodes(internalChildNodes, function(x) { + return x.depthCollapsed !== null && d.depth < x.depth; + }); + } + + notInternalDescendantNodes.forEach(function(x) { + x.isInternal = false; + x.isDescendant = true; + }); + + childNodes = childNodes.concat(internalChildNodes); + childNodes = childNodes.concat(notInternalDescendantNodes); + + childNodes.forEach(function(n) { + n.visualDepth = d.visualDepth + 1; + n.parent = d; + }); + + vis.profTable = vis.profTable.concat(childNodes); + d.collapsed = false; + + updateRows(); + + // Nodes are sorted "heaviest first" + if (childNodes.length == 1) toggleTreeNode(childNodes[0]); + } + else { + d.collapsed = !collapsed; + updateRows(); + } + } + + function updateLabelCells(labelCell) { + labelCell + .attr("nowrap", "true") + .style("padding-left", function(d) { + return (8 + 15 * (d.visualDepth - 1)) + "px"; + }) + .on("click", toggleTreeNode) + .attr("class", function(d) { + d.canExpand = false; + if (d.sumChildren) { + d.sumChildren.forEach(function(c) { + if (c.sumChildren.length > 0) { + if (!vis.hideInternals || oneNode(c, function(c1) { return c1.depthCollapsed !== null; })) + d.canExpand = true; + } + }); + } + + var collapsedClass = ""; + if (d.canExpand) + collapsedClass = d.collapsed === undefined ? "treetable-expand" : d.collapsed ? "treetable-expand" : "treetable-collapse"; + + return "code-label " + (d.canExpand ? "label-pointer " + collapsedClass : ""); + }); + } + + function updateRows() { + var rows = tableBody.selectAll("tr.treetable-row") + .data(vis.profTable, function(d) { + return d.id; + }); + + rows.exit() + .remove(); + + var updatedRows = rows + .style("display", updateRowsDisplay); + + var updatedLabelCells = updatedRows.selectAll("td.code-label"); + updateLabelCells(updatedLabelCells); + + var newRows = rows.enter() + .append("tr") + .filter(function(d) { + if (vis.hideInternals && d.depthCollapsed === null) + return false; + + return true; + }) + .on("click", function(d) { + table.selectAll("tr") + .style("background-color", null); + + this.style.backgroundColor = "rgb(241, 241, 241)"; + notifySourceFileMessage(d, "select"); + }) + .style("display", updateRowsDisplay); + + newRows + .attr("class", "treetable-row"); + + var labelCell = newRows.append("td"); + updateLabelCells(labelCell); + + var cellWrapper = labelCell.append("div"); + cellWrapper.append("div"); + + labelCell.append("div") + .attr("class", "label-text") + .text(function(d) { return d.label; }); + + newRows.append("td") + .attr("class", "path") + .text(function(d) { + var lastSlash = d.filename ? d.filename.lastIndexOf("/") : -1; + if (lastSlash >= 0) + return d.filename.substr(lastSlash + 1); + + return d.filename; + }); + + newRows.append("td") + .attr("class", "treetable-memory memory-info") + .text(function(d) { + return roundOneDecimal(d.sumMemDealloc); + }); + + var memoryBarContainer = newRows.append("td") + .attr("class", "treetable-memory memory-bar-container"); + + var memoryLeftCell = memoryBarContainer.append("div") + .attr("class", "memory-leftbar-wrapper"); + + memoryLeftCell.append("div") + .attr("class", "memory-leftbar") + .style("width", function(d) { + return 1 + Math.min(Math.abs(Math.min(Math.round(d.propMemDealloc * 5), 0)), 5) + "px"; + }); + + memoryBarContainer.append("div") + .attr("class", "memory-rightbar") + .style("width", function(d) { + return 1 + Math.min(Math.max(Math.round(d.propMemAlloc * 13), 0), 13) + "px"; + }); + + newRows.append("td") + .attr("class", "treetable-memory memory-info-right") + .text(function(d) { + return roundOneDecimal(d.sumMemAlloc); + }); + + newRows.append("td") + .attr("class", "time-info") + .text(function(d) { + return d.sumTime; + }); + + var timeCell = newRows.append("td") + .attr("class", "time-bar-container"); + + timeCell.append("div") + .attr("class", "timebar") + .style("width", function(d) { + return Math.round(d.propTime * 20) + "px"; + }); + + var unorderedRows = d3.selectAll("tr.treetable-row") + .data(vis.profTable, function(d) { + return d.id; + }); + + unorderedRows.sort(function(a,b) { + return (a.id < b.id) ? -1 : (a.id == b.id ? 0 : 1); + }); + + useMemoryResults(); + } + + var buildProfTable = function (profTree) { + var head = jQuery.extend({}, profTree); + var nodes = [head]; + + var aggregateChildren = function(node) { + var nameMap = {}; + node.children.forEach(function(c) { + var nameMapEntry = nameMap[c.label]; + if (!nameMapEntry) { + nameMapEntry = jQuery.extend({}, c); + nameMapEntry.sumTime = c.endTime - c.startTime; + nameMapEntry.sumChildren = []; + nameMapEntry.children = []; + nameMapEntry.parent = node; + nameMapEntry.sumCount = 1; + } + else { + nameMapEntry.sumMem = nameMapEntry.sumMem + c.sumMem; + nameMapEntry.sumMemDealloc = nameMapEntry.sumMemDealloc + c.sumMemDealloc; + nameMapEntry.sumMemAlloc = nameMapEntry.sumMemAlloc + c.sumMemAlloc; + nameMapEntry.sumTime = nameMapEntry.sumTime + (c.endTime - c.startTime); + nameMapEntry.sumCount = nameMapEntry.sumCount + 1; + } + + nameMapEntry.propMem = nameMapEntry.sumMem / vis.totalMem; + nameMapEntry.propMemDealloc = nameMapEntry.sumMemDealloc / vis.totalMem; + nameMapEntry.propMemAlloc = nameMapEntry.sumMemAlloc / vis.totalMem; + nameMapEntry.propTime = nameMapEntry.sumTime / vis.totalTime; + + c.children.forEach(function(e) { + nameMapEntry.children.push(e); + }); + + nameMap[c.label] = nameMapEntry; + }); + + var childrenSum = []; + for (var label in nameMap) { + childrenSum.push(nameMap[label]); + } + + // Sort by time descending + childrenSum.sort(function(a, b) { return b.sumTime - a.sumTime }); + return childrenSum; + }; + + function addToNodesAt(c, i) { + nodes.splice(i, 0, c); + } + + var id = 0; + while (nodes.length > 0) { + var node = nodes.shift(); + + node.id = id; + id = id + 1; + + node.sumChildren = aggregateChildren(node); + + // Processing in order is important to preserve order of IDs! + node.sumChildren.forEach(addToNodesAt); + } + + return head.sumChildren; + }; + + function useMemoryResults() { + d3.selectAll(".treetable-memory").style("display", vis.hideMemory ? "none" : ""); + } + + vis.profTable = buildProfTable(vis.profTree); + vis.profTable.forEach(function(e) { + e.visualDepth = 1; + }); + + updateRows(); + + return { + el: el, + onResize: updateRows, + onOptionsChange: updateRows, + onUpdateInternals: function() { + + }, + useMemoryResults: useMemoryResults + }; + } + + function enableScroll() { + vis.codeTable.enableScroll(); + vis.flameGraph.enableZoom(); + } + + function disableScroll() { + vis.codeTable.disableScroll(); + vis.flameGraph.disableZoom(); + } + + + // Set up resizing -------------------------------------------------------- + + // This is used as a jQuery event namespace so that we can remove the window + // resize handler on subsequent calls to initResizing(). Not elegant, but it + // gets the job done. + var resizeCallbackNamespace = randomString(10); + + // Resize panel1 and panel2 to 50% of available space and add callback + // for window resizing. + function initResizing() { + var $el = $(vis.el); + var $panel1 = $el.children(".profvis-panel1"); + var $panel2 = $el.children(".profvis-panel2"); + var $splitBar = $el.children(".profvis-splitbar"); + var $statusBar = $el.children(".profvis-status-bar"); + + // Clear any existing positioning that may have happened from previous + // calls to this function and the callbacks that it sets up. + $panel1.removeAttr("style"); + $panel2.removeAttr("style"); + $splitBar.removeAttr("style"); + $statusBar.removeAttr("style"); + + // CSS class suffix for split direction + var splitClass = (vis.splitDir === "h") ? "horizontal" : "vertical"; + + // Remove existing horizontal/vertical class and add the correct class back. + $panel1.removeClass("profvis-panel1-horizontal profvis-panel1-vertical"); + $panel2.removeClass("profvis-panel2-horizontal profvis-panel2-vertical"); + $splitBar.removeClass("profvis-splitbar-horizontal profvis-splitbar-vertical"); + $panel1.addClass("profvis-panel1-" + splitClass); + $panel2.addClass("profvis-panel2-" + splitClass); + $splitBar.addClass("profvis-splitbar-" + splitClass); + + + var splitBarGap; + var margin; + // Record the proportions from the previous call to resizePanels. This is + // needed when we resize the window to preserve the same proportions. + var lastSplitProportion; + + if (vis.splitDir === "v") { + // Record the gap between the split bar and the objects to left and right + splitBarGap = { + left: $splitBar.offset().left - offsetRight($panel1), + right: $panel2.offset().left - offsetRight($splitBar) + }; + + // Capture the initial distance from the left and right of container element + margin = { + left: $panel1.position().left, + right: $el.innerWidth() - positionRight($panel2) + }; + + } else if (vis.splitDir === "h") { + splitBarGap = { + top: $splitBar.offset().top - offsetBottom($panel1), + bottom: $panel2.offset().top - offsetBottom($splitBar) + }; + + margin = { + top: $panel1.position().top, + bottom: $el.innerWidth() - positionBottom($panel2) + }; + } + + // Resize the panels. splitProportion is a number from 0-1 representing the + // horizontal position of the split bar. + function resizePanels(splitProportion) { + if (!splitProportion) + splitProportion = lastSplitProportion; + + if (vis.splitDir === "v") { + var innerWidth = offsetRight($panel2) - $panel1.offset().left; + + $splitBar.offset({ + left: $panel1.offset().left + innerWidth * splitProportion - + $splitBar.outerWidth()/2 + }); + + // Size and position the panels + $panel1.outerWidth($splitBar.position().left - splitBarGap.left - + margin.left); + $panel2.offset({ left: offsetRight($splitBar) + splitBarGap.right }); + + } else if (vis.splitDir === "h") { + var innerHeight = offsetBottom($panel2) - $panel1.offset().top; + + $splitBar.offset({ + top: $panel1.offset().top + innerHeight * splitProportion - + $splitBar.outerHeight()/2 + }); + + // Size and position the panels + $panel1.outerHeight($splitBar.position().top - splitBarGap.top - + margin.top); + $panel2.offset({ top: offsetBottom($splitBar) + splitBarGap.bottom }); + } + + lastSplitProportion = splitProportion; + } + + // Initially, set widths to 50/50 + // For the first sizing, we don't need to call vis.flameGraph.onResize() + // because this happens before the flame graph is generated. + resizePanels(0.5); + + var resizePanelsDebounced = debounce(function() { + resizePanels(lastSplitProportion); + vis.activeViews.forEach(function(e) { + if (e.onResize) e.onResize(); + }); + }, 250); + + // Clear old resize handler and add new one. We use a namespace for this + // visualization to make sure not to delete handlers for other profvis + // visualizations on the same page (this can happen with Rmd documents). + $(window).off("resize.profvis." + resizeCallbackNamespace); + $(window).on("resize.profvis." + resizeCallbackNamespace, resizePanelsDebounced); + + // Get current proportional position of split bar + function splitProportion() { + var splitCenter; + + if (vis.splitDir === "v") { + splitCenter = $splitBar.offset().left - $panel1.offset().left + + $splitBar.outerWidth()/2; + var innerWidth = offsetRight($panel2) - $panel1.offset().left; + return splitCenter / innerWidth; + + } else if (vis.splitDir === "h") { + splitCenter = $splitBar.offset().top - $panel1.offset().top + + $splitBar.outerHeight()/2; + var innerHeight = offsetBottom($panel2) - $panel1.offset().top; + return splitCenter / innerHeight; + } + } + + function positionRight($el) { + return $el.position().left + $el.outerWidth(); + } + function offsetRight($el) { + return $el.offset().left + $el.outerWidth(); + } + function positionBottom($el) { + return $el.position().top + $el.outerHeight(); + } + function offsetBottom($el) { + return $el.offset().top + $el.outerHeight(); + } + + // Enable dragging of the split bar --------------------------------------- + (function() { + var dragging = false; + // For vertical split (left-right dragging) + var startDragX; + var startOffsetLeft; + // For horizontal split (up-down dragging) + var startDragY; + var startOffsetTop; + + var stopDrag = function(e) { + if (!dragging) return; + dragging = false; + + document.removeEventListener("mousemove", drag); + document.removeEventListener("mouseup", stopDrag); + + $splitBar.css("opacity", ""); + + if ((vis.splitDir === "v" && e.pageX - startDragX === 0) || + (vis.splitDir === "h" && e.pageY - startDragY === 0)) { + return; + } + + resizePanels(splitProportion()); + vis.flameGraph.onResize(); + }; + + var startDrag = function(e) { + // Don't start another drag if we're already in one. + if (dragging) return; + dragging = true; + pauseEvent(e); + + $splitBar.css("opacity", 0.75); + + if (vis.splitDir === "v") { + startDragX = e.pageX; + startOffsetLeft = $splitBar.offset().left; + } else { + startDragY = e.pageY; + startOffsetTop = $splitBar.offset().top; + } + + document.addEventListener("mousemove", drag); + document.addEventListener("mouseup", stopDrag); + }; + + var drag = function(e) { + if (!dragging) return; + pauseEvent(e); + + if (vis.splitDir === "v") { + var dx = e.pageX - startDragX; + if (dx === 0) + return; + + // Move the split bar + $splitBar.offset({ left: startOffsetLeft + dx }); + + } else if (vis.splitDir === "h") { + var dy = e.pageY - startDragY; + if (dy === 0) + return; + + // Move the split bar + $splitBar.offset({ top: startOffsetTop + dy }); + } + }; + + // Stop propogation so that we don't select text while dragging + function pauseEvent(e){ + if(e.stopPropagation) e.stopPropagation(); + if(e.preventDefault) e.preventDefault(); + e.cancelBubble = true; + e.returnValue = false; + return false; + } + + // Remove existing event listener from previous calls to initResizing(). + $splitBar.off("mousedown.profvis"); + $splitBar.on("mousedown.profvis", startDrag); + })(); + + + return { + resizePanels: resizePanels + }; + } + + + var prof = prepareProfData(message.prof, message.interval); + + var vis = { + el: el, + prof: prof, + profTree: getProfTree(prof), + interval: message.interval, + totalTime: getTotalTime(prof), + totalMem: getTotalMemory(prof), + files: message.files, + aggLabelTimes: getAggregatedLabelTimes(prof), + fileLineStats: getFileLineStats(prof, message.files), + profTable: [], + + // Objects representing each component + statusBar: null, + optionsPanel: null, + codeTable: null, + flameGraph: null, + infoBox: null, + treetable: null, + activeViews: [], + + // Functions to enable/disable responding to scrollwheel events + enableScroll: enableScroll, + disableScroll: disableScroll, + + splitDir: message.split, + hideInternals: true, + hideMemory: false, + + resizePanels: null + }; + + + // Render the objects --------------------------------------------- + + var statusBarEl = document.createElement("div"); + statusBarEl.className = "profvis-status-bar"; + vis.el.appendChild(statusBarEl); + + // Container panels - top/bottom or left/right + var panel1 = document.createElement("div"); + panel1.className = "profvis-panel1"; + vis.el.appendChild(panel1); + + var panel2 = document.createElement("div"); + panel2.className = "profvis-panel2"; + vis.el.appendChild(panel2); + + var splitBarEl = document.createElement("div"); + splitBarEl.className = "profvis-splitbar"; + vis.el.appendChild(splitBarEl); + + var footerEl = document.createElement("div"); + footerEl.className = "profvis-footer"; + vis.el.appendChild(footerEl); + + // Items in the panels + var codeTableEl = document.createElement("div"); + codeTableEl.className = "profvis-code"; + panel1.appendChild(codeTableEl); + + var flameGraphEl = document.createElement("div"); + flameGraphEl.className = "profvis-flamegraph"; + panel2.appendChild(flameGraphEl); + + var infoBoxEl = document.createElement("div"); + infoBoxEl.className = "profvis-infobox"; + panel2.appendChild(infoBoxEl); + + var treetableEl = document.createElement("div"); + treetableEl.className = "profvis-treetable"; + treetableEl.style.display = "none"; + vis.el.appendChild(treetableEl); + + var optionsPanelEl = document.createElement("div"); + optionsPanelEl.className = "profvis-options-panel"; + vis.el.appendChild(optionsPanelEl); + + // Efficient to properly size panels before the code + flamegraph are + // rendered, so that we don't have to re-render. + var resize = initResizing(); + vis.resizePanels = resize.resizePanels; + + var hideViews = function() { + splitBarEl.style.display = "none"; + panel1.style.display = "none"; + panel2.style.display = "none"; + treetableEl.style.display = "none"; + }; + + var toggleViews = function(view) { + hideViews(); + + switch (view) { + case "flamegraph": + splitBarEl.style.display = "block"; + panel1.style.display = "block"; + panel2.style.display = "block"; + + vis.activeViews = [vis.flameGraph, vis.codeTable]; + vis.resizePanels(); + break; + case "treetable": + if (!vis.treetable) { + vis.treetable = generateTreetable(treetableEl); + } + + treetableEl.style.display = "block"; + + vis.activeViews = [vis.treetable]; + break; + } + + vis.activeViews.forEach(function(e) { + if (e.onResize) e.onResize(); + }); + }; + + var onOptionsChange = function(option, checked) { + switch (option) + { + case "split": { + vis.splitDir = checked ? "h" : "v"; + // Check that flame graph is visible + if ($.inArray(vis.flameGraph, vis.activeViews) !== -1) { + initResizing(); + vis.flameGraph.onResize(); + } + break; + } + case "internals": { + vis.flameGraph.savePrevScales(); + + vis.hideInternals = checked; + if (checked) { + vis.flameGraph.useCollapsedDepth(); + vis.flameGraph.redrawCollapse(400, 400); + } else { + vis.flameGraph.useUncollapsedDepth(); + vis.flameGraph.redrawUncollapse(400, 250); + } + + vis.activeViews.forEach(function(e) { + if (e.onOptionsChange) e.onOptionsChange(); + }); + + break; + } + case "memory": { + vis.hideMemory = checked; + vis.activeViews.forEach(function(e) { + if (e.useMemoryResults) e.useMemoryResults(); + }); + break; + } + } + }; + + // Create the UI components + vis.statusBar = generateStatusBar(statusBarEl, toggleViews); + vis.footer = generateFooter(footerEl); + vis.optionsPanel = generateOptionsPanel(optionsPanelEl, onOptionsChange); + vis.codeTable = generateCodeTable(codeTableEl); + vis.flameGraph = generateFlameGraph(flameGraphEl); + vis.infoBox = initInfoBox(infoBoxEl); + vis.treetable = null; + vis.activeViews = [vis.flameGraph, vis.codeTable]; + + // If any depth collapsing occured, enable the "hide internal" checkbox. + if (prof.some(function(d) { return d.depth !== d.depthCollapsed; })) { + vis.optionsPanel.enableHideInternal(); + } + + // Start with scrolling disabled because of mousewheel scrolling issue + disableScroll(); + + // Make the vis object accessible via the DOM element + $(el).data("profvis", vis); + + return vis; + }; // profvis.render() + + // Calculate amount of time spent on each line of code. Returns nested objects + // grouped by file, and then by line number. + function getFileLineStats(prof, files) { + // Drop entries with null or "" filename + prof = prof.filter(function(row) { + return row.filename !== null && row.filename !== ""; + }); + + // Gather line-by-line file contents + var fileLineStats = files.map(function(file) { + // Create array of objects with info for each line of code. + var lines = file.content.split("\n"); + var lineData = []; + var filename = file.filename; + var normpath = file.normpath; + for (var i=0; i
+ + - - - + diff --git a/docs/reference/as_fst.html b/docs/reference/as_fst.html index 300e5ec..c5acdd9 100644 --- a/docs/reference/as_fst.html +++ b/docs/reference/as_fst.html @@ -1,73 +1,13 @@ - - - - - - - -Save a data.frame as a fst table — as_fst • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Save a data.frame as a fst table — as_fst • tidyft - + - - -
-
- - -
-
+
@@ -136,72 +61,70 @@

Save a data.frame as a fst table

and then parse it back as a fst table (class name is "fst_table").

-
as_fst(.data)
+
+
as_fst(.data)
+
-

Arguments

- - - - - - -
.data

A data.frame

+
+

Arguments

-

Value

-

An object of class fst_table

+
.data
+

A data.frame

-

Examples

-
-# \donttest{ - iris %>% - as_fst() -> iris_fst - iris_fst
#> <fst file> -#> 150 rows, 5 columns (.a5445415a58.fst) -#> -#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <double> <double> <double> <double> <factor> -#> 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
# } -
-
- +
+

Value

+

An object of class fst_table

+
+
+

Examples

+

+# \donttest{
+  iris %>%
+    as_fst() -> iris_fst
+  iris_fst
+#> <fst file>
+#> 150 rows, 5 columns (.27606c92540e.fst)
+#> 
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>         <double>    <double>     <double>    <double>  <factor>
+#> 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
+# }
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/complete.html b/docs/reference/complete.html index 2dce3ea..0d9cfee 100644 --- a/docs/reference/complete.html +++ b/docs/reference/complete.html @@ -1,73 +1,13 @@ - - - - - - - -Complete a data frame with missing combinations of data — complete • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Complete a data frame with missing combinations of data — complete • tidyft - + - - -
-
- - -
-
+
@@ -136,150 +61,158 @@

Complete a data frame with missing combinations of data

Analogous function for complete function in tidyr.

-
complete(.data, ..., fill = NA)
+
+
complete(.data, ..., fill = NA)
+
+ +
+

Arguments

-

Arguments

- - - - - - - - - - - - - - -
.data

data.frame

...

Specification of columns to expand.The selection of columns is -supported by the flexible select_dt. + +

.data
+

data.frame

+ + +
...
+

Specification of columns to expand.The selection of columns is +supported by the flexible select_dt. To find all unique combinations of provided columns, including those not found in the data, supply each variable as a separate argument. But the two modes (select the needed columns and fill outside values) could not be mixed, -find more details in examples.

fill

Atomic value to fill into the missing cell, default uses NA.

+find more details in examples.

-

Value

-

data.table

-

Details

+
fill
+

Atomic value to fill into the missing cell, default uses NA.

+
+
+

Value

+

data.table

+
+
+

Details

When the provided columns with addtion data are of different length, all the unique combinations would be returned. This operation should be used only on unique entries, and it will always returned the unique entries.

If you supply fill parameter, these values will also replace existing explicit missing values in the data set.

-

See also

- - - -

Examples

-
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 -) - -df %>% complete(item_id,item_name)
#> Key: <item_id, item_name> -#> item_id item_name group value1 value2 -#> <num> <char> <num> <int> <int> -#> 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
df %>% complete(item_id,item_name,fill = 0)
#> Key: <item_id, item_name> -#> item_id item_name group value1 value2 -#> <num> <char> <num> <int> <int> -#> 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
df %>% complete("item")
#> Key: <item_id, item_name> -#> item_id item_name group value1 value2 -#> <num> <char> <num> <int> <int> -#> 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
df %>% complete(item_id=1:3)
#> Key: <item_id> -#> item_id group item_name value1 value2 -#> <int> <num> <char> <int> <int> -#> 1: 1 1 a 1 4 -#> 2: 2 2 b 2 5 -#> 3: 2 1 b 3 6 -#> 4: 3 NA <NA> NA NA
df %>% complete(item_id=1:3,group=1:2)
#> Key: <item_id, group> -#> item_id group item_name value1 value2 -#> <int> <int> <char> <int> <int> -#> 1: 1 1 a 1 4 -#> 2: 1 2 <NA> NA NA -#> 3: 2 1 b 3 6 -#> 4: 2 2 b 2 5 -#> 5: 3 1 <NA> NA NA -#> 6: 3 2 <NA> NA NA
df %>% complete(item_id=1:3,group=1:3,item_name=c("a","b","c"))
#> Key: <item_id, group, item_name> -#> item_id group item_name value1 value2 -#> <int> <int> <char> <int> <int> -#> 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 -#> 18: 2 3 c NA NA -#> 19: 3 1 a NA NA -#> 20: 3 1 b NA NA -#> 21: 3 1 c NA NA -#> 22: 3 2 a NA NA -#> 23: 3 2 b NA NA -#> 24: 3 2 c NA NA -#> 25: 3 3 a NA NA -#> 26: 3 3 b NA NA -#> 27: 3 3 c NA NA -#> item_id group item_name value1 value2
-
-
- +
+

See also

+ +
+
+

Examples

+
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
+)
+
+df %>% complete(item_id,item_name)
+#> Key: <item_id, item_name>
+#>    item_id item_name group value1 value2
+#>      <num>    <char> <num>  <int>  <int>
+#> 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
+df %>% complete(item_id,item_name,fill = 0)
+#> Key: <item_id, item_name>
+#>    item_id item_name group value1 value2
+#>      <num>    <char> <num>  <int>  <int>
+#> 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
+df %>% complete("item")
+#> Key: <item_id, item_name>
+#>    item_id item_name group value1 value2
+#>      <num>    <char> <num>  <int>  <int>
+#> 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
+df %>% complete(item_id=1:3)
+#> Key: <item_id>
+#>    item_id group item_name value1 value2
+#>      <int> <num>    <char>  <int>  <int>
+#> 1:       1     1         a      1      4
+#> 2:       2     2         b      2      5
+#> 3:       2     1         b      3      6
+#> 4:       3    NA      <NA>     NA     NA
+df %>% complete(item_id=1:3,group=1:2)
+#> Key: <item_id, group>
+#>    item_id group item_name value1 value2
+#>      <int> <int>    <char>  <int>  <int>
+#> 1:       1     1         a      1      4
+#> 2:       1     2      <NA>     NA     NA
+#> 3:       2     1         b      3      6
+#> 4:       2     2         b      2      5
+#> 5:       3     1      <NA>     NA     NA
+#> 6:       3     2      <NA>     NA     NA
+df %>% complete(item_id=1:3,group=1:3,item_name=c("a","b","c"))
+#> Key: <item_id, group, item_name>
+#>     item_id group item_name value1 value2
+#>       <int> <int>    <char>  <int>  <int>
+#>  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
+#> 18:       2     3         c     NA     NA
+#> 19:       3     1         a     NA     NA
+#> 20:       3     1         b     NA     NA
+#> 21:       3     1         c     NA     NA
+#> 22:       3     2         a     NA     NA
+#> 23:       3     2         b     NA     NA
+#> 24:       3     2         c     NA     NA
+#> 25:       3     3         a     NA     NA
+#> 26:       3     3         b     NA     NA
+#> 27:       3     3         c     NA     NA
+#>     item_id group item_name value1 value2
+
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/count.html b/docs/reference/count.html index 895be5a..0f6fe7e 100644 --- a/docs/reference/count.html +++ b/docs/reference/count.html @@ -1,72 +1,12 @@ - - - - - - - -Count observations by group — count • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Count observations by group — count • tidyft - + - - -
-
- - -
-
+
@@ -134,135 +59,141 @@

Count observations by group

Analogous function for count and add_count in dplyr.

-
count(.data, ..., sort = FALSE, name = "n")
-
-add_count(.data, ..., name = "n")
- -

Arguments

- - - - - - - - - - - - - - - - - - -
.data

data.table

...

variables to group by.

sort

logical. If TRUE result will be sorted in desending order by resulting variable.

name

character. Name of resulting variable. Default uses "n".

- -

Value

+
+
count(.data, ..., sort = FALSE, name = "n")
+
+add_count(.data, ..., name = "n")
+
+ +
+

Arguments

-

data.table

-

Examples

-
a = as.data.table(mtcars) -count(a,cyl)
#> cyl n -#> <num> <int> -#> 1: 6 7 -#> 2: 4 11 -#> 3: 8 14
count(a,cyl,sort = TRUE)
#> cyl n -#> <num> <int> -#> 1: 8 14 -#> 2: 4 11 -#> 3: 6 7
a
#> mpg cyl disp hp drat wt qsec vs am gear carb -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 -#> 2: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 -#> 3: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 -#> 4: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 -#> 5: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 -#> 6: 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 -#> 7: 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 -#> 8: 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 -#> 9: 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 -#> 10: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 -#> 11: 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 -#> 12: 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 -#> 13: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 -#> 14: 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 -#> 15: 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 -#> 16: 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 -#> 17: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 -#> 18: 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 -#> 19: 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 -#> 20: 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 -#> 21: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 -#> 22: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 -#> 23: 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 -#> 24: 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 -#> 25: 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 -#> 26: 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 -#> 27: 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 -#> 28: 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 -#> 29: 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 -#> 30: 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 -#> 31: 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 -#> 32: 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 -#> mpg cyl disp hp drat wt qsec vs am gear carb
-b = as.data.table(iris) -b %>% add_count(Species,name = "N")
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species N -#> <num> <num> <num> <num> <fctr> <int> -#> 1: 5.1 3.5 1.4 0.2 setosa 50 -#> 2: 4.9 3.0 1.4 0.2 setosa 50 -#> 3: 4.7 3.2 1.3 0.2 setosa 50 -#> 4: 4.6 3.1 1.5 0.2 setosa 50 -#> 5: 5.0 3.6 1.4 0.2 setosa 50 -#> --- -#> 146: 6.7 3.0 5.2 2.3 virginica 50 -#> 147: 6.3 2.5 5.0 1.9 virginica 50 -#> 148: 6.5 3.0 5.2 2.0 virginica 50 -#> 149: 6.2 3.4 5.4 2.3 virginica 50 -#> 150: 5.9 3.0 5.1 1.8 virginica 50
b
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species N -#> <num> <num> <num> <num> <fctr> <int> -#> 1: 5.1 3.5 1.4 0.2 setosa 50 -#> 2: 4.9 3.0 1.4 0.2 setosa 50 -#> 3: 4.7 3.2 1.3 0.2 setosa 50 -#> 4: 4.6 3.1 1.5 0.2 setosa 50 -#> 5: 5.0 3.6 1.4 0.2 setosa 50 -#> --- -#> 146: 6.7 3.0 5.2 2.3 virginica 50 -#> 147: 6.3 2.5 5.0 1.9 virginica 50 -#> 148: 6.5 3.0 5.2 2.0 virginica 50 -#> 149: 6.2 3.4 5.4 2.3 virginica 50 -#> 150: 5.9 3.0 5.1 1.8 virginica 50
-
- +
+

Value

+

data.table

+
+ +
+

Examples

+
a = as.data.table(mtcars)
+count(a,cyl)
+#>      cyl     n
+#>    <num> <int>
+#> 1:     6     7
+#> 2:     4    11
+#> 3:     8    14
+count(a,cyl,sort = TRUE)
+#>      cyl     n
+#>    <num> <int>
+#> 1:     8    14
+#> 2:     4    11
+#> 3:     6     7
+a
+#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
+#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#>  1:  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4
+#>  2:  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4
+#>  3:  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1
+#>  4:  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1
+#>  5:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
+#>  6:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1
+#>  7:  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4
+#>  8:  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2
+#>  9:  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2
+#> 10:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
+#> 11:  17.8     6 167.6   123  3.92 3.440 18.90     1     0     4     4
+#> 12:  16.4     8 275.8   180  3.07 4.070 17.40     0     0     3     3
+#> 13:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3
+#> 14:  15.2     8 275.8   180  3.07 3.780 18.00     0     0     3     3
+#> 15:  10.4     8 472.0   205  2.93 5.250 17.98     0     0     3     4
+#> 16:  10.4     8 460.0   215  3.00 5.424 17.82     0     0     3     4
+#> 17:  14.7     8 440.0   230  3.23 5.345 17.42     0     0     3     4
+#> 18:  32.4     4  78.7    66  4.08 2.200 19.47     1     1     4     1
+#> 19:  30.4     4  75.7    52  4.93 1.615 18.52     1     1     4     2
+#> 20:  33.9     4  71.1    65  4.22 1.835 19.90     1     1     4     1
+#> 21:  21.5     4 120.1    97  3.70 2.465 20.01     1     0     3     1
+#> 22:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2
+#> 23:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2
+#> 24:  13.3     8 350.0   245  3.73 3.840 15.41     0     0     3     4
+#> 25:  19.2     8 400.0   175  3.08 3.845 17.05     0     0     3     2
+#> 26:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1
+#> 27:  26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2
+#> 28:  30.4     4  95.1   113  3.77 1.513 16.90     1     1     5     2
+#> 29:  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4
+#> 30:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6
+#> 31:  15.0     8 301.0   335  3.54 3.570 14.60     0     1     5     8
+#> 32:  21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2
+#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
+
+b = as.data.table(iris)
+b %>% add_count(Species,name = "N")
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species     N
+#>             <num>       <num>        <num>       <num>    <fctr> <int>
+#>   1:          5.1         3.5          1.4         0.2    setosa    50
+#>   2:          4.9         3.0          1.4         0.2    setosa    50
+#>   3:          4.7         3.2          1.3         0.2    setosa    50
+#>   4:          4.6         3.1          1.5         0.2    setosa    50
+#>   5:          5.0         3.6          1.4         0.2    setosa    50
+#>  ---                                                                  
+#> 146:          6.7         3.0          5.2         2.3 virginica    50
+#> 147:          6.3         2.5          5.0         1.9 virginica    50
+#> 148:          6.5         3.0          5.2         2.0 virginica    50
+#> 149:          6.2         3.4          5.4         2.3 virginica    50
+#> 150:          5.9         3.0          5.1         1.8 virginica    50
+b
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species     N
+#>             <num>       <num>        <num>       <num>    <fctr> <int>
+#>   1:          5.1         3.5          1.4         0.2    setosa    50
+#>   2:          4.9         3.0          1.4         0.2    setosa    50
+#>   3:          4.7         3.2          1.3         0.2    setosa    50
+#>   4:          4.6         3.1          1.5         0.2    setosa    50
+#>   5:          5.0         3.6          1.4         0.2    setosa    50
+#>  ---                                                                  
+#> 146:          6.7         3.0          5.2         2.3 virginica    50
+#> 147:          6.3         2.5          5.0         1.9 virginica    50
+#> 148:          6.5         3.0          5.2         2.0 virginica    50
+#> 149:          6.2         3.4          5.4         2.3 virginica    50
+#> 150:          5.9         3.0          5.1         1.8 virginica    50
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/cummean.html b/docs/reference/cummean.html index 5d73212..bd15ddf 100644 --- a/docs/reference/cummean.html +++ b/docs/reference/cummean.html @@ -1,72 +1,12 @@ - - - - - - - -Cumulative mean — cummean • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Cumulative mean — cummean • tidyft - + - - -
-
- - -
-
+
@@ -134,54 +59,52 @@

Cumulative mean

Returns a vector whose elements are the cumulative mean of the elements of the argument.

-
cummean(x)
+
+
cummean(x)
+
-

Arguments

- - - - - - -
x

a numeric or complex object, -or an object that can be coerced to one of these.

+
+

Arguments

-

Value

-

A numeric vector

+
x
+

a numeric or complex object, +or an object that can be coerced to one of these.

-

Examples

-
cummean(1:10)
#> [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5
-
-
- +
+

Value

+

A numeric vector

+
+
+

Examples

+
cummean(1:10)
+#>  [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5
+
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/distinct.html b/docs/reference/distinct.html index 2ef71bc..f89f37f 100644 --- a/docs/reference/distinct.html +++ b/docs/reference/distinct.html @@ -1,72 +1,12 @@ - - - - - - - -Select distinct/unique rows in data.table — distinct • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Select distinct/unique rows in data.table — distinct • tidyft - + - - -
-
- - -
-
+
@@ -134,83 +59,83 @@

Select distinct/unique rows in data.table

Analogous function for distinct in dplyr

-
distinct(.data, ..., .keep_all = FALSE)
- -

Arguments

- - - - - - - - - - - - - - -
.data

data.table

...

Optional variables to use when determining uniqueness. +

+
distinct(.data, ..., .keep_all = FALSE)
+
+ +
+

Arguments

+ + +
.data
+

data.table

+ + +
...
+

Optional variables to use when determining uniqueness. If there are multiple rows for a given combination of inputs, only the first row will be preserved. -If omitted, will use all variables.

.keep_all

If TRUE, keep all variables in data.table. If a combination of ... is not distinct, -this keeps the first row of values.

+If omitted, will use all variables.

+ -

Value

+
.keep_all
+

If TRUE, keep all variables in data.table. If a combination of ... is not distinct, +this keeps the first row of values.

+
+
+

Value

data.table

-

See also

- - - -

Examples

-
- a = as.data.table(iris) - b = as.data.table(mtcars) - a %>% distinct(Species)
#> Species -#> <fctr> -#> 1: setosa -#> 2: versicolor -#> 3: virginica
b %>% distinct(cyl,vs,.keep_all = TRUE)
#> mpg cyl disp hp drat wt qsec vs am gear carb -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 -#> 2: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 -#> 3: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 -#> 4: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 -#> 5: 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
- -
-
- +
+

See also

+ +
+
+

Examples

+

+ a = as.data.table(iris)
+ b = as.data.table(mtcars)
+ a %>% distinct(Species)
+#>       Species
+#>        <fctr>
+#> 1:     setosa
+#> 2: versicolor
+#> 3:  virginica
+ b %>% distinct(cyl,vs,.keep_all = TRUE)
+#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
+#>    <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#> 1:  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4
+#> 2:  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1
+#> 3:  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1
+#> 4:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
+#> 5:  26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2
+
+
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/drop_delete_na.html b/docs/reference/drop_delete_na.html index 3699b00..a05fd54 100644 --- a/docs/reference/drop_delete_na.html +++ b/docs/reference/drop_delete_na.html @@ -1,73 +1,13 @@ - - - - - - - -Drop or delete data by rows or columns — drop_na • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Drop or delete data by rows or columns — drop_na • tidyft - + - - -
-
- - -
-
+
@@ -136,104 +61,114 @@

Drop or delete data by rows or columns

delete_na deletes rows or columns with too many NAs.

-
drop_na(.data, ...)
-
-delete_na(.data, MARGIN, n)
- -

Arguments

- - - - - - - - - - - - - - - - - - -
.data

A data.table

...

Colunms to be dropped or deleted.

MARGIN

1 or 2. 1 for deleting rows, 2 for deleting columns.

n

If number (proportion) of NAs is larger than or equal to "n", -the columns/rows would be deleted. When smaller than 1, use as proportion. -When larger or equal to 1, use as number.

+
+
drop_na(.data, ...)
+
+delete_na(.data, MARGIN, n)
+
-

Value

+
+

Arguments

-

A data.table

-

Examples

-
x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4)) -x
#> x y z -#> <num> <num> <lgcl> -#> 1: 1 NA NA -#> 2: 2 NA NA -#> 3: NA 4 NA -#> 4: 3 5 NA
x %>% delete_na(2,0.75)
#> x y -#> <num> <num> -#> 1: 1 NA -#> 2: 2 NA -#> 3: NA 4 -#> 4: 3 5
-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)
#> x -#> <num> -#> 1: 1 -#> 2: 2 -#> 3: NA -#> 4: 3
-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)
#> Null data.table (0 rows and 0 cols)
-x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4)) -x %>% delete_na(2,2)
#> x -#> <num> -#> 1: 1 -#> 2: 2 -#> 3: NA -#> 4: 3
-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)
#> x y z -#> <num> <num> <lgcl> -#> 1: 3 5 NA
x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4)) -x %>% delete_na(1,2)
#> x y z -#> <num> <num> <lgcl> -#> 1: 3 5 NA
- -
-
- +
+

Value

+

A data.table

+
+ +
+

Examples

+
x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4))
+x
+#>        x     y      z
+#>    <num> <num> <lgcl>
+#> 1:     1    NA     NA
+#> 2:     2    NA     NA
+#> 3:    NA     4     NA
+#> 4:     3     5     NA
+x %>% delete_na(2,0.75)
+#>        x     y
+#>    <num> <num>
+#> 1:     1    NA
+#> 2:     2    NA
+#> 3:    NA     4
+#> 4:     3     5
+
+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)
+#>        x
+#>    <num>
+#> 1:     1
+#> 2:     2
+#> 3:    NA
+#> 4:     3
+
+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)
+#> Null data.table (0 rows and 0 cols)
+
+x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4))
+x %>% delete_na(2,2)
+#>        x
+#>    <num>
+#> 1:     1
+#> 2:     2
+#> 3:    NA
+#> 4:     3
+
+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)
+#>        x     y      z
+#>    <num> <num> <lgcl>
+#> 1:     3     5     NA
+x = data.table(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5),z = rep(NA,4))
+x %>% delete_na(1,2)
+#>        x     y      z
+#>    <num> <num> <lgcl>
+#> 1:     3     5     NA
+
+
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/dummy.html b/docs/reference/dummy.html index 9a1d3b6..617b998 100644 --- a/docs/reference/dummy.html +++ b/docs/reference/dummy.html @@ -1,73 +1,13 @@ - - - - - - - -Fast creation of dummy variables — dummy • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Fast creation of dummy variables — dummy • tidyft - + - - -
-
- - -
-
+
@@ -136,162 +61,166 @@

Fast creation of dummy variables

This function is useful for statistical analysis when you want binary columns rather than character columns.

-
dummy(.data, ..., longname = TRUE)
+
+
dummy(.data, ..., longname = TRUE)
+
+ +
+

Arguments

-

Arguments

- - - - - - - - - - - - - - -
.data

data.frame

...

Columns you want to create dummy variables from. -Very flexible, find in the examples.

longname

logical. Should the output column labeled with the -original column name? Default uses TRUE.

-

Value

+
.data
+

data.frame

-

data.table

-

Details

+
...
+

Columns you want to create dummy variables from. +Very flexible, find in the examples.

+ + +
longname
+

logical. Should the output column labeled with the +original column name? Default uses TRUE.

+ +
+
+

Value

+

data.table

+
+
+

Details

If no columns provided, will return the original data frame.

This function is inspired by fastDummies package, but provides -simple and precise usage, whereas fastDummies::dummy_cols provides more +simple and precise usage, whereas fastDummies::dummy_cols provides more features for statistical usage.

-

See also

- - - -

Examples

-
-iris = as.data.table(iris) -iris %>% dummy(Species)
#> Key: <Sepal.Length, Sepal.Width, Petal.Length, Petal.Width> -#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species_setosa -#> <num> <num> <num> <num> <num> -#> 1: 4.3 3.0 1.1 0.1 1 -#> 2: 4.4 2.9 1.4 0.2 1 -#> 3: 4.4 3.0 1.3 0.2 1 -#> 4: 4.4 3.2 1.3 0.2 1 -#> 5: 4.5 2.3 1.3 0.3 1 -#> --- -#> 146: 7.7 2.6 6.9 2.3 0 -#> 147: 7.7 2.8 6.7 2.0 0 -#> 148: 7.7 3.0 6.1 2.3 0 -#> 149: 7.7 3.8 6.7 2.2 0 -#> 150: 7.9 3.8 6.4 2.0 0 -#> Species_versicolor Species_virginica -#> <num> <num> -#> 1: 0 0 -#> 2: 0 0 -#> 3: 0 0 -#> 4: 0 0 -#> 5: 0 0 -#> --- -#> 146: 0 1 -#> 147: 0 1 -#> 148: 0 1 -#> 149: 0 1 -#> 150: 0 1
iris %>% dummy(Species,longname = FALSE)
#> Key: <Sepal.Length, Sepal.Width, Petal.Length, Petal.Width> -#> Sepal.Length Sepal.Width Petal.Length Petal.Width setosa versicolor -#> <num> <num> <num> <num> <num> <num> -#> 1: 4.3 3.0 1.1 0.1 1 0 -#> 2: 4.4 2.9 1.4 0.2 1 0 -#> 3: 4.4 3.0 1.3 0.2 1 0 -#> 4: 4.4 3.2 1.3 0.2 1 0 -#> 5: 4.5 2.3 1.3 0.3 1 0 -#> --- -#> 146: 7.7 2.6 6.9 2.3 0 0 -#> 147: 7.7 2.8 6.7 2.0 0 0 -#> 148: 7.7 3.0 6.1 2.3 0 0 -#> 149: 7.7 3.8 6.7 2.2 0 0 -#> 150: 7.9 3.8 6.4 2.0 0 0 -#> virginica -#> <num> -#> 1: 0 -#> 2: 0 -#> 3: 0 -#> 4: 0 -#> 5: 0 -#> --- -#> 146: 1 -#> 147: 1 -#> 148: 1 -#> 149: 1 -#> 150: 1
-mtcars = as.data.table(mtcars) -mtcars %>% head() %>% dummy(vs,am)
#> Key: <mpg, cyl, disp, hp, drat, wt, qsec, gear, carb, vs_0, vs_1> -#> mpg cyl disp hp drat wt qsec gear carb vs_0 vs_1 am_0 -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 18.1 6 225 105 2.76 3.460 20.22 3 1 0 1 1 -#> 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 -#> <num> -#> 1: 0 -#> 2: 0 -#> 3: 1 -#> 4: 1 -#> 5: 0 -#> 6: 1
mtcars %>% head() %>% dummy("cyl|gear")
#> Key: <mpg, disp, hp, drat, wt, qsec, vs, am, carb, cyl_4, cyl_6, cyl_8> -#> mpg disp hp drat wt qsec vs am carb cyl_4 cyl_6 cyl_8 -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 18.1 225 105 2.76 3.460 20.22 1 0 1 0 1 0 -#> 2: 18.7 360 175 3.15 3.440 17.02 0 0 2 0 0 1 -#> 3: 21.0 160 110 3.90 2.620 16.46 0 1 4 0 1 0 -#> 4: 21.0 160 110 3.90 2.875 17.02 0 1 4 0 1 0 -#> 5: 21.4 258 110 3.08 3.215 19.44 1 0 1 0 1 0 -#> 6: 22.8 108 93 3.85 2.320 18.61 1 1 1 1 0 0 -#> gear_3 gear_4 -#> <num> <num> -#> 1: 1 0 -#> 2: 1 0 -#> 3: 0 1 -#> 4: 0 1 -#> 5: 1 0 -#> 6: 0 1
-
-
- +
+

See also

+

dummy_cols

+
+
+

Examples

+

+iris = as.data.table(iris)
+iris %>% dummy(Species)
+#> Key: <Sepal.Length, Sepal.Width, Petal.Length, Petal.Width>
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width Species_setosa
+#>             <num>       <num>        <num>       <num>          <num>
+#>   1:          4.3         3.0          1.1         0.1              1
+#>   2:          4.4         2.9          1.4         0.2              1
+#>   3:          4.4         3.0          1.3         0.2              1
+#>   4:          4.4         3.2          1.3         0.2              1
+#>   5:          4.5         2.3          1.3         0.3              1
+#>  ---                                                                 
+#> 146:          7.7         2.6          6.9         2.3              0
+#> 147:          7.7         2.8          6.7         2.0              0
+#> 148:          7.7         3.0          6.1         2.3              0
+#> 149:          7.7         3.8          6.7         2.2              0
+#> 150:          7.9         3.8          6.4         2.0              0
+#>      Species_versicolor Species_virginica
+#>                   <num>             <num>
+#>   1:                  0                 0
+#>   2:                  0                 0
+#>   3:                  0                 0
+#>   4:                  0                 0
+#>   5:                  0                 0
+#>  ---                                     
+#> 146:                  0                 1
+#> 147:                  0                 1
+#> 148:                  0                 1
+#> 149:                  0                 1
+#> 150:                  0                 1
+iris %>% dummy(Species,longname = FALSE)
+#> Key: <Sepal.Length, Sepal.Width, Petal.Length, Petal.Width>
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width setosa versicolor
+#>             <num>       <num>        <num>       <num>  <num>      <num>
+#>   1:          4.3         3.0          1.1         0.1      1          0
+#>   2:          4.4         2.9          1.4         0.2      1          0
+#>   3:          4.4         3.0          1.3         0.2      1          0
+#>   4:          4.4         3.2          1.3         0.2      1          0
+#>   5:          4.5         2.3          1.3         0.3      1          0
+#>  ---                                                                    
+#> 146:          7.7         2.6          6.9         2.3      0          0
+#> 147:          7.7         2.8          6.7         2.0      0          0
+#> 148:          7.7         3.0          6.1         2.3      0          0
+#> 149:          7.7         3.8          6.7         2.2      0          0
+#> 150:          7.9         3.8          6.4         2.0      0          0
+#>      virginica
+#>          <num>
+#>   1:         0
+#>   2:         0
+#>   3:         0
+#>   4:         0
+#>   5:         0
+#>  ---          
+#> 146:         1
+#> 147:         1
+#> 148:         1
+#> 149:         1
+#> 150:         1
+
+mtcars = as.data.table(mtcars)
+mtcars %>% head() %>% dummy(vs,am)
+#> Key: <mpg, cyl, disp, hp, drat, wt, qsec, gear, carb, vs_0, vs_1>
+#>      mpg   cyl  disp    hp  drat    wt  qsec  gear  carb  vs_0  vs_1  am_0
+#>    <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#> 1:  18.1     6   225   105  2.76 3.460 20.22     3     1     0     1     1
+#> 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
+#>    <num>
+#> 1:     0
+#> 2:     0
+#> 3:     1
+#> 4:     1
+#> 5:     0
+#> 6:     1
+mtcars %>% head() %>% dummy("cyl|gear")
+#> Key: <mpg, disp, hp, drat, wt, qsec, vs, am, carb, cyl_4, cyl_6, cyl_8>
+#>      mpg  disp    hp  drat    wt  qsec    vs    am  carb cyl_4 cyl_6 cyl_8
+#>    <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#> 1:  18.1   225   105  2.76 3.460 20.22     1     0     1     0     1     0
+#> 2:  18.7   360   175  3.15 3.440 17.02     0     0     2     0     0     1
+#> 3:  21.0   160   110  3.90 2.620 16.46     0     1     4     0     1     0
+#> 4:  21.0   160   110  3.90 2.875 17.02     0     1     4     0     1     0
+#> 5:  21.4   258   110  3.08 3.215 19.44     1     0     1     0     1     0
+#> 6:  22.8   108    93  3.85 2.320 18.61     1     1     1     1     0     0
+#>    gear_3 gear_4
+#>     <num>  <num>
+#> 1:      1      0
+#> 2:      1      0
+#> 3:      0      1
+#> 4:      0      1
+#> 5:      1      0
+#> 6:      0      1
+
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/fill.html b/docs/reference/fill.html index 83680bc..c38eda6 100644 --- a/docs/reference/fill.html +++ b/docs/reference/fill.html @@ -1,72 +1,12 @@ - - - - - - - -Fill in missing values with previous or next value — fill • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Fill in missing values with previous or next value — fill • tidyft - - + - -
-
- - -
-
+
@@ -134,116 +59,118 @@

Fill in missing values with previous or next value

Fills missing values in selected columns using the next or previous entry.

-
fill(.data, ..., direction = "down")
-
-shift_fill(x, direction = "down")
- -

Arguments

- - - - - - - - - - - - - - - - - - -
.data

A data.table

...

A selection of columns.

direction

Direction in which to fill missing values. -Currently either "down" (the default), "up".

x

A vector.

- -

Value

+
+
fill(.data, ..., direction = "down")
+
+shift_fill(x, direction = "down")
+
-

A filled data.table

-

Details

+
+

Arguments

+ + +
.data
+

A data.table

+ + +
...
+

A selection of columns.

+ + +
direction
+

Direction in which to fill missing values. +Currently either "down" (the default), "up".

+ +
x
+

A vector.

+ +
+
+

Value

+

A filled data.table

+
+
+

Details

fill is filling data.table's columns, shift_fill is filling any vectors.

+
-

Examples

-
-df <- data.table(Month = 1:12, Year = c(2000, rep(NA, 10),2001)) -df
#> Month Year -#> <int> <num> -#> 1: 1 2000 -#> 2: 2 NA -#> 3: 3 NA -#> 4: 4 NA -#> 5: 5 NA -#> 6: 6 NA -#> 7: 7 NA -#> 8: 8 NA -#> 9: 9 NA -#> 10: 10 NA -#> 11: 11 NA -#> 12: 12 2001
df %>% fill(Year)
#> Month Year -#> <int> <num> -#> 1: 1 2000 -#> 2: 2 2000 -#> 3: 3 2000 -#> 4: 4 2000 -#> 5: 5 2000 -#> 6: 6 2000 -#> 7: 7 2000 -#> 8: 8 2000 -#> 9: 9 2000 -#> 10: 10 2000 -#> 11: 11 2000 -#> 12: 12 2001
-df <- data.table(Month = 1:12, Year = c(2000, rep(NA, 10),2001)) -df %>% fill(Year,direction = "up")
#> Month Year -#> <int> <num> -#> 1: 1 2000 -#> 2: 2 2001 -#> 3: 3 2001 -#> 4: 4 2001 -#> 5: 5 2001 -#> 6: 6 2001 -#> 7: 7 2001 -#> 8: 8 2001 -#> 9: 9 2001 -#> 10: 10 2001 -#> 11: 11 2001 -#> 12: 12 2001
-
-
- +
-
- +
+ + - - - + diff --git a/docs/reference/filter.html b/docs/reference/filter.html index ae0be81..e8d10dc 100644 --- a/docs/reference/filter.html +++ b/docs/reference/filter.html @@ -1,72 +1,12 @@ - - - - - - - -Filter entries in data.frame — filter • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Filter entries in data.frame — filter • tidyft - + - - -
-
- - -
-
+
@@ -134,96 +59,100 @@

Filter entries in data.frame

Analogous function for filter in dplyr.

-
filter(.data, ...)
+
+
filter(.data, ...)
+
-

Arguments

- - - - - - - - - - -
.data

data.frame

...

List of variables or name-value pairs of summary/modifications -functions.

+
+

Arguments

-

Value

-

A data.table

-

Details

+
.data
+

data.frame

+ + +
...
+

List of variables or name-value pairs of summary/modifications +functions.

+
+
+

Value

+

A data.table

+
+
+

Details

Currently data.table is not able to delete rows by reference,

-

References

- -

https://github.com/Rdatatable/data.table/issues/635

-

https://stackoverflow.com/questions/10790204/how-to-delete-a-row-by-reference-in-data-table

-

See also

- - - -

Examples

-
iris = as.data.table(iris) -iris %>% filter(Sepal.Length > 7)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 7.1 3.0 5.9 2.1 virginica -#> 2: 7.6 3.0 6.6 2.1 virginica -#> 3: 7.3 2.9 6.3 1.8 virginica -#> 4: 7.2 3.6 6.1 2.5 virginica -#> 5: 7.7 3.8 6.7 2.2 virginica -#> 6: 7.7 2.6 6.9 2.3 virginica -#> 7: 7.7 2.8 6.7 2.0 virginica -#> 8: 7.2 3.2 6.0 1.8 virginica -#> 9: 7.2 3.0 5.8 1.6 virginica -#> 10: 7.4 2.8 6.1 1.9 virginica -#> 11: 7.9 3.8 6.4 2.0 virginica -#> 12: 7.7 3.0 6.1 2.3 virginica
iris %>% filter(Sepal.Length > 7,Sepal.Width > 3)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 7.2 3.6 6.1 2.5 virginica -#> 2: 7.7 3.8 6.7 2.2 virginica -#> 3: 7.2 3.2 6.0 1.8 virginica -#> 4: 7.9 3.8 6.4 2.0 virginica
iris %>% filter(Sepal.Length > 7 & Sepal.Width > 3)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 7.2 3.6 6.1 2.5 virginica -#> 2: 7.7 3.8 6.7 2.2 virginica -#> 3: 7.2 3.2 6.0 1.8 virginica -#> 4: 7.9 3.8 6.4 2.0 virginica
iris %>% filter(Sepal.Length == max(Sepal.Length))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 7.9 3.8 6.4 2 virginica
-
- + +
+

See also

+ +
+
+

Examples

+
iris = as.data.table(iris)
+iris %>% filter(Sepal.Length > 7)
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>            <num>       <num>        <num>       <num>    <fctr>
+#>  1:          7.1         3.0          5.9         2.1 virginica
+#>  2:          7.6         3.0          6.6         2.1 virginica
+#>  3:          7.3         2.9          6.3         1.8 virginica
+#>  4:          7.2         3.6          6.1         2.5 virginica
+#>  5:          7.7         3.8          6.7         2.2 virginica
+#>  6:          7.7         2.6          6.9         2.3 virginica
+#>  7:          7.7         2.8          6.7         2.0 virginica
+#>  8:          7.2         3.2          6.0         1.8 virginica
+#>  9:          7.2         3.0          5.8         1.6 virginica
+#> 10:          7.4         2.8          6.1         1.9 virginica
+#> 11:          7.9         3.8          6.4         2.0 virginica
+#> 12:          7.7         3.0          6.1         2.3 virginica
+iris %>% filter(Sepal.Length > 7,Sepal.Width > 3)
+#>    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>           <num>       <num>        <num>       <num>    <fctr>
+#> 1:          7.2         3.6          6.1         2.5 virginica
+#> 2:          7.7         3.8          6.7         2.2 virginica
+#> 3:          7.2         3.2          6.0         1.8 virginica
+#> 4:          7.9         3.8          6.4         2.0 virginica
+iris %>% filter(Sepal.Length > 7 & Sepal.Width > 3)
+#>    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>           <num>       <num>        <num>       <num>    <fctr>
+#> 1:          7.2         3.6          6.1         2.5 virginica
+#> 2:          7.7         3.8          6.7         2.2 virginica
+#> 3:          7.2         3.2          6.0         1.8 virginica
+#> 4:          7.9         3.8          6.4         2.0 virginica
+iris %>% filter(Sepal.Length == max(Sepal.Length))
+#>    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>           <num>       <num>        <num>       <num>    <fctr>
+#> 1:          7.9         3.8          6.4           2 virginica
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/fst.html b/docs/reference/fst.html index d973203..76cc349 100644 --- a/docs/reference/fst.html +++ b/docs/reference/fst.html @@ -1,72 +1,12 @@ - - - - - - - -Parse,inspect and extract data.table from fst file — fst • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Parse,inspect and extract data.table from fst file — fst • tidyft - + - - -
-
- - -
-
+
@@ -134,366 +59,403 @@

Parse,inspect and extract data.table from fst file

An API for reading fst file as data.table.

-
parse_fst(path)
+    
+
parse_fst(path)
+
+slice_fst(ft, row_no)
+
+select_fst(ft, ...)
+
+filter_fst(ft, ...)
+
+summary_fst(ft)
+
+ +
+

Arguments

+ -slice_fst(ft, row_no) +
path
+

path to fst file

-select_fst(ft, ...) -filter_fst(ft, ...) +
ft
+

An object of class fst_table, returned by parse_fst

-summary_fst(ft)
-

Arguments

- - - - - - - - - - - - - - - - - - -
path

path to fst file

ft

An object of class fst_table, returned by parse_fst

row_no

An integer vector (Positive)

...

The filter conditions

+
row_no
+

An integer vector (Positive)

-

Value

+
...
+

The filter conditions

+ +
+
+

Value

parse_fst returns a fst_table class.

select_fst and filter_fst returns a data.table.

-

Details

- +
+
+

Details

summary_fst could provide some basic information about the fst table.

-

See also

- - - -

Examples

-
- # 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
#> <fst file> -#> 150 rows, 5 columns (filea5434c14328.fst) -#> -#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <double> <double> <double> <double> <factor> -#> 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)
#> <fst file> -#> 150 rows, 5 columns (filea5434c14328.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 -#> <num> <num> <num> <num> <fctr> -#> 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 -#> <num> <num> <num> <num> <fctr> -#> 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 -#> <num> -#> 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 -#> <num> <num> -#> 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 -#> <num> -#> 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(1:3)
#> Sepal.Length Sepal.Width Petal.Length -#> <num> <num> <num> -#> 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 -#> --- -#> 146: 6.7 3.0 5.2 -#> 147: 6.3 2.5 5.0 -#> 148: 6.5 3.0 5.2 -#> 149: 6.2 3.4 5.4 -#> 150: 5.9 3.0 5.1
ft %>% select_fst(1,3)
#> Sepal.Length Petal.Length -#> <num> <num> -#> 1: 5.1 1.4 -#> 2: 4.9 1.4 -#> 3: 4.7 1.3 -#> 4: 4.6 1.5 -#> 5: 5.0 1.4 -#> --- -#> 146: 6.7 5.2 -#> 147: 6.3 5.0 -#> 148: 6.5 5.2 -#> 149: 6.2 5.4 -#> 150: 5.9 5.1
ft %>% select_fst("Se")
#> Sepal.Length Sepal.Width -#> <num> <num> -#> 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
- # return a warning with message - # \donttest{ - ft %>% select_fst("nothing")
#> Warning: No matched columns,try other patterns. Names of the `fst_table` are listed.
#> [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
# } - - ft %>% select_fst("Se|Sp")
#> Sepal.Length Sepal.Width Species -#> <num> <num> <fctr> -#> 1: 5.1 3.5 setosa -#> 2: 4.9 3.0 setosa -#> 3: 4.7 3.2 setosa -#> 4: 4.6 3.1 setosa -#> 5: 5.0 3.6 setosa -#> --- -#> 146: 6.7 3.0 virginica -#> 147: 6.3 2.5 virginica -#> 148: 6.5 3.0 virginica -#> 149: 6.2 3.4 virginica -#> 150: 5.9 3.0 virginica
ft %>% select_fst(cols = names(iris)[2:3])
#> Sepal.Width Petal.Length -#> <num> <num> -#> 1: 3.5 1.4 -#> 2: 3.0 1.4 -#> 3: 3.2 1.3 -#> 4: 3.1 1.5 -#> 5: 3.6 1.4 -#> --- -#> 146: 3.0 5.2 -#> 147: 2.5 5.0 -#> 148: 3.0 5.2 -#> 149: 3.4 5.4 -#> 150: 3.0 5.1
- ft %>% filter_fst(Sepal.Width > 3)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 5.1 3.5 1.4 0.2 setosa -#> 2: 4.7 3.2 1.3 0.2 setosa -#> 3: 4.6 3.1 1.5 0.2 setosa -#> 4: 5.0 3.6 1.4 0.2 setosa -#> 5: 5.4 3.9 1.7 0.4 setosa -#> 6: 4.6 3.4 1.4 0.3 setosa -#> 7: 5.0 3.4 1.5 0.2 setosa -#> 8: 4.9 3.1 1.5 0.1 setosa -#> 9: 5.4 3.7 1.5 0.2 setosa -#> 10: 4.8 3.4 1.6 0.2 setosa -#> 11: 5.8 4.0 1.2 0.2 setosa -#> 12: 5.7 4.4 1.5 0.4 setosa -#> 13: 5.4 3.9 1.3 0.4 setosa -#> 14: 5.1 3.5 1.4 0.3 setosa -#> 15: 5.7 3.8 1.7 0.3 setosa -#> 16: 5.1 3.8 1.5 0.3 setosa -#> 17: 5.4 3.4 1.7 0.2 setosa -#> 18: 5.1 3.7 1.5 0.4 setosa -#> 19: 4.6 3.6 1.0 0.2 setosa -#> 20: 5.1 3.3 1.7 0.5 setosa -#> 21: 4.8 3.4 1.9 0.2 setosa -#> 22: 5.0 3.4 1.6 0.4 setosa -#> 23: 5.2 3.5 1.5 0.2 setosa -#> 24: 5.2 3.4 1.4 0.2 setosa -#> 25: 4.7 3.2 1.6 0.2 setosa -#> 26: 4.8 3.1 1.6 0.2 setosa -#> 27: 5.4 3.4 1.5 0.4 setosa -#> 28: 5.2 4.1 1.5 0.1 setosa -#> 29: 5.5 4.2 1.4 0.2 setosa -#> 30: 4.9 3.1 1.5 0.2 setosa -#> 31: 5.0 3.2 1.2 0.2 setosa -#> 32: 5.5 3.5 1.3 0.2 setosa -#> 33: 4.9 3.6 1.4 0.1 setosa -#> 34: 5.1 3.4 1.5 0.2 setosa -#> 35: 5.0 3.5 1.3 0.3 setosa -#> 36: 4.4 3.2 1.3 0.2 setosa -#> 37: 5.0 3.5 1.6 0.6 setosa -#> 38: 5.1 3.8 1.9 0.4 setosa -#> 39: 5.1 3.8 1.6 0.2 setosa -#> 40: 4.6 3.2 1.4 0.2 setosa -#> 41: 5.3 3.7 1.5 0.2 setosa -#> 42: 5.0 3.3 1.4 0.2 setosa -#> 43: 7.0 3.2 4.7 1.4 versicolor -#> 44: 6.4 3.2 4.5 1.5 versicolor -#> 45: 6.9 3.1 4.9 1.5 versicolor -#> 46: 6.3 3.3 4.7 1.6 versicolor -#> 47: 6.7 3.1 4.4 1.4 versicolor -#> 48: 5.9 3.2 4.8 1.8 versicolor -#> 49: 6.0 3.4 4.5 1.6 versicolor -#> 50: 6.7 3.1 4.7 1.5 versicolor -#> 51: 6.3 3.3 6.0 2.5 virginica -#> 52: 7.2 3.6 6.1 2.5 virginica -#> 53: 6.5 3.2 5.1 2.0 virginica -#> 54: 6.4 3.2 5.3 2.3 virginica -#> 55: 7.7 3.8 6.7 2.2 virginica -#> 56: 6.9 3.2 5.7 2.3 virginica -#> 57: 6.7 3.3 5.7 2.1 virginica -#> 58: 7.2 3.2 6.0 1.8 virginica -#> 59: 7.9 3.8 6.4 2.0 virginica -#> 60: 6.3 3.4 5.6 2.4 virginica -#> 61: 6.4 3.1 5.5 1.8 virginica -#> 62: 6.9 3.1 5.4 2.1 virginica -#> 63: 6.7 3.1 5.6 2.4 virginica -#> 64: 6.9 3.1 5.1 2.3 virginica -#> 65: 6.8 3.2 5.9 2.3 virginica -#> 66: 6.7 3.3 5.7 2.5 virginica -#> 67: 6.2 3.4 5.4 2.3 virginica -#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
ft %>% filter_fst(Sepal.Length > 6 , Species == "virginica")
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 6.3 3.3 6.0 2.5 virginica -#> 2: 7.1 3.0 5.9 2.1 virginica -#> 3: 6.3 2.9 5.6 1.8 virginica -#> 4: 6.5 3.0 5.8 2.2 virginica -#> 5: 7.6 3.0 6.6 2.1 virginica -#> 6: 7.3 2.9 6.3 1.8 virginica -#> 7: 6.7 2.5 5.8 1.8 virginica -#> 8: 7.2 3.6 6.1 2.5 virginica -#> 9: 6.5 3.2 5.1 2.0 virginica -#> 10: 6.4 2.7 5.3 1.9 virginica -#> 11: 6.8 3.0 5.5 2.1 virginica -#> 12: 6.4 3.2 5.3 2.3 virginica -#> 13: 6.5 3.0 5.5 1.8 virginica -#> 14: 7.7 3.8 6.7 2.2 virginica -#> 15: 7.7 2.6 6.9 2.3 virginica -#> 16: 6.9 3.2 5.7 2.3 virginica -#> 17: 7.7 2.8 6.7 2.0 virginica -#> 18: 6.3 2.7 4.9 1.8 virginica -#> 19: 6.7 3.3 5.7 2.1 virginica -#> 20: 7.2 3.2 6.0 1.8 virginica -#> 21: 6.2 2.8 4.8 1.8 virginica -#> 22: 6.1 3.0 4.9 1.8 virginica -#> 23: 6.4 2.8 5.6 2.1 virginica -#> 24: 7.2 3.0 5.8 1.6 virginica -#> 25: 7.4 2.8 6.1 1.9 virginica -#> 26: 7.9 3.8 6.4 2.0 virginica -#> 27: 6.4 2.8 5.6 2.2 virginica -#> 28: 6.3 2.8 5.1 1.5 virginica -#> 29: 6.1 2.6 5.6 1.4 virginica -#> 30: 7.7 3.0 6.1 2.3 virginica -#> 31: 6.3 3.4 5.6 2.4 virginica -#> 32: 6.4 3.1 5.5 1.8 virginica -#> 33: 6.9 3.1 5.4 2.1 virginica -#> 34: 6.7 3.1 5.6 2.4 virginica -#> 35: 6.9 3.1 5.1 2.3 virginica -#> 36: 6.8 3.2 5.9 2.3 virginica -#> 37: 6.7 3.3 5.7 2.5 virginica -#> 38: 6.7 3.0 5.2 2.3 virginica -#> 39: 6.3 2.5 5.0 1.9 virginica -#> 40: 6.5 3.0 5.2 2.0 virginica -#> 41: 6.2 3.4 5.4 2.3 virginica -#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
ft %>% filter_fst(Sepal.Length > 6 & Species == "virginica" & Sepal.Width < 3)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 6.3 2.9 5.6 1.8 virginica -#> 2: 7.3 2.9 6.3 1.8 virginica -#> 3: 6.7 2.5 5.8 1.8 virginica -#> 4: 6.4 2.7 5.3 1.9 virginica -#> 5: 7.7 2.6 6.9 2.3 virginica -#> 6: 7.7 2.8 6.7 2.0 virginica -#> 7: 6.3 2.7 4.9 1.8 virginica -#> 8: 6.2 2.8 4.8 1.8 virginica -#> 9: 6.4 2.8 5.6 2.1 virginica -#> 10: 7.4 2.8 6.1 1.9 virginica -#> 11: 6.4 2.8 5.6 2.2 virginica -#> 12: 6.3 2.8 5.1 1.5 virginica -#> 13: 6.1 2.6 5.6 1.4 virginica -#> 14: 6.3 2.5 5.0 1.9 virginica
-
-
- +
+

See also

+ +
+
+

Examples

+

+  # 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
+#> <fst file>
+#> 150 rows, 5 columns (file27604466d41.fst)
+#> 
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>         <double>    <double>     <double>    <double>  <factor>
+#> 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)
+#> <fst file>
+#> 150 rows, 5 columns (file27604466d41.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
+#>           <num>       <num>        <num>       <num>  <fctr>
+#> 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
+#>           <num>       <num>        <num>       <num>  <fctr>
+#> 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
+#>             <num>
+#>   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
+#>             <num>       <num>
+#>   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
+#>             <num>
+#>   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(1:3)
+#>      Sepal.Length Sepal.Width Petal.Length
+#>             <num>       <num>        <num>
+#>   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
+#>  ---                                      
+#> 146:          6.7         3.0          5.2
+#> 147:          6.3         2.5          5.0
+#> 148:          6.5         3.0          5.2
+#> 149:          6.2         3.4          5.4
+#> 150:          5.9         3.0          5.1
+  ft %>% select_fst(1,3)
+#>      Sepal.Length Petal.Length
+#>             <num>        <num>
+#>   1:          5.1          1.4
+#>   2:          4.9          1.4
+#>   3:          4.7          1.3
+#>   4:          4.6          1.5
+#>   5:          5.0          1.4
+#>  ---                          
+#> 146:          6.7          5.2
+#> 147:          6.3          5.0
+#> 148:          6.5          5.2
+#> 149:          6.2          5.4
+#> 150:          5.9          5.1
+  ft %>% select_fst("Se")
+#>      Sepal.Length Sepal.Width
+#>             <num>       <num>
+#>   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
+
+  # return a warning with message
+  # \donttest{
+    ft %>% select_fst("nothing")
+#> Warning: No matched columns,try other patterns. Names of the `fst_table` are listed.
+#> [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     
+  # }
+
+  ft %>% select_fst("Se|Sp")
+#>      Sepal.Length Sepal.Width   Species
+#>             <num>       <num>    <fctr>
+#>   1:          5.1         3.5    setosa
+#>   2:          4.9         3.0    setosa
+#>   3:          4.7         3.2    setosa
+#>   4:          4.6         3.1    setosa
+#>   5:          5.0         3.6    setosa
+#>  ---                                   
+#> 146:          6.7         3.0 virginica
+#> 147:          6.3         2.5 virginica
+#> 148:          6.5         3.0 virginica
+#> 149:          6.2         3.4 virginica
+#> 150:          5.9         3.0 virginica
+  ft %>% select_fst(cols = names(iris)[2:3])
+#>      Sepal.Width Petal.Length
+#>            <num>        <num>
+#>   1:         3.5          1.4
+#>   2:         3.0          1.4
+#>   3:         3.2          1.3
+#>   4:         3.1          1.5
+#>   5:         3.6          1.4
+#>  ---                         
+#> 146:         3.0          5.2
+#> 147:         2.5          5.0
+#> 148:         3.0          5.2
+#> 149:         3.4          5.4
+#> 150:         3.0          5.1
+
+  ft %>% filter_fst(Sepal.Width > 3)
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
+#>            <num>       <num>        <num>       <num>     <fctr>
+#>  1:          5.1         3.5          1.4         0.2     setosa
+#>  2:          4.7         3.2          1.3         0.2     setosa
+#>  3:          4.6         3.1          1.5         0.2     setosa
+#>  4:          5.0         3.6          1.4         0.2     setosa
+#>  5:          5.4         3.9          1.7         0.4     setosa
+#>  6:          4.6         3.4          1.4         0.3     setosa
+#>  7:          5.0         3.4          1.5         0.2     setosa
+#>  8:          4.9         3.1          1.5         0.1     setosa
+#>  9:          5.4         3.7          1.5         0.2     setosa
+#> 10:          4.8         3.4          1.6         0.2     setosa
+#> 11:          5.8         4.0          1.2         0.2     setosa
+#> 12:          5.7         4.4          1.5         0.4     setosa
+#> 13:          5.4         3.9          1.3         0.4     setosa
+#> 14:          5.1         3.5          1.4         0.3     setosa
+#> 15:          5.7         3.8          1.7         0.3     setosa
+#> 16:          5.1         3.8          1.5         0.3     setosa
+#> 17:          5.4         3.4          1.7         0.2     setosa
+#> 18:          5.1         3.7          1.5         0.4     setosa
+#> 19:          4.6         3.6          1.0         0.2     setosa
+#> 20:          5.1         3.3          1.7         0.5     setosa
+#> 21:          4.8         3.4          1.9         0.2     setosa
+#> 22:          5.0         3.4          1.6         0.4     setosa
+#> 23:          5.2         3.5          1.5         0.2     setosa
+#> 24:          5.2         3.4          1.4         0.2     setosa
+#> 25:          4.7         3.2          1.6         0.2     setosa
+#> 26:          4.8         3.1          1.6         0.2     setosa
+#> 27:          5.4         3.4          1.5         0.4     setosa
+#> 28:          5.2         4.1          1.5         0.1     setosa
+#> 29:          5.5         4.2          1.4         0.2     setosa
+#> 30:          4.9         3.1          1.5         0.2     setosa
+#> 31:          5.0         3.2          1.2         0.2     setosa
+#> 32:          5.5         3.5          1.3         0.2     setosa
+#> 33:          4.9         3.6          1.4         0.1     setosa
+#> 34:          5.1         3.4          1.5         0.2     setosa
+#> 35:          5.0         3.5          1.3         0.3     setosa
+#> 36:          4.4         3.2          1.3         0.2     setosa
+#> 37:          5.0         3.5          1.6         0.6     setosa
+#> 38:          5.1         3.8          1.9         0.4     setosa
+#> 39:          5.1         3.8          1.6         0.2     setosa
+#> 40:          4.6         3.2          1.4         0.2     setosa
+#> 41:          5.3         3.7          1.5         0.2     setosa
+#> 42:          5.0         3.3          1.4         0.2     setosa
+#> 43:          7.0         3.2          4.7         1.4 versicolor
+#> 44:          6.4         3.2          4.5         1.5 versicolor
+#> 45:          6.9         3.1          4.9         1.5 versicolor
+#> 46:          6.3         3.3          4.7         1.6 versicolor
+#> 47:          6.7         3.1          4.4         1.4 versicolor
+#> 48:          5.9         3.2          4.8         1.8 versicolor
+#> 49:          6.0         3.4          4.5         1.6 versicolor
+#> 50:          6.7         3.1          4.7         1.5 versicolor
+#> 51:          6.3         3.3          6.0         2.5  virginica
+#> 52:          7.2         3.6          6.1         2.5  virginica
+#> 53:          6.5         3.2          5.1         2.0  virginica
+#> 54:          6.4         3.2          5.3         2.3  virginica
+#> 55:          7.7         3.8          6.7         2.2  virginica
+#> 56:          6.9         3.2          5.7         2.3  virginica
+#> 57:          6.7         3.3          5.7         2.1  virginica
+#> 58:          7.2         3.2          6.0         1.8  virginica
+#> 59:          7.9         3.8          6.4         2.0  virginica
+#> 60:          6.3         3.4          5.6         2.4  virginica
+#> 61:          6.4         3.1          5.5         1.8  virginica
+#> 62:          6.9         3.1          5.4         2.1  virginica
+#> 63:          6.7         3.1          5.6         2.4  virginica
+#> 64:          6.9         3.1          5.1         2.3  virginica
+#> 65:          6.8         3.2          5.9         2.3  virginica
+#> 66:          6.7         3.3          5.7         2.5  virginica
+#> 67:          6.2         3.4          5.4         2.3  virginica
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
+  ft %>% filter_fst(Sepal.Length > 6 , Species == "virginica")
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>            <num>       <num>        <num>       <num>    <fctr>
+#>  1:          6.3         3.3          6.0         2.5 virginica
+#>  2:          7.1         3.0          5.9         2.1 virginica
+#>  3:          6.3         2.9          5.6         1.8 virginica
+#>  4:          6.5         3.0          5.8         2.2 virginica
+#>  5:          7.6         3.0          6.6         2.1 virginica
+#>  6:          7.3         2.9          6.3         1.8 virginica
+#>  7:          6.7         2.5          5.8         1.8 virginica
+#>  8:          7.2         3.6          6.1         2.5 virginica
+#>  9:          6.5         3.2          5.1         2.0 virginica
+#> 10:          6.4         2.7          5.3         1.9 virginica
+#> 11:          6.8         3.0          5.5         2.1 virginica
+#> 12:          6.4         3.2          5.3         2.3 virginica
+#> 13:          6.5         3.0          5.5         1.8 virginica
+#> 14:          7.7         3.8          6.7         2.2 virginica
+#> 15:          7.7         2.6          6.9         2.3 virginica
+#> 16:          6.9         3.2          5.7         2.3 virginica
+#> 17:          7.7         2.8          6.7         2.0 virginica
+#> 18:          6.3         2.7          4.9         1.8 virginica
+#> 19:          6.7         3.3          5.7         2.1 virginica
+#> 20:          7.2         3.2          6.0         1.8 virginica
+#> 21:          6.2         2.8          4.8         1.8 virginica
+#> 22:          6.1         3.0          4.9         1.8 virginica
+#> 23:          6.4         2.8          5.6         2.1 virginica
+#> 24:          7.2         3.0          5.8         1.6 virginica
+#> 25:          7.4         2.8          6.1         1.9 virginica
+#> 26:          7.9         3.8          6.4         2.0 virginica
+#> 27:          6.4         2.8          5.6         2.2 virginica
+#> 28:          6.3         2.8          5.1         1.5 virginica
+#> 29:          6.1         2.6          5.6         1.4 virginica
+#> 30:          7.7         3.0          6.1         2.3 virginica
+#> 31:          6.3         3.4          5.6         2.4 virginica
+#> 32:          6.4         3.1          5.5         1.8 virginica
+#> 33:          6.9         3.1          5.4         2.1 virginica
+#> 34:          6.7         3.1          5.6         2.4 virginica
+#> 35:          6.9         3.1          5.1         2.3 virginica
+#> 36:          6.8         3.2          5.9         2.3 virginica
+#> 37:          6.7         3.3          5.7         2.5 virginica
+#> 38:          6.7         3.0          5.2         2.3 virginica
+#> 39:          6.3         2.5          5.0         1.9 virginica
+#> 40:          6.5         3.0          5.2         2.0 virginica
+#> 41:          6.2         3.4          5.4         2.3 virginica
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+  ft %>% filter_fst(Sepal.Length > 6 & Species == "virginica" & Sepal.Width < 3)
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>            <num>       <num>        <num>       <num>    <fctr>
+#>  1:          6.3         2.9          5.6         1.8 virginica
+#>  2:          7.3         2.9          6.3         1.8 virginica
+#>  3:          6.7         2.5          5.8         1.8 virginica
+#>  4:          6.4         2.7          5.3         1.9 virginica
+#>  5:          7.7         2.6          6.9         2.3 virginica
+#>  6:          7.7         2.8          6.7         2.0 virginica
+#>  7:          6.3         2.7          4.9         1.8 virginica
+#>  8:          6.2         2.8          4.8         1.8 virginica
+#>  9:          6.4         2.8          5.6         2.1 virginica
+#> 10:          7.4         2.8          6.1         1.9 virginica
+#> 11:          6.4         2.8          5.6         2.2 virginica
+#> 12:          6.3         2.8          5.1         1.5 virginica
+#> 13:          6.1         2.6          5.6         1.4 virginica
+#> 14:          6.3         2.5          5.0         1.9 virginica
+
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/fst_io.html b/docs/reference/fst_io.html index 68160fc..0b22969 100644 --- a/docs/reference/fst_io.html +++ b/docs/reference/fst_io.html @@ -1,74 +1,14 @@ - - - - - - - -Read and write fst files — export_fst • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Read and write fst files — export_fst • tidyft - - - - - - - - - - - - + - - -
-
- - -
-
+
-

Wrapper for read_fst and write_fst +

Wrapper for read_fst and write_fst from fst, but use a different default. For data import, always return a data.table. For data export, always compress the data to the smallest size.

-
export_fst(x, path, compress = 100, uniform_encoding = TRUE)
-
-import_fst(
-  path,
-  columns = NULL,
-  from = 1,
-  to = NULL,
-  as.data.table = TRUE,
-  old_format = FALSE
-)
- -

Arguments

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
x

a data frame to write to disk

path

path to fst file

compress

value in the range 0 to 100, indicating the amount of compression to use. -Lower values mean larger file sizes. The default compression is set to 50.

uniform_encoding

If `TRUE`, all character vectors will be assumed to have elements with equal encoding. +

+
export_fst(x, path, compress = 100, uniform_encoding = TRUE)
+
+import_fst(
+  path,
+  columns = NULL,
+  from = 1,
+  to = NULL,
+  as.data.table = TRUE,
+  old_format = FALSE
+)
+
+ +
+

Arguments

+ + +
x
+

a data frame to write to disk

+ + +
path
+

path to fst file

+ + +
compress
+

value in the range 0 to 100, indicating the amount of compression to use. +Lower values mean larger file sizes. The default compression is set to 50.

+ + +
uniform_encoding
+

If `TRUE`, all character vectors will be assumed to have elements with equal encoding. The encoding (latin1, UTF8 or native) of the first non-NA element will used as encoding for the whole column. This will be a correct assumption for most use cases. If `uniform.encoding` is set to `FALSE`, no such assumption will be made and all elements will be converted to the same encoding. The latter is a relatively expensive operation and will reduce write performance for -character columns.

columns

Column names to read. The default is to read all columns.

from

Read data starting from this row number.

to

Read data up until this row number. The default is to read to the last row of the stored dataset.

as.data.table

If TRUE, the result will be returned as a data.table object. Any keys set on +character columns.

+ + +
columns
+

Column names to read. The default is to read all columns.

+ + +
from
+

Read data starting from this row number.

+ + +
to
+

Read data up until this row number. The default is to read to the last row of the stored dataset.

+ + +
as.data.table
+

If TRUE, the result will be returned as a data.table object. Any keys set on dataset x before writing will be retained. This allows for storage of sorted datasets. This option -requires data.table package to be installed.

old_format

must be FALSE, the old fst file format is deprecated and can only be read and -converted with fst package versions 0.8.0 to 0.8.10.

+requires data.table package to be installed.

+ -

Value

+
old_format
+

must be FALSE, the old fst file format is deprecated and can only be read and +converted with fst package versions 0.8.0 to 0.8.10.

+
+
+

Value

`import_fst` returns a data.table with the selected columns and rows. `export_fst` writes `x` to a `fst` file and invisibly returns `x` (so you can use this function in a pipeline).

-

See also

- - - -

Examples

-
-# \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 -#> <num> <num> <num> <num> <fctr> -#> 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") -# }
-
- +
+

See also

+ +
+
+

Examples

+

+# \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
+#>             <num>       <num>        <num>       <num>    <fctr>
+#>   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")
+# }
+
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/group.html b/docs/reference/group.html index 2007379..ab48966 100644 --- a/docs/reference/group.html +++ b/docs/reference/group.html @@ -1,76 +1,16 @@ - - - - - - - -Group by one or more variables — group_by • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Group by one or more variables — group_by • tidyft - - - - - - - - - - - + - - - -
-
- - -
-
+
@@ -142,105 +67,109 @@

Group by one or more variables

returned by group_by.

-
group_by(.data, ...)
+    
+
group_by(.data, ...)
+
+group_exe(.data, ...)
+
+groups(x)
+
+ungroup(x)
+
+ +
+

Arguments

-group_exe(.data, ...) -groups(x) +
.data
+

A data.table

-ungroup(x)
-

Arguments

- - - - - - - - - - - - - - -
.data

A data.table

...

For group_by:Variables to group by. +

...
+

For group_by:Variables to group by. For group_exe:Any data manipulation arguments that - could be implemented on a data.table.

x

A data.table

+ could be implemented on a data.table.

-

Value

-

A data.table with keys

-

Details

+
x
+

A data.table

+
+
+

Value

+

A data.table with keys

+
+
+

Details

For mutate and summarise, it is recommended to use the innate "by" parameter, which is faster. Once the data.table is grouped, the order is changed forever.

groups() could return a character vector of specified groups.

ungroup() would delete the keys in data.table.

+
-

Examples

-
a = as.data.table(iris) -a
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 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> -#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width -#> <fctr> <num> <num> <num> <num> -#> 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
-
- +
-
+ + - - - + diff --git a/docs/reference/index.html b/docs/reference/index.html index 7c92e12..bf7deb3 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -1,71 +1,12 @@ - - - - - - - -Function reference • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Package index • tidyft - + - - -
-
- - -
-
+
- - - - - - - - - - -
-

All functions

+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - -
+

All functions

+

arrange()

Arrange entries in data.frame

+

as_fst()

Save a data.frame as a fst table

+

complete()

Complete a data frame with missing combinations of data

+

count() add_count()

Count observations by group

+

cummean()

Cumulative mean

+

distinct()

Select distinct/unique rows in data.table

+

drop_na() delete_na()

Drop or delete data by rows or columns

+

dummy()

Fast creation of dummy variables

+

fill() shift_fill()

Fill in missing values with previous or next value

+

filter()

Filter entries in data.frame

+

parse_fst() slice_fst() select_fst() filter_fst() summary_fst()

Parse,inspect and extract data.table from fst file

+

export_fst() import_fst()

Read and write fst files

+

group_by() group_exe() groups() ungroup()

Group by one or more variables

+

inner_join() left_join() right_join() full_join() anti_join() semi_join()

Join tables

+

lead() lag()

Fast lead/lag for vectors

+

longer() wider()

Pivot data between long and wide

+

mutate() transmute() mutate_when() mutate_vars()

Create or transform variables

+

nest() unnest() squeeze() chop() unchop()

Nest and unnest

+

nth()

Extract the nth value from a vector

+

object_size()

Nice printing of report the Space Allocated for an Object

+

pull()

Pull out a single variable

+

read_csv()

Convenient file reader

+

relocate()

Change column order

+

replace_vars()

Fast value replacement in data frame

+

rowwise_mutate() rowwise_summarise()

Computation by rows

+

select() select_vars() select_dt() select_mix() rename()

Select/rename variables by name

+

separate()

Separate a character column into two columns using -a regular expression separator

+

Separate a character column into two columns using a regular expression separator

slice() slice_head() slice_tail() slice_max() slice_min() slice_sample()

Subset rows using their positions

+

summarise() summarise_when() summarise_vars()

Summarise columns to single values

+

sys_time_print()

Convenient print of time taken

+

mat_df() df_mat()

Conversion between tidy table and named matrix

+

uncount()

"Uncount" a data frame

+

unite()

Unite multiple columns into one by pasting strings together

+

utf8_encoding()

Use UTF-8 for character encoding in a data frame

- +
- +
-
- +
+ + - - - + diff --git a/docs/reference/join.html b/docs/reference/join.html index e6d2b1c..af593d6 100644 --- a/docs/reference/join.html +++ b/docs/reference/join.html @@ -1,49 +1,5 @@ - - - - - - - -Join tables — inner_join • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Join tables — inner_join • tidyft - - - - - - - - - - - + - - - -
-
- - -
-
+
@@ -152,32 +77,34 @@

Join tables

* `anti_join()` return all rows from `x` without a match in `y`.

-
inner_join(x, y, by = NULL, on = NULL)
+    
+
inner_join(x, y, by = NULL, on = NULL)
+
+left_join(x, y, by = NULL, on = NULL)
+
+right_join(x, y, by = NULL, on = NULL)
+
+full_join(x, y, by = NULL, on = NULL)
+
+anti_join(x, y, by = NULL, on = NULL)
+
+semi_join(x, y, by = NULL, on = NULL)
+
+ +
+

Arguments

-left_join(x, y, by = NULL, on = NULL) -right_join(x, y, by = NULL, on = NULL) +
x
+

A data.table

-full_join(x, y, by = NULL, on = NULL) -anti_join(x, y, by = NULL, on = NULL) +
y
+

A data.table

-semi_join(x, y, by = NULL, on = NULL)
-

Arguments

- - - - - - - - - - - - - - - - - - -
x

A data.table

y

A data.table

by

(Optional) A character vector of variables to join by.

+
by
+

(Optional) A character vector of variables to join by.

If `NULL`, the default, `*_join()` will perform a natural join, using all variables in common across `x` and `y`. A message lists the variables so that you can check they're correct; suppress the message by supplying `by` explicitly.

@@ -187,11 +114,11 @@

Arg For example, `by = c("a", "b")` will match `x$a` to `y$a` and `x$b` to `y$b`. Use a named vector to match different variables in `x` and `y`. For example, `by = c("a" = "b", "c" = "d")` will match `x$a` to `y$b` and - `x$c` to `y$d`.

on

(Optional) + `x$c` to `y$d`.

+ + +
on
+

(Optional) Indicate which columns in x should be joined with which columns in y. Examples included: 1..by = c("a","b") (this is a must for set_full_join); @@ -199,108 +126,126 @@

Arg 3..by = c("x1==y1", "x2==y2"); 4..by = c("a", V2="b"); 5..by = .(a, b); - 6..by = c("x>=a", "y<=b") or .by = .(x>=a, y<=b).

- -

Value

+ 6..by = c("x>=a", "y<=b") or .by = .(x>=a, y<=b).

+
+
+

Value

A data.table

+
-

Examples

-
-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> -#> name company position -#> <char> <char> <char> -#> 1: Daniela Ajax engineer -#> 2: John Ajax designer
workers %>% left_join(positions)
#> Joining by: name
#> Key: <name> -#> name company position -#> <char> <char> <char> -#> 1: Daniela Ajax engineer -#> 2: John Ajax designer -#> 3: Nick Acme <NA>
workers %>% right_join(positions)
#> Joining by: name
#> Key: <name> -#> name company position -#> <char> <char> <char> -#> 1: Cathie <NA> manager -#> 2: Daniela Ajax engineer -#> 3: John Ajax designer
workers %>% full_join(positions)
#> Joining by: name
#> Key: <name> -#> name company position -#> <char> <char> <char> -#> 1: Cathie <NA> manager -#> 2: Daniela Ajax engineer -#> 3: John Ajax designer -#> 4: Nick Acme <NA>
-# filtering joins -workers %>% anti_join(positions)
#> Joining by: name
#> name company -#> <char> <char> -#> 1: Nick Acme
workers %>% semi_join(positions)
#> Joining by: name
#> name company -#> <char> <char> -#> 1: John Ajax -#> 2: Daniela Ajax
-# To suppress the message, supply 'by' argument -workers %>% left_join(positions, by = "name")
#> Key: <name> -#> name company position -#> <char> <char> <char> -#> 1: Daniela Ajax engineer -#> 2: John Ajax designer -#> 3: Nick Acme <NA>
-# 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> -#> name company position -#> <char> <char> <char> -#> 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 -#> <char> <char> <char> -#> 1: John Ajax designer -#> 2: Daniela Ajax engineer
- -
-
- +
-
+ + - - - + diff --git a/docs/reference/lag_lead.html b/docs/reference/lag_lead.html index 2cb83bc..caaa5cc 100644 --- a/docs/reference/lag_lead.html +++ b/docs/reference/lag_lead.html @@ -1,73 +1,13 @@ - - - - - - - -Fast lead/lag for vectors — lead • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Fast lead/lag for vectors — lead • tidyft - - + - -
-
- - -
-
+
@@ -136,68 +61,72 @@

Fast lead/lag for vectors

wrapping data.table's shift.

-
lead(x, n = 1L, fill = NA)
-
-lag(x, n = 1L, fill = NA)
- -

Arguments

- - - - - - - - - - - - - - -
x

A vector

n

a positive integer of length 1, -giving the number of positions to lead or lag by. Default uses 1

fill

Value to use for padding when the window goes beyond the input length. -Default uses NA

- -

Value

+
+
lead(x, n = 1L, fill = NA)
+
+lag(x, n = 1L, fill = NA)
+
-

A vector

-

See also

+
+

Arguments

- -

Examples

-
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
-
- +
+

Value

+

A vector

+
+
+

See also

+ +
+ +
+

Examples

+
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
+
+
+
-
+ + - - - + diff --git a/docs/reference/long_wide.html b/docs/reference/long_wide.html index d928bdb..6f142ba 100644 --- a/docs/reference/long_wide.html +++ b/docs/reference/long_wide.html @@ -1,74 +1,14 @@ - - - - - - - -Pivot data between long and wide — longer • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Pivot data between long and wide — longer • tidyft - - - - - - - - - - - + - - - -
-
- - -
-
+
@@ -138,217 +63,223 @@

Pivot data between long and wide

from data.table.

-
longer(.data, ..., name = "name", value = "value", na.rm = FALSE)
-
-wider(.data, ..., name, value = NULL, fun = NULL, fill = NA)
- -

Arguments

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.data

A data.table

...

Columns for unchanged group. Flexible, see examples.

name

Name for the measured variable names column.

value

Name for the data values column(s).

na.rm

If TRUE, NA values will be removed from the molten data.

fun

Should the data be aggregated before casting? +

+
longer(.data, ..., name = "name", value = "value", na.rm = FALSE)
+
+wider(.data, ..., name, value = NULL, fun = NULL, fill = NA)
+
+ +
+

Arguments

+ + +
.data
+

A data.table

+ + +
...
+

Columns for unchanged group. Flexible, see examples.

+ + +
name
+

Name for the measured variable names column.

+ + +
value
+

Name for the data values column(s).

+ + +
na.rm
+

If TRUE, NA values will be removed from the molten data.

+ + +
fun
+

Should the data be aggregated before casting? Defaults to NULL, which uses length for aggregation. -If a function is provided, with aggregated by this function.

fill

Value with which to fill missing cells. Default uses NA.

+If a function is provided, with aggregated by this function.

+ -

Value

+
fill
+

Value with which to fill missing cells. Default uses NA.

+
+
+

Value

A data.table

-

See also

- - - -

Examples

-
-stocks <- data.table( - time = as.Date('2009-01-01') + 0:9, - X = rnorm(10, 0, 1), - Y = rnorm(10, 0, 2), - Z = rnorm(10, 0, 4) -) - -stocks %>% longer(time)
#> time name value -#> <Date> <fctr> <num> -#> 1: 2009-01-01 X -1.400043517 -#> 2: 2009-01-02 X 0.255317055 -#> 3: 2009-01-03 X -2.437263611 -#> 4: 2009-01-04 X -0.005571287 -#> 5: 2009-01-05 X 0.621552721 -#> 6: 2009-01-06 X 1.148411606 -#> 7: 2009-01-07 X -1.821817661 -#> 8: 2009-01-08 X -0.247325302 -#> 9: 2009-01-09 X -0.244199607 -#> 10: 2009-01-10 X -0.282705449 -#> 11: 2009-01-01 Y -1.107398767 -#> 12: 2009-01-02 Y 1.257964084 -#> 13: 2009-01-03 Y 4.130049791 -#> 14: 2009-01-04 Y -3.261978804 -#> 15: 2009-01-05 Y 1.024853900 -#> 16: 2009-01-06 Y -3.726022984 -#> 17: 2009-01-07 Y -1.044025029 -#> 18: 2009-01-08 Y -0.105203820 -#> 19: 2009-01-09 Y 1.085992685 -#> 20: 2009-01-10 Y -1.828149655 -#> 21: 2009-01-01 Z 1.872617682 -#> 22: 2009-01-02 Z 1.451805023 -#> 23: 2009-01-03 Z -5.218174180 -#> 24: 2009-01-04 Z 2.951105285 -#> 25: 2009-01-05 Z 7.554019717 -#> 26: 2009-01-06 Z -0.389780418 -#> 27: 2009-01-07 Z -3.743389414 -#> 28: 2009-01-08 Z -0.063801245 -#> 29: 2009-01-09 Z -3.307155815 -#> 30: 2009-01-10 Z -6.049598605 -#> time name value
stocks %>% longer(-(2:4)) # same
#> time name value -#> <Date> <fctr> <num> -#> 1: 2009-01-01 X -1.400043517 -#> 2: 2009-01-02 X 0.255317055 -#> 3: 2009-01-03 X -2.437263611 -#> 4: 2009-01-04 X -0.005571287 -#> 5: 2009-01-05 X 0.621552721 -#> 6: 2009-01-06 X 1.148411606 -#> 7: 2009-01-07 X -1.821817661 -#> 8: 2009-01-08 X -0.247325302 -#> 9: 2009-01-09 X -0.244199607 -#> 10: 2009-01-10 X -0.282705449 -#> 11: 2009-01-01 Y -1.107398767 -#> 12: 2009-01-02 Y 1.257964084 -#> 13: 2009-01-03 Y 4.130049791 -#> 14: 2009-01-04 Y -3.261978804 -#> 15: 2009-01-05 Y 1.024853900 -#> 16: 2009-01-06 Y -3.726022984 -#> 17: 2009-01-07 Y -1.044025029 -#> 18: 2009-01-08 Y -0.105203820 -#> 19: 2009-01-09 Y 1.085992685 -#> 20: 2009-01-10 Y -1.828149655 -#> 21: 2009-01-01 Z 1.872617682 -#> 22: 2009-01-02 Z 1.451805023 -#> 23: 2009-01-03 Z -5.218174180 -#> 24: 2009-01-04 Z 2.951105285 -#> 25: 2009-01-05 Z 7.554019717 -#> 26: 2009-01-06 Z -0.389780418 -#> 27: 2009-01-07 Z -3.743389414 -#> 28: 2009-01-08 Z -0.063801245 -#> 29: 2009-01-09 Z -3.307155815 -#> 30: 2009-01-10 Z -6.049598605 -#> time name value
stocks %>% longer(-"X|Y|Z") # same
#> time name value -#> <Date> <fctr> <num> -#> 1: 2009-01-01 X -1.400043517 -#> 2: 2009-01-02 X 0.255317055 -#> 3: 2009-01-03 X -2.437263611 -#> 4: 2009-01-04 X -0.005571287 -#> 5: 2009-01-05 X 0.621552721 -#> 6: 2009-01-06 X 1.148411606 -#> 7: 2009-01-07 X -1.821817661 -#> 8: 2009-01-08 X -0.247325302 -#> 9: 2009-01-09 X -0.244199607 -#> 10: 2009-01-10 X -0.282705449 -#> 11: 2009-01-01 Y -1.107398767 -#> 12: 2009-01-02 Y 1.257964084 -#> 13: 2009-01-03 Y 4.130049791 -#> 14: 2009-01-04 Y -3.261978804 -#> 15: 2009-01-05 Y 1.024853900 -#> 16: 2009-01-06 Y -3.726022984 -#> 17: 2009-01-07 Y -1.044025029 -#> 18: 2009-01-08 Y -0.105203820 -#> 19: 2009-01-09 Y 1.085992685 -#> 20: 2009-01-10 Y -1.828149655 -#> 21: 2009-01-01 Z 1.872617682 -#> 22: 2009-01-02 Z 1.451805023 -#> 23: 2009-01-03 Z -5.218174180 -#> 24: 2009-01-04 Z 2.951105285 -#> 25: 2009-01-05 Z 7.554019717 -#> 26: 2009-01-06 Z -0.389780418 -#> 27: 2009-01-07 Z -3.743389414 -#> 28: 2009-01-08 Z -0.063801245 -#> 29: 2009-01-09 Z -3.307155815 -#> 30: 2009-01-10 Z -6.049598605 -#> time name value
long_stocks = longer(stocks,"ti") # same as above except for assignment - -long_stocks %>% wider(time,name = "name",value = "value")
#> Key: <time> -#> time X Y Z -#> <Date> <num> <num> <num> -#> 1: 2009-01-01 -1.400043517 -1.1073988 1.87261768 -#> 2: 2009-01-02 0.255317055 1.2579641 1.45180502 -#> 3: 2009-01-03 -2.437263611 4.1300498 -5.21817418 -#> 4: 2009-01-04 -0.005571287 -3.2619788 2.95110529 -#> 5: 2009-01-05 0.621552721 1.0248539 7.55401972 -#> 6: 2009-01-06 1.148411606 -3.7260230 -0.38978042 -#> 7: 2009-01-07 -1.821817661 -1.0440250 -3.74338941 -#> 8: 2009-01-08 -0.247325302 -0.1052038 -0.06380125 -#> 9: 2009-01-09 -0.244199607 1.0859927 -3.30715581 -#> 10: 2009-01-10 -0.282705449 -1.8281497 -6.04959861
-# the unchanged group could be missed if all the rest will be used -long_stocks %>% wider(name = "name",value = "value")
#> Key: <time> -#> time X Y Z -#> <Date> <num> <num> <num> -#> 1: 2009-01-01 -1.400043517 -1.1073988 1.87261768 -#> 2: 2009-01-02 0.255317055 1.2579641 1.45180502 -#> 3: 2009-01-03 -2.437263611 4.1300498 -5.21817418 -#> 4: 2009-01-04 -0.005571287 -3.2619788 2.95110529 -#> 5: 2009-01-05 0.621552721 1.0248539 7.55401972 -#> 6: 2009-01-06 1.148411606 -3.7260230 -0.38978042 -#> 7: 2009-01-07 -1.821817661 -1.0440250 -3.74338941 -#> 8: 2009-01-08 -0.247325302 -0.1052038 -0.06380125 -#> 9: 2009-01-09 -0.244199607 1.0859927 -3.30715581 -#> 10: 2009-01-10 -0.282705449 -1.8281497 -6.04959861
-
-
- +
+

See also

+ +
+
+

Examples

+

+stocks <- data.table(
+  time = as.Date('2009-01-01') + 0:9,
+  X = rnorm(10, 0, 1),
+  Y = rnorm(10, 0, 2),
+  Z = rnorm(10, 0, 4)
+)
+
+stocks %>% longer(time)
+#>           time   name        value
+#>         <Date> <fctr>        <num>
+#>  1: 2009-01-01      X -1.400043517
+#>  2: 2009-01-02      X  0.255317055
+#>  3: 2009-01-03      X -2.437263611
+#>  4: 2009-01-04      X -0.005571287
+#>  5: 2009-01-05      X  0.621552721
+#>  6: 2009-01-06      X  1.148411606
+#>  7: 2009-01-07      X -1.821817661
+#>  8: 2009-01-08      X -0.247325302
+#>  9: 2009-01-09      X -0.244199607
+#> 10: 2009-01-10      X -0.282705449
+#> 11: 2009-01-01      Y -1.107398767
+#> 12: 2009-01-02      Y  1.257964084
+#> 13: 2009-01-03      Y  4.130049791
+#> 14: 2009-01-04      Y -3.261978804
+#> 15: 2009-01-05      Y  1.024853900
+#> 16: 2009-01-06      Y -3.726022984
+#> 17: 2009-01-07      Y -1.044025029
+#> 18: 2009-01-08      Y -0.105203820
+#> 19: 2009-01-09      Y  1.085992685
+#> 20: 2009-01-10      Y -1.828149655
+#> 21: 2009-01-01      Z  1.872617682
+#> 22: 2009-01-02      Z  1.451805023
+#> 23: 2009-01-03      Z -5.218174180
+#> 24: 2009-01-04      Z  2.951105285
+#> 25: 2009-01-05      Z  7.554019717
+#> 26: 2009-01-06      Z -0.389780418
+#> 27: 2009-01-07      Z -3.743389414
+#> 28: 2009-01-08      Z -0.063801245
+#> 29: 2009-01-09      Z -3.307155815
+#> 30: 2009-01-10      Z -6.049598605
+#>           time   name        value
+stocks %>% longer(-(2:4)) # same
+#>           time   name        value
+#>         <Date> <fctr>        <num>
+#>  1: 2009-01-01      X -1.400043517
+#>  2: 2009-01-02      X  0.255317055
+#>  3: 2009-01-03      X -2.437263611
+#>  4: 2009-01-04      X -0.005571287
+#>  5: 2009-01-05      X  0.621552721
+#>  6: 2009-01-06      X  1.148411606
+#>  7: 2009-01-07      X -1.821817661
+#>  8: 2009-01-08      X -0.247325302
+#>  9: 2009-01-09      X -0.244199607
+#> 10: 2009-01-10      X -0.282705449
+#> 11: 2009-01-01      Y -1.107398767
+#> 12: 2009-01-02      Y  1.257964084
+#> 13: 2009-01-03      Y  4.130049791
+#> 14: 2009-01-04      Y -3.261978804
+#> 15: 2009-01-05      Y  1.024853900
+#> 16: 2009-01-06      Y -3.726022984
+#> 17: 2009-01-07      Y -1.044025029
+#> 18: 2009-01-08      Y -0.105203820
+#> 19: 2009-01-09      Y  1.085992685
+#> 20: 2009-01-10      Y -1.828149655
+#> 21: 2009-01-01      Z  1.872617682
+#> 22: 2009-01-02      Z  1.451805023
+#> 23: 2009-01-03      Z -5.218174180
+#> 24: 2009-01-04      Z  2.951105285
+#> 25: 2009-01-05      Z  7.554019717
+#> 26: 2009-01-06      Z -0.389780418
+#> 27: 2009-01-07      Z -3.743389414
+#> 28: 2009-01-08      Z -0.063801245
+#> 29: 2009-01-09      Z -3.307155815
+#> 30: 2009-01-10      Z -6.049598605
+#>           time   name        value
+stocks %>% longer(-"X|Y|Z") # same
+#>           time   name        value
+#>         <Date> <fctr>        <num>
+#>  1: 2009-01-01      X -1.400043517
+#>  2: 2009-01-02      X  0.255317055
+#>  3: 2009-01-03      X -2.437263611
+#>  4: 2009-01-04      X -0.005571287
+#>  5: 2009-01-05      X  0.621552721
+#>  6: 2009-01-06      X  1.148411606
+#>  7: 2009-01-07      X -1.821817661
+#>  8: 2009-01-08      X -0.247325302
+#>  9: 2009-01-09      X -0.244199607
+#> 10: 2009-01-10      X -0.282705449
+#> 11: 2009-01-01      Y -1.107398767
+#> 12: 2009-01-02      Y  1.257964084
+#> 13: 2009-01-03      Y  4.130049791
+#> 14: 2009-01-04      Y -3.261978804
+#> 15: 2009-01-05      Y  1.024853900
+#> 16: 2009-01-06      Y -3.726022984
+#> 17: 2009-01-07      Y -1.044025029
+#> 18: 2009-01-08      Y -0.105203820
+#> 19: 2009-01-09      Y  1.085992685
+#> 20: 2009-01-10      Y -1.828149655
+#> 21: 2009-01-01      Z  1.872617682
+#> 22: 2009-01-02      Z  1.451805023
+#> 23: 2009-01-03      Z -5.218174180
+#> 24: 2009-01-04      Z  2.951105285
+#> 25: 2009-01-05      Z  7.554019717
+#> 26: 2009-01-06      Z -0.389780418
+#> 27: 2009-01-07      Z -3.743389414
+#> 28: 2009-01-08      Z -0.063801245
+#> 29: 2009-01-09      Z -3.307155815
+#> 30: 2009-01-10      Z -6.049598605
+#>           time   name        value
+long_stocks = longer(stocks,"ti") # same as above except for assignment
+
+long_stocks %>% wider(time,name = "name",value = "value")
+#> Key: <time>
+#>           time            X          Y           Z
+#>         <Date>        <num>      <num>       <num>
+#>  1: 2009-01-01 -1.400043517 -1.1073988  1.87261768
+#>  2: 2009-01-02  0.255317055  1.2579641  1.45180502
+#>  3: 2009-01-03 -2.437263611  4.1300498 -5.21817418
+#>  4: 2009-01-04 -0.005571287 -3.2619788  2.95110529
+#>  5: 2009-01-05  0.621552721  1.0248539  7.55401972
+#>  6: 2009-01-06  1.148411606 -3.7260230 -0.38978042
+#>  7: 2009-01-07 -1.821817661 -1.0440250 -3.74338941
+#>  8: 2009-01-08 -0.247325302 -0.1052038 -0.06380125
+#>  9: 2009-01-09 -0.244199607  1.0859927 -3.30715581
+#> 10: 2009-01-10 -0.282705449 -1.8281497 -6.04959861
+
+# the unchanged group could be missed if all the rest will be used
+long_stocks %>% wider(name = "name",value = "value")
+#> Key: <time>
+#>           time            X          Y           Z
+#>         <Date>        <num>      <num>       <num>
+#>  1: 2009-01-01 -1.400043517 -1.1073988  1.87261768
+#>  2: 2009-01-02  0.255317055  1.2579641  1.45180502
+#>  3: 2009-01-03 -2.437263611  4.1300498 -5.21817418
+#>  4: 2009-01-04 -0.005571287 -3.2619788  2.95110529
+#>  5: 2009-01-05  0.621552721  1.0248539  7.55401972
+#>  6: 2009-01-06  1.148411606 -3.7260230 -0.38978042
+#>  7: 2009-01-07 -1.821817661 -1.0440250 -3.74338941
+#>  8: 2009-01-08 -0.247325302 -0.1052038 -0.06380125
+#>  9: 2009-01-09 -0.244199607  1.0859927 -3.30715581
+#> 10: 2009-01-10 -0.282705449 -1.8281497 -6.04959861
+
+
+
+
- + + - - - + diff --git a/docs/reference/mutate.html b/docs/reference/mutate.html index d3c5f3d..5407a6f 100644 --- a/docs/reference/mutate.html +++ b/docs/reference/mutate.html @@ -1,49 +1,5 @@ - - - - - - - -Create or transform variables — mutate • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Create or transform variables — mutate • tidyft - - - - - - - - - - - + - - - -
-
- - -
-
+
@@ -151,355 +76,369 @@

Create or transform variables

If you mutate a data.table, it is forever changed. No copies made, which is efficient, but should be used with caution. If you still want the keep the original data.table, use - copy first.

+ copy first.

+
+ +
+
mutate(.data, ..., by)
+
+transmute(.data, ..., by)
+
+mutate_when(.data, when, ..., by)
+
+mutate_vars(.data, .cols = NULL, .func, ..., by)
-
mutate(.data, ..., by)
-
-transmute(.data, ..., by)
-
-mutate_when(.data, when, ..., by)
-
-mutate_vars(.data, .cols = NULL, .func, ..., by)
- -

Arguments

- - - - - - - - - - - - - - - - - - - - - - - - - - -
.data

A data.table

...

Name-value pairs of expressions

by

(Optional) Mutate by what group?

when

An object which can be coerced to logical mode

.cols

Any types that can be accepted by select_dt.

.func

Function to be run within each column, should return a value or -vectors with same length.

- -

Value

+
+

Arguments

-

A data.table

-

Examples

-
# Newly created variables are available immediately - a = as.data.table(mtcars) - copy(a) %>% mutate(cyl2 = cyl * 2)
#> mpg cyl disp hp drat wt qsec vs am gear carb cyl2 -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 12 -#> 2: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 12 -#> 3: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 8 -#> 4: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 12 -#> 5: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 16 -#> 6: 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 12 -#> 7: 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 16 -#> 8: 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 8 -#> 9: 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 8 -#> 10: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 12 -#> 11: 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 12 -#> 12: 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 16 -#> 13: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 16 -#> 14: 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 16 -#> 15: 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 16 -#> 16: 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 16 -#> 17: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 16 -#> 18: 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 8 -#> 19: 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 8 -#> 20: 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 8 -#> 21: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 8 -#> 22: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 16 -#> 23: 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 16 -#> 24: 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 16 -#> 25: 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 16 -#> 26: 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 8 -#> 27: 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 8 -#> 28: 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 8 -#> 29: 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 16 -#> 30: 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 12 -#> 31: 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 16 -#> 32: 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 8 -#> mpg cyl disp hp drat wt qsec vs am gear carb cyl2
a
#> mpg cyl disp hp drat wt qsec vs am gear carb -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 -#> 2: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 -#> 3: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 -#> 4: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 -#> 5: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 -#> 6: 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 -#> 7: 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 -#> 8: 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 -#> 9: 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 -#> 10: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 -#> 11: 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 -#> 12: 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 -#> 13: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 -#> 14: 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 -#> 15: 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 -#> 16: 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 -#> 17: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 -#> 18: 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 -#> 19: 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 -#> 20: 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 -#> 21: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 -#> 22: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 -#> 23: 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 -#> 24: 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 -#> 25: 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 -#> 26: 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 -#> 27: 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 -#> 28: 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 -#> 29: 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 -#> 30: 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 -#> 31: 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 -#> 32: 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 -#> mpg cyl disp hp drat wt qsec vs am gear carb
- # change forever - a %>% mutate(cyl2 = cyl * 2)
#> mpg cyl disp hp drat wt qsec vs am gear carb cyl2 -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 12 -#> 2: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 12 -#> 3: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 8 -#> 4: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 12 -#> 5: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 16 -#> 6: 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 12 -#> 7: 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 16 -#> 8: 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 8 -#> 9: 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 8 -#> 10: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 12 -#> 11: 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 12 -#> 12: 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 16 -#> 13: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 16 -#> 14: 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 16 -#> 15: 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 16 -#> 16: 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 16 -#> 17: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 16 -#> 18: 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 8 -#> 19: 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 8 -#> 20: 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 8 -#> 21: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 8 -#> 22: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 16 -#> 23: 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 16 -#> 24: 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 16 -#> 25: 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 16 -#> 26: 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 8 -#> 27: 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 8 -#> 28: 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 8 -#> 29: 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 16 -#> 30: 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 12 -#> 31: 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 16 -#> 32: 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 8 -#> mpg cyl disp hp drat wt qsec vs am gear carb cyl2
a
#> mpg cyl disp hp drat wt qsec vs am gear carb cyl2 -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 12 -#> 2: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 12 -#> 3: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 8 -#> 4: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 12 -#> 5: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 16 -#> 6: 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 12 -#> 7: 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 16 -#> 8: 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 8 -#> 9: 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 8 -#> 10: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 12 -#> 11: 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 12 -#> 12: 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 16 -#> 13: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 16 -#> 14: 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 16 -#> 15: 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 16 -#> 16: 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 16 -#> 17: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 16 -#> 18: 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 8 -#> 19: 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 8 -#> 20: 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 8 -#> 21: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 8 -#> 22: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 16 -#> 23: 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 16 -#> 24: 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 16 -#> 25: 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 16 -#> 26: 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 8 -#> 27: 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 8 -#> 28: 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 8 -#> 29: 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 16 -#> 30: 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 12 -#> 31: 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 16 -#> 32: 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 8 -#> mpg cyl disp hp drat wt qsec vs am gear carb cyl2
- # You can also use mutate() to remove variables and - # modify existing variables - a %>% mutate( - mpg = NULL, - disp = disp * 0.0163871 # convert to litres - )
#> cyl disp hp drat wt qsec vs am gear carb cyl2 -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 6 2.621936 110 3.90 2.620 16.46 0 1 4 4 12 -#> 2: 6 2.621936 110 3.90 2.875 17.02 0 1 4 4 12 -#> 3: 4 1.769807 93 3.85 2.320 18.61 1 1 4 1 8 -#> 4: 6 4.227872 110 3.08 3.215 19.44 1 0 3 1 12 -#> 5: 8 5.899356 175 3.15 3.440 17.02 0 0 3 2 16 -#> 6: 6 3.687098 105 2.76 3.460 20.22 1 0 3 1 12 -#> 7: 8 5.899356 245 3.21 3.570 15.84 0 0 3 4 16 -#> 8: 4 2.403988 62 3.69 3.190 20.00 1 0 4 2 8 -#> 9: 4 2.307304 95 3.92 3.150 22.90 1 0 4 2 8 -#> 10: 6 2.746478 123 3.92 3.440 18.30 1 0 4 4 12 -#> 11: 6 2.746478 123 3.92 3.440 18.90 1 0 4 4 12 -#> 12: 8 4.519562 180 3.07 4.070 17.40 0 0 3 3 16 -#> 13: 8 4.519562 180 3.07 3.730 17.60 0 0 3 3 16 -#> 14: 8 4.519562 180 3.07 3.780 18.00 0 0 3 3 16 -#> 15: 8 7.734711 205 2.93 5.250 17.98 0 0 3 4 16 -#> 16: 8 7.538066 215 3.00 5.424 17.82 0 0 3 4 16 -#> 17: 8 7.210324 230 3.23 5.345 17.42 0 0 3 4 16 -#> 18: 4 1.289665 66 4.08 2.200 19.47 1 1 4 1 8 -#> 19: 4 1.240503 52 4.93 1.615 18.52 1 1 4 2 8 -#> 20: 4 1.165123 65 4.22 1.835 19.90 1 1 4 1 8 -#> 21: 4 1.968091 97 3.70 2.465 20.01 1 0 3 1 8 -#> 22: 8 5.211098 150 2.76 3.520 16.87 0 0 3 2 16 -#> 23: 8 4.981678 150 3.15 3.435 17.30 0 0 3 2 16 -#> 24: 8 5.735485 245 3.73 3.840 15.41 0 0 3 4 16 -#> 25: 8 6.554840 175 3.08 3.845 17.05 0 0 3 2 16 -#> 26: 4 1.294581 66 4.08 1.935 18.90 1 1 4 1 8 -#> 27: 4 1.971368 91 4.43 2.140 16.70 0 1 5 2 8 -#> 28: 4 1.558413 113 3.77 1.513 16.90 1 1 5 2 8 -#> 29: 8 5.751872 264 4.22 3.170 14.50 0 1 5 4 16 -#> 30: 6 2.376130 175 3.62 2.770 15.50 0 1 5 6 12 -#> 31: 8 4.932517 335 3.54 3.570 14.60 0 1 5 8 16 -#> 32: 4 1.982839 109 4.11 2.780 18.60 1 1 4 2 8 -#> cyl disp hp drat wt qsec vs am gear carb cyl2
- a %>% transmute(cyl,one = 1)
#> cyl one -#> <num> <num> -#> 1: 6 1 -#> 2: 6 1 -#> 3: 4 1 -#> 4: 6 1 -#> 5: 8 1 -#> 6: 6 1 -#> 7: 8 1 -#> 8: 4 1 -#> 9: 4 1 -#> 10: 6 1 -#> 11: 6 1 -#> 12: 8 1 -#> 13: 8 1 -#> 14: 8 1 -#> 15: 8 1 -#> 16: 8 1 -#> 17: 8 1 -#> 18: 4 1 -#> 19: 4 1 -#> 20: 4 1 -#> 21: 4 1 -#> 22: 8 1 -#> 23: 8 1 -#> 24: 8 1 -#> 25: 8 1 -#> 26: 4 1 -#> 27: 4 1 -#> 28: 4 1 -#> 29: 8 1 -#> 30: 6 1 -#> 31: 8 1 -#> 32: 4 1 -#> cyl one
a
#> cyl disp hp drat wt qsec vs am gear carb cyl2 -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 6 2.621936 110 3.90 2.620 16.46 0 1 4 4 12 -#> 2: 6 2.621936 110 3.90 2.875 17.02 0 1 4 4 12 -#> 3: 4 1.769807 93 3.85 2.320 18.61 1 1 4 1 8 -#> 4: 6 4.227872 110 3.08 3.215 19.44 1 0 3 1 12 -#> 5: 8 5.899356 175 3.15 3.440 17.02 0 0 3 2 16 -#> 6: 6 3.687098 105 2.76 3.460 20.22 1 0 3 1 12 -#> 7: 8 5.899356 245 3.21 3.570 15.84 0 0 3 4 16 -#> 8: 4 2.403988 62 3.69 3.190 20.00 1 0 4 2 8 -#> 9: 4 2.307304 95 3.92 3.150 22.90 1 0 4 2 8 -#> 10: 6 2.746478 123 3.92 3.440 18.30 1 0 4 4 12 -#> 11: 6 2.746478 123 3.92 3.440 18.90 1 0 4 4 12 -#> 12: 8 4.519562 180 3.07 4.070 17.40 0 0 3 3 16 -#> 13: 8 4.519562 180 3.07 3.730 17.60 0 0 3 3 16 -#> 14: 8 4.519562 180 3.07 3.780 18.00 0 0 3 3 16 -#> 15: 8 7.734711 205 2.93 5.250 17.98 0 0 3 4 16 -#> 16: 8 7.538066 215 3.00 5.424 17.82 0 0 3 4 16 -#> 17: 8 7.210324 230 3.23 5.345 17.42 0 0 3 4 16 -#> 18: 4 1.289665 66 4.08 2.200 19.47 1 1 4 1 8 -#> 19: 4 1.240503 52 4.93 1.615 18.52 1 1 4 2 8 -#> 20: 4 1.165123 65 4.22 1.835 19.90 1 1 4 1 8 -#> 21: 4 1.968091 97 3.70 2.465 20.01 1 0 3 1 8 -#> 22: 8 5.211098 150 2.76 3.520 16.87 0 0 3 2 16 -#> 23: 8 4.981678 150 3.15 3.435 17.30 0 0 3 2 16 -#> 24: 8 5.735485 245 3.73 3.840 15.41 0 0 3 4 16 -#> 25: 8 6.554840 175 3.08 3.845 17.05 0 0 3 2 16 -#> 26: 4 1.294581 66 4.08 1.935 18.90 1 1 4 1 8 -#> 27: 4 1.971368 91 4.43 2.140 16.70 0 1 5 2 8 -#> 28: 4 1.558413 113 3.77 1.513 16.90 1 1 5 2 8 -#> 29: 8 5.751872 264 4.22 3.170 14.50 0 1 5 4 16 -#> 30: 6 2.376130 175 3.62 2.770 15.50 0 1 5 6 12 -#> 31: 8 4.932517 335 3.54 3.570 14.60 0 1 5 8 16 -#> 32: 4 1.982839 109 4.11 2.780 18.60 1 1 4 2 8 -#> cyl disp hp drat wt qsec vs am gear carb cyl2
- - iris[3:8,] %>% - as.data.table() %>% - mutate_when(Petal.Width == .2, - one = 1,Sepal.Length=2)
#> Index: <Petal.Width> -#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species one -#> <num> <num> <num> <num> <fctr> <num> -#> 1: 2.0 3.2 1.3 0.2 setosa 1 -#> 2: 2.0 3.1 1.5 0.2 setosa 1 -#> 3: 2.0 3.6 1.4 0.2 setosa 1 -#> 4: 5.4 3.9 1.7 0.4 setosa NA -#> 5: 4.6 3.4 1.4 0.3 setosa NA -#> 6: 2.0 3.4 1.5 0.2 setosa 1
- iris[3:8,] %>% - as.data.table() %>% - mutate_vars("Pe",scale)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 4.7 3.2 -1.219875 -0.5976143 setosa -#> 2: 4.6 3.1 0.243975 -0.5976143 setosa -#> 3: 5.0 3.6 -0.487950 -0.5976143 setosa -#> 4: 5.4 3.9 1.707825 1.7928429 setosa -#> 5: 4.6 3.4 -0.487950 0.5976143 setosa -#> 6: 5.0 3.4 0.243975 -0.5976143 setosa
-
-
- +
+

Value

+

A data.table

+
+ +
+

Examples

+
  # Newly created variables are available immediately
+  a = as.data.table(mtcars)
+  copy(a) %>% mutate(cyl2 = cyl * 2)
+#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2
+#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#>  1:  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4    12
+#>  2:  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4    12
+#>  3:  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1     8
+#>  4:  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1    12
+#>  5:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2    16
+#>  6:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1    12
+#>  7:  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4    16
+#>  8:  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2     8
+#>  9:  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2     8
+#> 10:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4    12
+#> 11:  17.8     6 167.6   123  3.92 3.440 18.90     1     0     4     4    12
+#> 12:  16.4     8 275.8   180  3.07 4.070 17.40     0     0     3     3    16
+#> 13:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3    16
+#> 14:  15.2     8 275.8   180  3.07 3.780 18.00     0     0     3     3    16
+#> 15:  10.4     8 472.0   205  2.93 5.250 17.98     0     0     3     4    16
+#> 16:  10.4     8 460.0   215  3.00 5.424 17.82     0     0     3     4    16
+#> 17:  14.7     8 440.0   230  3.23 5.345 17.42     0     0     3     4    16
+#> 18:  32.4     4  78.7    66  4.08 2.200 19.47     1     1     4     1     8
+#> 19:  30.4     4  75.7    52  4.93 1.615 18.52     1     1     4     2     8
+#> 20:  33.9     4  71.1    65  4.22 1.835 19.90     1     1     4     1     8
+#> 21:  21.5     4 120.1    97  3.70 2.465 20.01     1     0     3     1     8
+#> 22:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2    16
+#> 23:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2    16
+#> 24:  13.3     8 350.0   245  3.73 3.840 15.41     0     0     3     4    16
+#> 25:  19.2     8 400.0   175  3.08 3.845 17.05     0     0     3     2    16
+#> 26:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1     8
+#> 27:  26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2     8
+#> 28:  30.4     4  95.1   113  3.77 1.513 16.90     1     1     5     2     8
+#> 29:  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4    16
+#> 30:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6    12
+#> 31:  15.0     8 301.0   335  3.54 3.570 14.60     0     1     5     8    16
+#> 32:  21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2     8
+#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2
+  a
+#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
+#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#>  1:  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4
+#>  2:  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4
+#>  3:  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1
+#>  4:  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1
+#>  5:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
+#>  6:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1
+#>  7:  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4
+#>  8:  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2
+#>  9:  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2
+#> 10:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
+#> 11:  17.8     6 167.6   123  3.92 3.440 18.90     1     0     4     4
+#> 12:  16.4     8 275.8   180  3.07 4.070 17.40     0     0     3     3
+#> 13:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3
+#> 14:  15.2     8 275.8   180  3.07 3.780 18.00     0     0     3     3
+#> 15:  10.4     8 472.0   205  2.93 5.250 17.98     0     0     3     4
+#> 16:  10.4     8 460.0   215  3.00 5.424 17.82     0     0     3     4
+#> 17:  14.7     8 440.0   230  3.23 5.345 17.42     0     0     3     4
+#> 18:  32.4     4  78.7    66  4.08 2.200 19.47     1     1     4     1
+#> 19:  30.4     4  75.7    52  4.93 1.615 18.52     1     1     4     2
+#> 20:  33.9     4  71.1    65  4.22 1.835 19.90     1     1     4     1
+#> 21:  21.5     4 120.1    97  3.70 2.465 20.01     1     0     3     1
+#> 22:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2
+#> 23:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2
+#> 24:  13.3     8 350.0   245  3.73 3.840 15.41     0     0     3     4
+#> 25:  19.2     8 400.0   175  3.08 3.845 17.05     0     0     3     2
+#> 26:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1
+#> 27:  26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2
+#> 28:  30.4     4  95.1   113  3.77 1.513 16.90     1     1     5     2
+#> 29:  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4
+#> 30:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6
+#> 31:  15.0     8 301.0   335  3.54 3.570 14.60     0     1     5     8
+#> 32:  21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2
+#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
+
+  # change forever
+  a %>% mutate(cyl2 = cyl * 2)
+#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2
+#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#>  1:  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4    12
+#>  2:  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4    12
+#>  3:  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1     8
+#>  4:  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1    12
+#>  5:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2    16
+#>  6:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1    12
+#>  7:  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4    16
+#>  8:  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2     8
+#>  9:  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2     8
+#> 10:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4    12
+#> 11:  17.8     6 167.6   123  3.92 3.440 18.90     1     0     4     4    12
+#> 12:  16.4     8 275.8   180  3.07 4.070 17.40     0     0     3     3    16
+#> 13:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3    16
+#> 14:  15.2     8 275.8   180  3.07 3.780 18.00     0     0     3     3    16
+#> 15:  10.4     8 472.0   205  2.93 5.250 17.98     0     0     3     4    16
+#> 16:  10.4     8 460.0   215  3.00 5.424 17.82     0     0     3     4    16
+#> 17:  14.7     8 440.0   230  3.23 5.345 17.42     0     0     3     4    16
+#> 18:  32.4     4  78.7    66  4.08 2.200 19.47     1     1     4     1     8
+#> 19:  30.4     4  75.7    52  4.93 1.615 18.52     1     1     4     2     8
+#> 20:  33.9     4  71.1    65  4.22 1.835 19.90     1     1     4     1     8
+#> 21:  21.5     4 120.1    97  3.70 2.465 20.01     1     0     3     1     8
+#> 22:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2    16
+#> 23:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2    16
+#> 24:  13.3     8 350.0   245  3.73 3.840 15.41     0     0     3     4    16
+#> 25:  19.2     8 400.0   175  3.08 3.845 17.05     0     0     3     2    16
+#> 26:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1     8
+#> 27:  26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2     8
+#> 28:  30.4     4  95.1   113  3.77 1.513 16.90     1     1     5     2     8
+#> 29:  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4    16
+#> 30:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6    12
+#> 31:  15.0     8 301.0   335  3.54 3.570 14.60     0     1     5     8    16
+#> 32:  21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2     8
+#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2
+  a
+#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2
+#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#>  1:  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4    12
+#>  2:  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4    12
+#>  3:  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1     8
+#>  4:  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1    12
+#>  5:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2    16
+#>  6:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1    12
+#>  7:  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4    16
+#>  8:  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2     8
+#>  9:  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2     8
+#> 10:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4    12
+#> 11:  17.8     6 167.6   123  3.92 3.440 18.90     1     0     4     4    12
+#> 12:  16.4     8 275.8   180  3.07 4.070 17.40     0     0     3     3    16
+#> 13:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3    16
+#> 14:  15.2     8 275.8   180  3.07 3.780 18.00     0     0     3     3    16
+#> 15:  10.4     8 472.0   205  2.93 5.250 17.98     0     0     3     4    16
+#> 16:  10.4     8 460.0   215  3.00 5.424 17.82     0     0     3     4    16
+#> 17:  14.7     8 440.0   230  3.23 5.345 17.42     0     0     3     4    16
+#> 18:  32.4     4  78.7    66  4.08 2.200 19.47     1     1     4     1     8
+#> 19:  30.4     4  75.7    52  4.93 1.615 18.52     1     1     4     2     8
+#> 20:  33.9     4  71.1    65  4.22 1.835 19.90     1     1     4     1     8
+#> 21:  21.5     4 120.1    97  3.70 2.465 20.01     1     0     3     1     8
+#> 22:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2    16
+#> 23:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2    16
+#> 24:  13.3     8 350.0   245  3.73 3.840 15.41     0     0     3     4    16
+#> 25:  19.2     8 400.0   175  3.08 3.845 17.05     0     0     3     2    16
+#> 26:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1     8
+#> 27:  26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2     8
+#> 28:  30.4     4  95.1   113  3.77 1.513 16.90     1     1     5     2     8
+#> 29:  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4    16
+#> 30:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6    12
+#> 31:  15.0     8 301.0   335  3.54 3.570 14.60     0     1     5     8    16
+#> 32:  21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2     8
+#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2
+
+  # You can also use mutate() to remove variables and
+  # modify existing variables
+  a %>% mutate(
+    mpg = NULL,
+    disp = disp * 0.0163871 # convert to litres
+  )
+#>       cyl     disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2
+#>     <num>    <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#>  1:     6 2.621936   110  3.90 2.620 16.46     0     1     4     4    12
+#>  2:     6 2.621936   110  3.90 2.875 17.02     0     1     4     4    12
+#>  3:     4 1.769807    93  3.85 2.320 18.61     1     1     4     1     8
+#>  4:     6 4.227872   110  3.08 3.215 19.44     1     0     3     1    12
+#>  5:     8 5.899356   175  3.15 3.440 17.02     0     0     3     2    16
+#>  6:     6 3.687098   105  2.76 3.460 20.22     1     0     3     1    12
+#>  7:     8 5.899356   245  3.21 3.570 15.84     0     0     3     4    16
+#>  8:     4 2.403988    62  3.69 3.190 20.00     1     0     4     2     8
+#>  9:     4 2.307304    95  3.92 3.150 22.90     1     0     4     2     8
+#> 10:     6 2.746478   123  3.92 3.440 18.30     1     0     4     4    12
+#> 11:     6 2.746478   123  3.92 3.440 18.90     1     0     4     4    12
+#> 12:     8 4.519562   180  3.07 4.070 17.40     0     0     3     3    16
+#> 13:     8 4.519562   180  3.07 3.730 17.60     0     0     3     3    16
+#> 14:     8 4.519562   180  3.07 3.780 18.00     0     0     3     3    16
+#> 15:     8 7.734711   205  2.93 5.250 17.98     0     0     3     4    16
+#> 16:     8 7.538066   215  3.00 5.424 17.82     0     0     3     4    16
+#> 17:     8 7.210324   230  3.23 5.345 17.42     0     0     3     4    16
+#> 18:     4 1.289665    66  4.08 2.200 19.47     1     1     4     1     8
+#> 19:     4 1.240503    52  4.93 1.615 18.52     1     1     4     2     8
+#> 20:     4 1.165123    65  4.22 1.835 19.90     1     1     4     1     8
+#> 21:     4 1.968091    97  3.70 2.465 20.01     1     0     3     1     8
+#> 22:     8 5.211098   150  2.76 3.520 16.87     0     0     3     2    16
+#> 23:     8 4.981678   150  3.15 3.435 17.30     0     0     3     2    16
+#> 24:     8 5.735485   245  3.73 3.840 15.41     0     0     3     4    16
+#> 25:     8 6.554840   175  3.08 3.845 17.05     0     0     3     2    16
+#> 26:     4 1.294581    66  4.08 1.935 18.90     1     1     4     1     8
+#> 27:     4 1.971368    91  4.43 2.140 16.70     0     1     5     2     8
+#> 28:     4 1.558413   113  3.77 1.513 16.90     1     1     5     2     8
+#> 29:     8 5.751872   264  4.22 3.170 14.50     0     1     5     4    16
+#> 30:     6 2.376130   175  3.62 2.770 15.50     0     1     5     6    12
+#> 31:     8 4.932517   335  3.54 3.570 14.60     0     1     5     8    16
+#> 32:     4 1.982839   109  4.11 2.780 18.60     1     1     4     2     8
+#>       cyl     disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2
+
+  a %>% transmute(cyl,one = 1)
+#>       cyl   one
+#>     <num> <num>
+#>  1:     6     1
+#>  2:     6     1
+#>  3:     4     1
+#>  4:     6     1
+#>  5:     8     1
+#>  6:     6     1
+#>  7:     8     1
+#>  8:     4     1
+#>  9:     4     1
+#> 10:     6     1
+#> 11:     6     1
+#> 12:     8     1
+#> 13:     8     1
+#> 14:     8     1
+#> 15:     8     1
+#> 16:     8     1
+#> 17:     8     1
+#> 18:     4     1
+#> 19:     4     1
+#> 20:     4     1
+#> 21:     4     1
+#> 22:     8     1
+#> 23:     8     1
+#> 24:     8     1
+#> 25:     8     1
+#> 26:     4     1
+#> 27:     4     1
+#> 28:     4     1
+#> 29:     8     1
+#> 30:     6     1
+#> 31:     8     1
+#> 32:     4     1
+#>       cyl   one
+  a
+#>       cyl     disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2
+#>     <num>    <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#>  1:     6 2.621936   110  3.90 2.620 16.46     0     1     4     4    12
+#>  2:     6 2.621936   110  3.90 2.875 17.02     0     1     4     4    12
+#>  3:     4 1.769807    93  3.85 2.320 18.61     1     1     4     1     8
+#>  4:     6 4.227872   110  3.08 3.215 19.44     1     0     3     1    12
+#>  5:     8 5.899356   175  3.15 3.440 17.02     0     0     3     2    16
+#>  6:     6 3.687098   105  2.76 3.460 20.22     1     0     3     1    12
+#>  7:     8 5.899356   245  3.21 3.570 15.84     0     0     3     4    16
+#>  8:     4 2.403988    62  3.69 3.190 20.00     1     0     4     2     8
+#>  9:     4 2.307304    95  3.92 3.150 22.90     1     0     4     2     8
+#> 10:     6 2.746478   123  3.92 3.440 18.30     1     0     4     4    12
+#> 11:     6 2.746478   123  3.92 3.440 18.90     1     0     4     4    12
+#> 12:     8 4.519562   180  3.07 4.070 17.40     0     0     3     3    16
+#> 13:     8 4.519562   180  3.07 3.730 17.60     0     0     3     3    16
+#> 14:     8 4.519562   180  3.07 3.780 18.00     0     0     3     3    16
+#> 15:     8 7.734711   205  2.93 5.250 17.98     0     0     3     4    16
+#> 16:     8 7.538066   215  3.00 5.424 17.82     0     0     3     4    16
+#> 17:     8 7.210324   230  3.23 5.345 17.42     0     0     3     4    16
+#> 18:     4 1.289665    66  4.08 2.200 19.47     1     1     4     1     8
+#> 19:     4 1.240503    52  4.93 1.615 18.52     1     1     4     2     8
+#> 20:     4 1.165123    65  4.22 1.835 19.90     1     1     4     1     8
+#> 21:     4 1.968091    97  3.70 2.465 20.01     1     0     3     1     8
+#> 22:     8 5.211098   150  2.76 3.520 16.87     0     0     3     2    16
+#> 23:     8 4.981678   150  3.15 3.435 17.30     0     0     3     2    16
+#> 24:     8 5.735485   245  3.73 3.840 15.41     0     0     3     4    16
+#> 25:     8 6.554840   175  3.08 3.845 17.05     0     0     3     2    16
+#> 26:     4 1.294581    66  4.08 1.935 18.90     1     1     4     1     8
+#> 27:     4 1.971368    91  4.43 2.140 16.70     0     1     5     2     8
+#> 28:     4 1.558413   113  3.77 1.513 16.90     1     1     5     2     8
+#> 29:     8 5.751872   264  4.22 3.170 14.50     0     1     5     4    16
+#> 30:     6 2.376130   175  3.62 2.770 15.50     0     1     5     6    12
+#> 31:     8 4.932517   335  3.54 3.570 14.60     0     1     5     8    16
+#> 32:     4 1.982839   109  4.11 2.780 18.60     1     1     4     2     8
+#>       cyl     disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2
+
+
+ iris[3:8,] %>%
+   as.data.table() %>%
+   mutate_when(Petal.Width == .2,
+               one = 1,Sepal.Length=2)
+#> Index: <Petal.Width>
+#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species   one
+#>           <num>       <num>        <num>       <num>  <fctr> <num>
+#> 1:          2.0         3.2          1.3         0.2  setosa     1
+#> 2:          2.0         3.1          1.5         0.2  setosa     1
+#> 3:          2.0         3.6          1.4         0.2  setosa     1
+#> 4:          5.4         3.9          1.7         0.4  setosa    NA
+#> 5:          4.6         3.4          1.4         0.3  setosa    NA
+#> 6:          2.0         3.4          1.5         0.2  setosa     1
+
+ iris[3:8,] %>%
+   as.data.table() %>%
+   mutate_vars("Pe",scale)
+#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
+#>           <num>       <num>        <num>       <num>  <fctr>
+#> 1:          4.7         3.2    -1.219875  -0.5976143  setosa
+#> 2:          4.6         3.1     0.243975  -0.5976143  setosa
+#> 3:          5.0         3.6    -0.487950  -0.5976143  setosa
+#> 4:          5.4         3.9     1.707825   1.7928429  setosa
+#> 5:          4.6         3.4    -0.487950   0.5976143  setosa
+#> 6:          5.0         3.4     0.243975  -0.5976143  setosa
+
+
+
+
-
+ + - - - + diff --git a/docs/reference/nest.html b/docs/reference/nest.html index f0a7168..f32dea6 100644 --- a/docs/reference/nest.html +++ b/docs/reference/nest.html @@ -1,75 +1,15 @@ - - - - - - - -Nest and unnest — nest • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Nest and unnest — nest • tidyft - - - - - - - - - - - - - + - -
-
- - -
-
+
@@ -140,42 +65,48 @@

Nest and unnest

designed to merge multiple columns into list column.

-
nest(.data, ..., mcols = NULL)
+    
+
nest(.data, ..., mcols = NULL, .name = "ndt")
+
+unnest(.data, ...)
+
+squeeze(.data, ..., .name = "ndt")
+
+chop(.data, ...)
+
+unchop(.data, ...)
+
-unnest(.data, ...) +
+

Arguments

-squeeze(.data, ...) -chop(.data, ...) +
.data
+

data.table, nested or unnested

-unchop(.data, ...)
-

Arguments

- - - - - - - - - - - - - - -
.data

data.table, nested or unnested

...

The variables for nest group(for nest), +

...
+

The variables for nest group(for nest), columns to be nested(for squeeze and chop), or column(s) to be unnested(for unnest). -Could recieve anything that select_dt could receive.

mcols

Name-variable pairs in the list, form like -list(petal="^Pe",sepal="^Se"), see example.

+Could recieve anything that select_dt could receive.

-

Value

-

data.table, nested or unnested

-

Details

+
mcols
+

Name-variable pairs in the list, form like

+ +
.name
+

Character. The nested column name. Defaults to "ndt". +list(petal="^Pe",sepal="^Se"), see example.

+ +
+
+

Value

+

data.table, nested or unnested

+
+
+

Details

In the nest, the data would be nested to a column named `ndt`, which is short for nested data.table.

The squeeze would not remove the originial columns.

@@ -183,281 +114,321 @@

Details

These functions are experiencing the experimental stage, especially the unnest. If they don't work on some circumtances, try tidyr package.

-

References

- +
+
+

References

https://www.r-bloggers.com/much-faster-unnesting-with-data-table/

https://stackoverflow.com/questions/25430986/create-nested-data-tables-by-collapsing-rows-into-new-data-tables

-

See also

- - - -

Examples

-
-mtcars = as.data.table(mtcars) -iris = as.data.table(iris) - -# examples for nest - -# nest by which columns? - mtcars %>% nest(cyl)
#> cyl ndt -#> <num> <list> -#> 1: 6 <data.table> -#> 2: 4 <data.table> -#> 3: 8 <data.table>
mtcars %>% nest("cyl")
#> cyl ndt -#> <num> <list> -#> 1: 6 <data.table> -#> 2: 4 <data.table> -#> 3: 8 <data.table>
mtcars %>% nest(cyl,vs)
#> cyl vs ndt -#> <num> <num> <list> -#> 1: 6 0 <data.table> -#> 2: 4 1 <data.table> -#> 3: 6 1 <data.table> -#> 4: 8 0 <data.table> -#> 5: 4 0 <data.table>
mtcars %>% nest(vs:am)
#> vs am ndt -#> <num> <num> <list> -#> 1: 0 1 <data.table> -#> 2: 1 1 <data.table> -#> 3: 1 0 <data.table> -#> 4: 0 0 <data.table>
mtcars %>% nest("cyl|vs")
#> cyl vs ndt -#> <num> <num> <list> -#> 1: 6 0 <data.table> -#> 2: 4 1 <data.table> -#> 3: 6 1 <data.table> -#> 4: 8 0 <data.table> -#> 5: 4 0 <data.table>
mtcars %>% nest(c("cyl","vs"))
#> cyl vs ndt -#> <num> <num> <list> -#> 1: 6 0 <data.table> -#> 2: 4 1 <data.table> -#> 3: 6 1 <data.table> -#> 4: 8 0 <data.table> -#> 5: 4 0 <data.table>
-# nest two columns directly -iris %>% nest(mcols = list(petal="^Pe",sepal="^Se"))
#> Key: <Species> -#> Species petal sepal -#> <fctr> <list> <list> -#> 1: setosa <data.table> <data.table> -#> 2: versicolor <data.table> <data.table> -#> 3: virginica <data.table> <data.table>
-# nest more flexibly -iris %>% nest(mcols = list(ndt1 = 1:3, - ndt2 = "Pe", - ndt3 = Sepal.Length:Sepal.Width))
#> Key: <Species> -#> Species ndt1 ndt2 ndt3 -#> <fctr> <list> <list> <list> -#> 1: setosa <data.table> <data.table> <data.table> -#> 2: versicolor <data.table> <data.table> <data.table> -#> 3: virginica <data.table> <data.table> <data.table>
-# examples for unnest -# unnest which column? - mtcars %>% nest("cyl|vs") %>% - unnest(ndt)
#> cyl vs mpg disp hp drat wt qsec am gear carb -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 6 0 21.0 160.0 110 3.90 2.620 16.46 1 4 4 -#> 2: 6 0 21.0 160.0 110 3.90 2.875 17.02 1 4 4 -#> 3: 6 0 19.7 145.0 175 3.62 2.770 15.50 1 5 6 -#> 4: 4 1 22.8 108.0 93 3.85 2.320 18.61 1 4 1 -#> 5: 4 1 24.4 146.7 62 3.69 3.190 20.00 0 4 2 -#> 6: 4 1 22.8 140.8 95 3.92 3.150 22.90 0 4 2 -#> 7: 4 1 32.4 78.7 66 4.08 2.200 19.47 1 4 1 -#> 8: 4 1 30.4 75.7 52 4.93 1.615 18.52 1 4 2 -#> 9: 4 1 33.9 71.1 65 4.22 1.835 19.90 1 4 1 -#> 10: 4 1 21.5 120.1 97 3.70 2.465 20.01 0 3 1 -#> 11: 4 1 27.3 79.0 66 4.08 1.935 18.90 1 4 1 -#> 12: 4 1 30.4 95.1 113 3.77 1.513 16.90 1 5 2 -#> 13: 4 1 21.4 121.0 109 4.11 2.780 18.60 1 4 2 -#> 14: 6 1 21.4 258.0 110 3.08 3.215 19.44 0 3 1 -#> 15: 6 1 18.1 225.0 105 2.76 3.460 20.22 0 3 1 -#> 16: 6 1 19.2 167.6 123 3.92 3.440 18.30 0 4 4 -#> 17: 6 1 17.8 167.6 123 3.92 3.440 18.90 0 4 4 -#> 18: 8 0 18.7 360.0 175 3.15 3.440 17.02 0 3 2 -#> 19: 8 0 14.3 360.0 245 3.21 3.570 15.84 0 3 4 -#> 20: 8 0 16.4 275.8 180 3.07 4.070 17.40 0 3 3 -#> 21: 8 0 17.3 275.8 180 3.07 3.730 17.60 0 3 3 -#> 22: 8 0 15.2 275.8 180 3.07 3.780 18.00 0 3 3 -#> 23: 8 0 10.4 472.0 205 2.93 5.250 17.98 0 3 4 -#> 24: 8 0 10.4 460.0 215 3.00 5.424 17.82 0 3 4 -#> 25: 8 0 14.7 440.0 230 3.23 5.345 17.42 0 3 4 -#> 26: 8 0 15.5 318.0 150 2.76 3.520 16.87 0 3 2 -#> 27: 8 0 15.2 304.0 150 3.15 3.435 17.30 0 3 2 -#> 28: 8 0 13.3 350.0 245 3.73 3.840 15.41 0 3 4 -#> 29: 8 0 19.2 400.0 175 3.08 3.845 17.05 0 3 2 -#> 30: 8 0 15.8 351.0 264 4.22 3.170 14.50 1 5 4 -#> 31: 8 0 15.0 301.0 335 3.54 3.570 14.60 1 5 8 -#> 32: 4 0 26.0 120.3 91 4.43 2.140 16.70 1 5 2 -#> cyl vs mpg disp hp drat wt qsec am gear carb
mtcars %>% nest("cyl|vs") %>% - unnest("ndt")
#> cyl vs mpg disp hp drat wt qsec am gear carb -#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> -#> 1: 6 0 21.0 160.0 110 3.90 2.620 16.46 1 4 4 -#> 2: 6 0 21.0 160.0 110 3.90 2.875 17.02 1 4 4 -#> 3: 6 0 19.7 145.0 175 3.62 2.770 15.50 1 5 6 -#> 4: 4 1 22.8 108.0 93 3.85 2.320 18.61 1 4 1 -#> 5: 4 1 24.4 146.7 62 3.69 3.190 20.00 0 4 2 -#> 6: 4 1 22.8 140.8 95 3.92 3.150 22.90 0 4 2 -#> 7: 4 1 32.4 78.7 66 4.08 2.200 19.47 1 4 1 -#> 8: 4 1 30.4 75.7 52 4.93 1.615 18.52 1 4 2 -#> 9: 4 1 33.9 71.1 65 4.22 1.835 19.90 1 4 1 -#> 10: 4 1 21.5 120.1 97 3.70 2.465 20.01 0 3 1 -#> 11: 4 1 27.3 79.0 66 4.08 1.935 18.90 1 4 1 -#> 12: 4 1 30.4 95.1 113 3.77 1.513 16.90 1 5 2 -#> 13: 4 1 21.4 121.0 109 4.11 2.780 18.60 1 4 2 -#> 14: 6 1 21.4 258.0 110 3.08 3.215 19.44 0 3 1 -#> 15: 6 1 18.1 225.0 105 2.76 3.460 20.22 0 3 1 -#> 16: 6 1 19.2 167.6 123 3.92 3.440 18.30 0 4 4 -#> 17: 6 1 17.8 167.6 123 3.92 3.440 18.90 0 4 4 -#> 18: 8 0 18.7 360.0 175 3.15 3.440 17.02 0 3 2 -#> 19: 8 0 14.3 360.0 245 3.21 3.570 15.84 0 3 4 -#> 20: 8 0 16.4 275.8 180 3.07 4.070 17.40 0 3 3 -#> 21: 8 0 17.3 275.8 180 3.07 3.730 17.60 0 3 3 -#> 22: 8 0 15.2 275.8 180 3.07 3.780 18.00 0 3 3 -#> 23: 8 0 10.4 472.0 205 2.93 5.250 17.98 0 3 4 -#> 24: 8 0 10.4 460.0 215 3.00 5.424 17.82 0 3 4 -#> 25: 8 0 14.7 440.0 230 3.23 5.345 17.42 0 3 4 -#> 26: 8 0 15.5 318.0 150 2.76 3.520 16.87 0 3 2 -#> 27: 8 0 15.2 304.0 150 3.15 3.435 17.30 0 3 2 -#> 28: 8 0 13.3 350.0 245 3.73 3.840 15.41 0 3 4 -#> 29: 8 0 19.2 400.0 175 3.08 3.845 17.05 0 3 2 -#> 30: 8 0 15.8 351.0 264 4.22 3.170 14.50 1 5 4 -#> 31: 8 0 15.0 301.0 335 3.54 3.570 14.60 1 5 8 -#> 32: 4 0 26.0 120.3 91 4.43 2.140 16.70 1 5 2 -#> cyl vs mpg disp hp drat wt qsec am gear carb
-df <- data.table( - a = list(c("a", "b"), "c"), - b = list(c(TRUE,TRUE),FALSE), - c = list(3,c(1,2)), - d = c(11, 22) -) - -df
#> a b c d -#> <list> <list> <list> <num> -#> 1: a,b TRUE,TRUE 3 11 -#> 2: c FALSE 1,2 22
df %>% unnest(a)
#> d a -#> <num> <char> -#> 1: 11 a -#> 2: 11 b -#> 3: 22 c
df %>% unnest(2)
#> d b -#> <num> <lgcl> -#> 1: 11 TRUE -#> 2: 11 TRUE -#> 3: 22 FALSE
df %>% unnest("c")
#> d c -#> <num> <num> -#> 1: 11 3 -#> 2: 22 1 -#> 3: 22 2
df %>% unnest(cols = names(df)[3])
#> d c -#> <num> <num> -#> 1: 11 3 -#> 2: 22 1 -#> 3: 22 2
-# You can unnest multiple columns simultaneously -df %>% unnest(1:3)
#> Key: <d> -#> d a b c -#> <num> <char> <lgcl> <num> -#> 1: 11 a TRUE 3 -#> 2: 11 a TRUE 3 -#> 3: 11 b TRUE 3 -#> 4: 11 b TRUE 3 -#> 5: 22 c FALSE 1 -#> 6: 22 c FALSE 2
df %>% unnest(a,b,c)
#> Key: <d> -#> d a b c -#> <num> <char> <lgcl> <num> -#> 1: 11 a TRUE 3 -#> 2: 11 a TRUE 3 -#> 3: 11 b TRUE 3 -#> 4: 11 b TRUE 3 -#> 5: 22 c FALSE 1 -#> 6: 22 c FALSE 2
df %>% unnest("a|b|c")
#> Key: <d> -#> d a b c -#> <num> <char> <lgcl> <num> -#> 1: 11 a TRUE 3 -#> 2: 11 a TRUE 3 -#> 3: 11 b TRUE 3 -#> 4: 11 b TRUE 3 -#> 5: 22 c FALSE 1 -#> 6: 22 c FALSE 2
-# examples for squeeze -# nest which columns? -iris %>% squeeze(1:2)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species ndt -#> <num> <num> <num> <num> <fctr> <list> -#> 1: 5.1 3.5 1.4 0.2 setosa 5.1,3.5 -#> 2: 4.9 3.0 1.4 0.2 setosa 4.9,3.0 -#> 3: 4.7 3.2 1.3 0.2 setosa 4.7,3.2 -#> 4: 4.6 3.1 1.5 0.2 setosa 4.6,3.1 -#> 5: 5.0 3.6 1.4 0.2 setosa 5.0,3.6 -#> --- -#> 146: 6.7 3.0 5.2 2.3 virginica 6.7,3.0 -#> 147: 6.3 2.5 5.0 1.9 virginica 6.3,2.5 -#> 148: 6.5 3.0 5.2 2.0 virginica 6.5,3.0 -#> 149: 6.2 3.4 5.4 2.3 virginica 6.2,3.4 -#> 150: 5.9 3.0 5.1 1.8 virginica 5.9,3.0
iris %>% squeeze("Se")
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species ndt -#> <num> <num> <num> <num> <fctr> <list> -#> 1: 5.1 3.5 1.4 0.2 setosa 5.1,3.5 -#> 2: 4.9 3.0 1.4 0.2 setosa 4.9,3.0 -#> 3: 4.7 3.2 1.3 0.2 setosa 4.7,3.2 -#> 4: 4.6 3.1 1.5 0.2 setosa 4.6,3.1 -#> 5: 5.0 3.6 1.4 0.2 setosa 5.0,3.6 -#> --- -#> 146: 6.7 3.0 5.2 2.3 virginica 6.7,3.0 -#> 147: 6.3 2.5 5.0 1.9 virginica 6.3,2.5 -#> 148: 6.5 3.0 5.2 2.0 virginica 6.5,3.0 -#> 149: 6.2 3.4 5.4 2.3 virginica 6.2,3.4 -#> 150: 5.9 3.0 5.1 1.8 virginica 5.9,3.0
iris %>% squeeze(Sepal.Length:Petal.Width)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species ndt -#> <num> <num> <num> <num> <fctr> <list> -#> 1: 5.1 3.5 1.4 0.2 setosa 5.1,3.5 -#> 2: 4.9 3.0 1.4 0.2 setosa 4.9,3.0 -#> 3: 4.7 3.2 1.3 0.2 setosa 4.7,3.2 -#> 4: 4.6 3.1 1.5 0.2 setosa 4.6,3.1 -#> 5: 5.0 3.6 1.4 0.2 setosa 5.0,3.6 -#> --- -#> 146: 6.7 3.0 5.2 2.3 virginica 6.7,3.0 -#> 147: 6.3 2.5 5.0 1.9 virginica 6.3,2.5 -#> 148: 6.5 3.0 5.2 2.0 virginica 6.5,3.0 -#> 149: 6.2 3.4 5.4 2.3 virginica 6.2,3.4 -#> 150: 5.9 3.0 5.1 1.8 virginica 5.9,3.0
-# examples for chop -df <- data.table(x = c(1, 1, 1, 2, 2, 3), y = 1:6, z = 6:1) -df %>% chop(y,z)
#> x y z -#> <num> <list> <list> -#> 1: 1 1,2,3 6,5,4 -#> 2: 2 4,5 3,2 -#> 3: 3 6 1
df %>% chop(y,z) %>% unchop(y,z)
#> x y z -#> <num> <int> <int> -#> 1: 1 1 6 -#> 2: 1 2 5 -#> 3: 1 3 4 -#> 4: 2 4 3 -#> 5: 2 5 2 -#> 6: 3 6 1
-
- +
+

See also

+ +
+
+

Examples

+

+mtcars = as.data.table(mtcars)
+iris = as.data.table(iris)
+
+# examples for nest
+
+# nest by which columns?
+ mtcars %>% nest(cyl)
+#>      cyl                 ndt
+#>    <num>              <list>
+#> 1:     6  <data.table[7x10]>
+#> 2:     4 <data.table[11x10]>
+#> 3:     8 <data.table[14x10]>
+ mtcars %>% nest("cyl")
+#>      cyl                 ndt
+#>    <num>              <list>
+#> 1:     6  <data.table[7x10]>
+#> 2:     4 <data.table[11x10]>
+#> 3:     8 <data.table[14x10]>
+ mtcars %>% nest(cyl,vs)
+#>      cyl    vs                ndt
+#>    <num> <num>             <list>
+#> 1:     6     0  <data.table[3x9]>
+#> 2:     4     1 <data.table[10x9]>
+#> 3:     6     1  <data.table[4x9]>
+#> 4:     8     0 <data.table[14x9]>
+#> 5:     4     0  <data.table[1x9]>
+ mtcars %>% nest(vs:am)
+#>       vs    am                ndt
+#>    <num> <num>             <list>
+#> 1:     0     1  <data.table[6x9]>
+#> 2:     1     1  <data.table[7x9]>
+#> 3:     1     0  <data.table[7x9]>
+#> 4:     0     0 <data.table[12x9]>
+ mtcars %>% nest("cyl|vs")
+#>      cyl    vs                ndt
+#>    <num> <num>             <list>
+#> 1:     6     0  <data.table[3x9]>
+#> 2:     4     1 <data.table[10x9]>
+#> 3:     6     1  <data.table[4x9]>
+#> 4:     8     0 <data.table[14x9]>
+#> 5:     4     0  <data.table[1x9]>
+ mtcars %>% nest(c("cyl","vs"))
+#>      cyl    vs                ndt
+#>    <num> <num>             <list>
+#> 1:     6     0  <data.table[3x9]>
+#> 2:     4     1 <data.table[10x9]>
+#> 3:     6     1  <data.table[4x9]>
+#> 4:     8     0 <data.table[14x9]>
+#> 5:     4     0  <data.table[1x9]>
+
+# nest two columns directly
+iris %>% nest(mcols = list(petal="^Pe",sepal="^Se"))
+#> Key: <Species>
+#>       Species              petal              sepal
+#>        <fctr>             <list>             <list>
+#> 1:     setosa <data.table[50x4]> <data.table[50x4]>
+#> 2: versicolor <data.table[50x4]> <data.table[50x4]>
+#> 3:  virginica <data.table[50x4]> <data.table[50x4]>
+
+# nest more flexibly
+iris %>% nest(mcols = list(ndt1 = 1:3,
+  ndt2 = "Pe",
+  ndt3 = Sepal.Length:Sepal.Width))
+#> Key: <Species>
+#>       Species               ndt1               ndt2               ndt3
+#>        <fctr>             <list>             <list>             <list>
+#> 1:     setosa <data.table[50x4]> <data.table[50x4]> <data.table[50x4]>
+#> 2: versicolor <data.table[50x4]> <data.table[50x4]> <data.table[50x4]>
+#> 3:  virginica <data.table[50x4]> <data.table[50x4]> <data.table[50x4]>
+
+# examples for unnest
+# unnest which column?
+ mtcars %>% nest("cyl|vs") %>%
+   unnest(ndt)
+#>       cyl    vs   mpg  disp    hp  drat    wt  qsec    am  gear  carb
+#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#>  1:     6     0  21.0 160.0   110  3.90 2.620 16.46     1     4     4
+#>  2:     6     0  21.0 160.0   110  3.90 2.875 17.02     1     4     4
+#>  3:     6     0  19.7 145.0   175  3.62 2.770 15.50     1     5     6
+#>  4:     4     1  22.8 108.0    93  3.85 2.320 18.61     1     4     1
+#>  5:     4     1  24.4 146.7    62  3.69 3.190 20.00     0     4     2
+#>  6:     4     1  22.8 140.8    95  3.92 3.150 22.90     0     4     2
+#>  7:     4     1  32.4  78.7    66  4.08 2.200 19.47     1     4     1
+#>  8:     4     1  30.4  75.7    52  4.93 1.615 18.52     1     4     2
+#>  9:     4     1  33.9  71.1    65  4.22 1.835 19.90     1     4     1
+#> 10:     4     1  21.5 120.1    97  3.70 2.465 20.01     0     3     1
+#> 11:     4     1  27.3  79.0    66  4.08 1.935 18.90     1     4     1
+#> 12:     4     1  30.4  95.1   113  3.77 1.513 16.90     1     5     2
+#> 13:     4     1  21.4 121.0   109  4.11 2.780 18.60     1     4     2
+#> 14:     6     1  21.4 258.0   110  3.08 3.215 19.44     0     3     1
+#> 15:     6     1  18.1 225.0   105  2.76 3.460 20.22     0     3     1
+#> 16:     6     1  19.2 167.6   123  3.92 3.440 18.30     0     4     4
+#> 17:     6     1  17.8 167.6   123  3.92 3.440 18.90     0     4     4
+#> 18:     8     0  18.7 360.0   175  3.15 3.440 17.02     0     3     2
+#> 19:     8     0  14.3 360.0   245  3.21 3.570 15.84     0     3     4
+#> 20:     8     0  16.4 275.8   180  3.07 4.070 17.40     0     3     3
+#> 21:     8     0  17.3 275.8   180  3.07 3.730 17.60     0     3     3
+#> 22:     8     0  15.2 275.8   180  3.07 3.780 18.00     0     3     3
+#> 23:     8     0  10.4 472.0   205  2.93 5.250 17.98     0     3     4
+#> 24:     8     0  10.4 460.0   215  3.00 5.424 17.82     0     3     4
+#> 25:     8     0  14.7 440.0   230  3.23 5.345 17.42     0     3     4
+#> 26:     8     0  15.5 318.0   150  2.76 3.520 16.87     0     3     2
+#> 27:     8     0  15.2 304.0   150  3.15 3.435 17.30     0     3     2
+#> 28:     8     0  13.3 350.0   245  3.73 3.840 15.41     0     3     4
+#> 29:     8     0  19.2 400.0   175  3.08 3.845 17.05     0     3     2
+#> 30:     8     0  15.8 351.0   264  4.22 3.170 14.50     1     5     4
+#> 31:     8     0  15.0 301.0   335  3.54 3.570 14.60     1     5     8
+#> 32:     4     0  26.0 120.3    91  4.43 2.140 16.70     1     5     2
+#>       cyl    vs   mpg  disp    hp  drat    wt  qsec    am  gear  carb
+ mtcars %>% nest("cyl|vs") %>%
+   unnest("ndt")
+#>       cyl    vs   mpg  disp    hp  drat    wt  qsec    am  gear  carb
+#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
+#>  1:     6     0  21.0 160.0   110  3.90 2.620 16.46     1     4     4
+#>  2:     6     0  21.0 160.0   110  3.90 2.875 17.02     1     4     4
+#>  3:     6     0  19.7 145.0   175  3.62 2.770 15.50     1     5     6
+#>  4:     4     1  22.8 108.0    93  3.85 2.320 18.61     1     4     1
+#>  5:     4     1  24.4 146.7    62  3.69 3.190 20.00     0     4     2
+#>  6:     4     1  22.8 140.8    95  3.92 3.150 22.90     0     4     2
+#>  7:     4     1  32.4  78.7    66  4.08 2.200 19.47     1     4     1
+#>  8:     4     1  30.4  75.7    52  4.93 1.615 18.52     1     4     2
+#>  9:     4     1  33.9  71.1    65  4.22 1.835 19.90     1     4     1
+#> 10:     4     1  21.5 120.1    97  3.70 2.465 20.01     0     3     1
+#> 11:     4     1  27.3  79.0    66  4.08 1.935 18.90     1     4     1
+#> 12:     4     1  30.4  95.1   113  3.77 1.513 16.90     1     5     2
+#> 13:     4     1  21.4 121.0   109  4.11 2.780 18.60     1     4     2
+#> 14:     6     1  21.4 258.0   110  3.08 3.215 19.44     0     3     1
+#> 15:     6     1  18.1 225.0   105  2.76 3.460 20.22     0     3     1
+#> 16:     6     1  19.2 167.6   123  3.92 3.440 18.30     0     4     4
+#> 17:     6     1  17.8 167.6   123  3.92 3.440 18.90     0     4     4
+#> 18:     8     0  18.7 360.0   175  3.15 3.440 17.02     0     3     2
+#> 19:     8     0  14.3 360.0   245  3.21 3.570 15.84     0     3     4
+#> 20:     8     0  16.4 275.8   180  3.07 4.070 17.40     0     3     3
+#> 21:     8     0  17.3 275.8   180  3.07 3.730 17.60     0     3     3
+#> 22:     8     0  15.2 275.8   180  3.07 3.780 18.00     0     3     3
+#> 23:     8     0  10.4 472.0   205  2.93 5.250 17.98     0     3     4
+#> 24:     8     0  10.4 460.0   215  3.00 5.424 17.82     0     3     4
+#> 25:     8     0  14.7 440.0   230  3.23 5.345 17.42     0     3     4
+#> 26:     8     0  15.5 318.0   150  2.76 3.520 16.87     0     3     2
+#> 27:     8     0  15.2 304.0   150  3.15 3.435 17.30     0     3     2
+#> 28:     8     0  13.3 350.0   245  3.73 3.840 15.41     0     3     4
+#> 29:     8     0  19.2 400.0   175  3.08 3.845 17.05     0     3     2
+#> 30:     8     0  15.8 351.0   264  4.22 3.170 14.50     1     5     4
+#> 31:     8     0  15.0 301.0   335  3.54 3.570 14.60     1     5     8
+#> 32:     4     0  26.0 120.3    91  4.43 2.140 16.70     1     5     2
+#>       cyl    vs   mpg  disp    hp  drat    wt  qsec    am  gear  carb
+
+df <- data.table(
+  a = list(c("a", "b"), "c"),
+  b = list(c(TRUE,TRUE),FALSE),
+  c = list(3,c(1,2)),
+  d = c(11, 22)
+)
+
+df
+#>         a         b      c     d
+#>    <list>    <list> <list> <num>
+#> 1:    a,b TRUE,TRUE      3    11
+#> 2:      c     FALSE    1,2    22
+df %>% unnest(a)
+#>        d      a
+#>    <num> <char>
+#> 1:    11      a
+#> 2:    11      b
+#> 3:    22      c
+df %>% unnest(2)
+#>        d      b
+#>    <num> <lgcl>
+#> 1:    11   TRUE
+#> 2:    11   TRUE
+#> 3:    22  FALSE
+df %>% unnest("c")
+#>        d     c
+#>    <num> <num>
+#> 1:    11     3
+#> 2:    22     1
+#> 3:    22     2
+df %>% unnest(cols = names(df)[3])
+#>        d     c
+#>    <num> <num>
+#> 1:    11     3
+#> 2:    22     1
+#> 3:    22     2
+
+# You can unnest multiple columns simultaneously
+df %>% unnest(1:3)
+#> Key: <d>
+#>        d      a      b     c
+#>    <num> <char> <lgcl> <num>
+#> 1:    11      a   TRUE     3
+#> 2:    11      a   TRUE     3
+#> 3:    11      b   TRUE     3
+#> 4:    11      b   TRUE     3
+#> 5:    22      c  FALSE     1
+#> 6:    22      c  FALSE     2
+df %>% unnest(a,b,c)
+#> Key: <d>
+#>        d      a      b     c
+#>    <num> <char> <lgcl> <num>
+#> 1:    11      a   TRUE     3
+#> 2:    11      a   TRUE     3
+#> 3:    11      b   TRUE     3
+#> 4:    11      b   TRUE     3
+#> 5:    22      c  FALSE     1
+#> 6:    22      c  FALSE     2
+df %>% unnest("a|b|c")
+#> Key: <d>
+#>        d      a      b     c
+#>    <num> <char> <lgcl> <num>
+#> 1:    11      a   TRUE     3
+#> 2:    11      a   TRUE     3
+#> 3:    11      b   TRUE     3
+#> 4:    11      b   TRUE     3
+#> 5:    22      c  FALSE     1
+#> 6:    22      c  FALSE     2
+
+# examples for squeeze
+# nest which columns?
+iris %>% squeeze(1:2)
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species     ndt
+#>             <num>       <num>        <num>       <num>    <fctr>  <list>
+#>   1:          5.1         3.5          1.4         0.2    setosa 5.1,3.5
+#>   2:          4.9         3.0          1.4         0.2    setosa 4.9,3.0
+#>   3:          4.7         3.2          1.3         0.2    setosa 4.7,3.2
+#>   4:          4.6         3.1          1.5         0.2    setosa 4.6,3.1
+#>   5:          5.0         3.6          1.4         0.2    setosa 5.0,3.6
+#>  ---                                                                    
+#> 146:          6.7         3.0          5.2         2.3 virginica 6.7,3.0
+#> 147:          6.3         2.5          5.0         1.9 virginica 6.3,2.5
+#> 148:          6.5         3.0          5.2         2.0 virginica 6.5,3.0
+#> 149:          6.2         3.4          5.4         2.3 virginica 6.2,3.4
+#> 150:          5.9         3.0          5.1         1.8 virginica 5.9,3.0
+iris %>% squeeze("Se")
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species     ndt
+#>             <num>       <num>        <num>       <num>    <fctr>  <list>
+#>   1:          5.1         3.5          1.4         0.2    setosa 5.1,3.5
+#>   2:          4.9         3.0          1.4         0.2    setosa 4.9,3.0
+#>   3:          4.7         3.2          1.3         0.2    setosa 4.7,3.2
+#>   4:          4.6         3.1          1.5         0.2    setosa 4.6,3.1
+#>   5:          5.0         3.6          1.4         0.2    setosa 5.0,3.6
+#>  ---                                                                    
+#> 146:          6.7         3.0          5.2         2.3 virginica 6.7,3.0
+#> 147:          6.3         2.5          5.0         1.9 virginica 6.3,2.5
+#> 148:          6.5         3.0          5.2         2.0 virginica 6.5,3.0
+#> 149:          6.2         3.4          5.4         2.3 virginica 6.2,3.4
+#> 150:          5.9         3.0          5.1         1.8 virginica 5.9,3.0
+iris %>% squeeze(Sepal.Length:Petal.Width)
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species     ndt
+#>             <num>       <num>        <num>       <num>    <fctr>  <list>
+#>   1:          5.1         3.5          1.4         0.2    setosa 5.1,3.5
+#>   2:          4.9         3.0          1.4         0.2    setosa 4.9,3.0
+#>   3:          4.7         3.2          1.3         0.2    setosa 4.7,3.2
+#>   4:          4.6         3.1          1.5         0.2    setosa 4.6,3.1
+#>   5:          5.0         3.6          1.4         0.2    setosa 5.0,3.6
+#>  ---                                                                    
+#> 146:          6.7         3.0          5.2         2.3 virginica 6.7,3.0
+#> 147:          6.3         2.5          5.0         1.9 virginica 6.3,2.5
+#> 148:          6.5         3.0          5.2         2.0 virginica 6.5,3.0
+#> 149:          6.2         3.4          5.4         2.3 virginica 6.2,3.4
+#> 150:          5.9         3.0          5.1         1.8 virginica 5.9,3.0
+
+# examples for chop
+df <- data.table(x = c(1, 1, 1, 2, 2, 3), y = 1:6, z = 6:1)
+df %>% chop(y,z)
+#>        x      y      z
+#>    <num> <list> <list>
+#> 1:     1  1,2,3  6,5,4
+#> 2:     2    4,5    3,2
+#> 3:     3      6      1
+df %>% chop(y,z) %>% unchop(y,z)
+#>        x     y     z
+#>    <num> <int> <int>
+#> 1:     1     1     6
+#> 2:     1     2     5
+#> 3:     1     3     4
+#> 4:     2     4     3
+#> 5:     2     5     2
+#> 6:     3     6     1
+
+
+
- + + - - - + diff --git a/docs/reference/nth.html b/docs/reference/nth.html index 3ad787c..487ab3c 100644 --- a/docs/reference/nth.html +++ b/docs/reference/nth.html @@ -1,72 +1,12 @@ - - - - - - - -Extract the nth value from a vector — nth • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Extract the nth value from a vector — nth • tidyft - + - - -
-
- - -
-
+
@@ -134,62 +59,64 @@

Extract the nth value from a vector

Get the value from a vector with its position.

-
nth(v, n = 1)
- -

Arguments

- - - - - - - - - - -
v

A vector

n

A single integer specifying the position. Default uses 1. +

+
nth(v, n = 1)
+
+ +
+

Arguments

+ + +
v
+

A vector

+ + +
n
+

A single integer specifying the position. Default uses 1. Negative integers index from the end (i.e. -1L will return the last value in the vector). - If a double is supplied, it will be silently truncated.

- -

Value

+ If a double is supplied, it will be silently truncated.

+
+
+

Value

A single value.

+
-

Examples

-
-x = 1:10 -nth(x, 1)
#> [1] 1
nth(x, 5)
#> [1] 5
nth(x, -2)
#> [1] 9
-
-
- +
- + + - - - + diff --git a/docs/reference/object_size.html b/docs/reference/object_size.html index f26b240..6b88c4e 100644 --- a/docs/reference/object_size.html +++ b/docs/reference/object_size.html @@ -1,73 +1,13 @@ - - - - - - - -Nice printing of report the Space Allocated for an Object — object_size • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Nice printing of report the Space Allocated for an Object — object_size • tidyft - + - - -
-
- - -
-
+
@@ -136,54 +61,52 @@

Nice printing of report the Space Allocated for an Object

A wrapper of `object.size`, but use a nicer printing unit.

-
object_size(object)
+
+
object_size(object)
+
-

Arguments

- - - - - - -
object

an R object.

+
+

Arguments

-

Value

-

An object of class "object_size"

+
object
+

an R object.

-

Examples

-
-iris %>% object_size()
#> 7.1 Kb
-
-
- +
+

Value

+

An object of class "object_size"

+
+
+

Examples

+

+iris %>% object_size()
+#> 7.1 Kb
+
+
+
+
-
+ + - - - + diff --git a/docs/reference/pull.html b/docs/reference/pull.html index 81e8a01..64cb775 100644 --- a/docs/reference/pull.html +++ b/docs/reference/pull.html @@ -1,72 +1,12 @@ - - - - - - - -Pull out a single variable — pull • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Pull out a single variable — pull • tidyft + - - - -
-
- - -
-
+
@@ -134,60 +59,62 @@

Pull out a single variable

Analogous function for pull in dplyr

-
pull(.data, col)
+
+
pull(.data, col)
+
-

Arguments

- - - - - - - - - - -
.data

data.frame

col

A name of column or index (should be positive).

+
+

Arguments

-

Value

-

A vector

-

See also

+
.data
+

data.frame

- -

Examples

-
mtcars %>% pull(2)
#> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
mtcars %>% pull(cyl)
#> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
mtcars %>% pull("cyl")
#> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
-
- +
+

Value

+

A vector

+
+
+

See also

+ +
+
+

Examples

+
mtcars %>% pull(2)
+#>  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
+mtcars %>% pull(cyl)
+#>  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
+mtcars %>% pull("cyl")
+#>  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
+
+
+
-
+ + - - - + diff --git a/docs/reference/read_csv.html b/docs/reference/read_csv.html index 9605df6..70b0eff 100644 --- a/docs/reference/read_csv.html +++ b/docs/reference/read_csv.html @@ -1,73 +1,13 @@ - - - - - - - -Convenient file reader — read_csv • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Convenient file reader — read_csv • tidyft - + - - -
-
- - -
-
+
@@ -136,57 +61,52 @@

Convenient file reader

Highlighting the encoding.

-
read_csv(path, utf8 = FALSE, ...)
- -

Arguments

- - - - - - - - - - - - - - -
path

File name in working directory, path to file.

utf8

Should "UTF-8" used as the encoding? (Defaults to FALSE)

...

Other parameters passed to data.table::fread.

- -

Value

+
+
read_csv(path, utf8 = FALSE, ...)
+
-

A data.table

+
+

Arguments

-
- +
+

Value

+

A data.table

+
+
-
+ + - - - + diff --git a/docs/reference/reexports.html b/docs/reference/reexports.html index f424dc7..cbcde5d 100644 --- a/docs/reference/reexports.html +++ b/docs/reference/reexports.html @@ -1,79 +1,23 @@ - - - - - - - -Objects exported from other packages — reexports • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Objects exported from other packages — reexports • tidyft - - + stringr +%&gt;% - - - - - - - - + - -
-
- - -
-
+

These objects are imported from other packages. Follow the links below to see their documentation.

-
-
data.table

as.data.table, CJ, copy, data.table, fcoalesce, fread, fwrite, rbindlist, setDT, setnames, tables, transpose, uniqueN

+
data.table
+

as.data.table, CJ, copy, data.table, fcoalesce, fread, fwrite, rbindlist, rleid, rleidv, setDT, setnames, tables, transpose, uniqueN

-
stringr

%>%

-
-
+
stringr
+

%>%

+
-
- +
-
+ + - - - + diff --git a/docs/reference/relocate.html b/docs/reference/relocate.html index a0bf3aa..bd64bb8 100644 --- a/docs/reference/relocate.html +++ b/docs/reference/relocate.html @@ -1,74 +1,14 @@ - - - - - - - -Change column order — relocate • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Change column order — relocate • tidyft - - - - - - - - - - - - - + - -
-
- - -
-
+
@@ -138,103 +63,123 @@

Change column order

as `relocate()` in dplyr.

-
relocate(.data, ..., how = "first", where = NULL)
- -

Arguments

- - - - - - - - - - - - - - - - - - -
.data

A data.table

...

Columns to move

how

The mode of movement, including "first","last","after","before". -Default uses "first".

where

Destination of columns selected by .... -Applicable for "after" and "before" mode.

- -

Value

+
+
relocate(.data, ..., how = "first", where = NULL)
+
-

A data.table with rearranged columns.

-

Details

+
+

Arguments

-

Once you relocate the columns, the order changes forever.

-

Examples

-
df <- data.table(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a") -df
#> a b c d e f -#> <num> <num> <num> <char> <char> <char> -#> 1: 1 1 1 a a a
df %>% relocate(f)
#> f a b c d e -#> <char> <num> <num> <num> <char> <char> -#> 1: a 1 1 1 a a
df %>% relocate(a,how = "last")
#> f b c d e a -#> <char> <num> <num> <char> <char> <num> -#> 1: a 1 1 a a 1
-df %>% relocate(is.character)
#> f d e b c a -#> <char> <char> <char> <num> <num> <num> -#> 1: a a a 1 1 1
df %>% relocate(is.numeric, how = "last")
#> f d e b c a -#> <char> <char> <char> <num> <num> <num> -#> 1: a a a 1 1 1
df %>% relocate("[aeiou]")
#> e a f d b c -#> <char> <num> <char> <char> <num> <num> -#> 1: a 1 a a 1 1
-df %>% relocate(a, how = "after",where = f)
#> e f a d b c -#> <char> <char> <num> <char> <num> <num> -#> 1: a a 1 a 1 1
df %>% relocate(f, how = "before",where = a)
#> e f a d b c -#> <char> <char> <num> <char> <num> <num> -#> 1: a a 1 a 1 1
df %>% relocate(f, how = "before",where = c)
#> e a d b f c -#> <char> <num> <char> <num> <char> <num> -#> 1: a 1 a 1 a 1
df %>% relocate(f, how = "after",where = c)
#> e a d b c f -#> <char> <num> <char> <num> <num> <char> -#> 1: a 1 a 1 1 a
-df2 <- data.table(a = 1, b = "a", c = 1, d = "a") -df2 %>% relocate(is.numeric, - how = "after", - where = is.character)
#> b d a c -#> <char> <char> <num> <num> -#> 1: a a 1 1
df2 %>% relocate(is.numeric, - how="before", - where = is.character)
#> a c b d -#> <num> <num> <char> <char> -#> 1: 1 1 a a
-
- +
+

Value

+

A data.table with rearranged columns.

+
+
+

Details

+

Once you relocate the columns, the order changes forever.

+
+ +
+

Examples

+
df <- data.table(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a")
+df
+#>        a     b     c      d      e      f
+#>    <num> <num> <num> <char> <char> <char>
+#> 1:     1     1     1      a      a      a
+df %>% relocate(f)
+#>         f     a     b     c      d      e
+#>    <char> <num> <num> <num> <char> <char>
+#> 1:      a     1     1     1      a      a
+df %>% relocate(a,how = "last")
+#>         f     b     c      d      e     a
+#>    <char> <num> <num> <char> <char> <num>
+#> 1:      a     1     1      a      a     1
+
+df %>% relocate(is.character)
+#>         f      d      e     b     c     a
+#>    <char> <char> <char> <num> <num> <num>
+#> 1:      a      a      a     1     1     1
+df %>% relocate(is.numeric, how = "last")
+#>         f      d      e     b     c     a
+#>    <char> <char> <char> <num> <num> <num>
+#> 1:      a      a      a     1     1     1
+df %>% relocate("[aeiou]")
+#>         e     a      f      d     b     c
+#>    <char> <num> <char> <char> <num> <num>
+#> 1:      a     1      a      a     1     1
+
+df %>% relocate(a, how = "after",where = f)
+#>         e      f     a      d     b     c
+#>    <char> <char> <num> <char> <num> <num>
+#> 1:      a      a     1      a     1     1
+df %>% relocate(f, how = "before",where = a)
+#>         e      f     a      d     b     c
+#>    <char> <char> <num> <char> <num> <num>
+#> 1:      a      a     1      a     1     1
+df %>% relocate(f, how = "before",where = c)
+#>         e     a      d     b      f     c
+#>    <char> <num> <char> <num> <char> <num>
+#> 1:      a     1      a     1      a     1
+df %>% relocate(f, how = "after",where = c)
+#>         e     a      d     b     c      f
+#>    <char> <num> <char> <num> <num> <char>
+#> 1:      a     1      a     1     1      a
+
+df2 <- data.table(a = 1, b = "a", c = 1, d = "a")
+df2 %>% relocate(is.numeric,
+                    how = "after",
+                    where = is.character)
+#>         b      d     a     c
+#>    <char> <char> <num> <num>
+#> 1:      a      a     1     1
+df2 %>% relocate(is.numeric,
+                    how="before",
+                    where = is.character)
+#>        a     c      b      d
+#>    <num> <num> <char> <char>
+#> 1:     1     1      a      a
+
+
+
-
+ + - - - + diff --git a/docs/reference/replace_vars.html b/docs/reference/replace_vars.html index f7d6dcd..9216e40 100644 --- a/docs/reference/replace_vars.html +++ b/docs/reference/replace_vars.html @@ -1,73 +1,13 @@ - - - - - - - -Fast value replacement in data frame — replace_vars • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Fast value replacement in data frame — replace_vars • tidyft - + - - -
-
- - -
-
+
@@ -136,138 +61,144 @@

Fast value replacement in data frame

that match specific patterns to another specific value in a data.table.

-
replace_vars(.data, ..., from = is.na, to)
- -

Arguments

- - - - - - - - - - - - - - - - - - -
.data

A data.table

...

Colunms to be replaced. If not specified, use all columns.

from

A value, a vector of values or a function returns a logical value. -Defaults to NaN.

to

A value.

- -

Value

+
+
replace_vars(.data, ..., from = is.na, to)
+
+ +
+

Arguments

+ + +
.data
+

A data.table

+ + +
...
+

Colunms to be replaced. If not specified, use all columns.

+ +
from
+

A value, a vector of values or a function returns a logical value. +Defaults to NaN.

+ + +
to
+

A value.

+ +
+
+

Value

A data.table.

-

See also

- - - -

Examples

-
iris %>% as.data.table() %>% - mutate(Species = as.character(Species))-> new_iris - - new_iris %>% - replace_vars(Species, from = "setosa",to = "SS")
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <char> -#> 1: 5.1 3.5 1.4 0.2 SS -#> 2: 4.9 3.0 1.4 0.2 SS -#> 3: 4.7 3.2 1.3 0.2 SS -#> 4: 4.6 3.1 1.5 0.2 SS -#> 5: 5.0 3.6 1.4 0.2 SS -#> --- -#> 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
new_iris %>% - replace_vars(Species,from = c("setosa","virginica"),to = "sv")
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <char> -#> 1: 5.1 3.5 1.4 0.2 SS -#> 2: 4.9 3.0 1.4 0.2 SS -#> 3: 4.7 3.2 1.3 0.2 SS -#> 4: 4.6 3.1 1.5 0.2 SS -#> 5: 5.0 3.6 1.4 0.2 SS -#> --- -#> 146: 6.7 3.0 5.2 2.3 sv -#> 147: 6.3 2.5 5.0 1.9 sv -#> 148: 6.5 3.0 5.2 2.0 sv -#> 149: 6.2 3.4 5.4 2.3 sv -#> 150: 5.9 3.0 5.1 1.8 sv
new_iris %>% - replace_vars(Petal.Width, from = .2,to = 2)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <char> -#> 1: 5.1 3.5 1.4 2.0 SS -#> 2: 4.9 3.0 1.4 2.0 SS -#> 3: 4.7 3.2 1.3 2.0 SS -#> 4: 4.6 3.1 1.5 2.0 SS -#> 5: 5.0 3.6 1.4 2.0 SS -#> --- -#> 146: 6.7 3.0 5.2 2.3 sv -#> 147: 6.3 2.5 5.0 1.9 sv -#> 148: 6.5 3.0 5.2 2.0 sv -#> 149: 6.2 3.4 5.4 2.3 sv -#> 150: 5.9 3.0 5.1 1.8 sv
new_iris %>% - replace_vars(from = .2,to = NA)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <char> -#> 1: 5.1 3.5 1.4 2.0 SS -#> 2: 4.9 3.0 1.4 2.0 SS -#> 3: 4.7 3.2 1.3 2.0 SS -#> 4: 4.6 3.1 1.5 2.0 SS -#> 5: 5.0 3.6 1.4 2.0 SS -#> --- -#> 146: 6.7 3.0 5.2 2.3 sv -#> 147: 6.3 2.5 5.0 1.9 sv -#> 148: 6.5 3.0 5.2 2.0 sv -#> 149: 6.2 3.4 5.4 2.3 sv -#> 150: 5.9 3.0 5.1 1.8 sv
new_iris %>% - replace_vars(is.numeric, from = function(x) x > 3, to = 9999 )
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <char> -#> 1: 9999 9999.0 1.4 2.0 SS -#> 2: 9999 3.0 1.4 2.0 SS -#> 3: 9999 9999.0 1.3 2.0 SS -#> 4: 9999 9999.0 1.5 2.0 SS -#> 5: 9999 9999.0 1.4 2.0 SS -#> --- -#> 146: 9999 3.0 9999.0 2.3 sv -#> 147: 9999 2.5 9999.0 1.9 sv -#> 148: 9999 3.0 9999.0 2.0 sv -#> 149: 9999 9999.0 9999.0 2.3 sv -#> 150: 9999 3.0 9999.0 1.8 sv
-
-
- +
+

See also

+ +
+
+

Examples

+
 iris %>% as.data.table() %>%
+   mutate(Species = as.character(Species))-> new_iris
+
+ new_iris %>%
+   replace_vars(Species, from = "setosa",to = "SS")
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>             <num>       <num>        <num>       <num>    <char>
+#>   1:          5.1         3.5          1.4         0.2        SS
+#>   2:          4.9         3.0          1.4         0.2        SS
+#>   3:          4.7         3.2          1.3         0.2        SS
+#>   4:          4.6         3.1          1.5         0.2        SS
+#>   5:          5.0         3.6          1.4         0.2        SS
+#>  ---                                                            
+#> 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
+ new_iris %>%
+   replace_vars(Species,from = c("setosa","virginica"),to = "sv")
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
+#>             <num>       <num>        <num>       <num>  <char>
+#>   1:          5.1         3.5          1.4         0.2      SS
+#>   2:          4.9         3.0          1.4         0.2      SS
+#>   3:          4.7         3.2          1.3         0.2      SS
+#>   4:          4.6         3.1          1.5         0.2      SS
+#>   5:          5.0         3.6          1.4         0.2      SS
+#>  ---                                                          
+#> 146:          6.7         3.0          5.2         2.3      sv
+#> 147:          6.3         2.5          5.0         1.9      sv
+#> 148:          6.5         3.0          5.2         2.0      sv
+#> 149:          6.2         3.4          5.4         2.3      sv
+#> 150:          5.9         3.0          5.1         1.8      sv
+ new_iris %>%
+   replace_vars(Petal.Width, from = .2,to = 2)
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
+#>             <num>       <num>        <num>       <num>  <char>
+#>   1:          5.1         3.5          1.4         2.0      SS
+#>   2:          4.9         3.0          1.4         2.0      SS
+#>   3:          4.7         3.2          1.3         2.0      SS
+#>   4:          4.6         3.1          1.5         2.0      SS
+#>   5:          5.0         3.6          1.4         2.0      SS
+#>  ---                                                          
+#> 146:          6.7         3.0          5.2         2.3      sv
+#> 147:          6.3         2.5          5.0         1.9      sv
+#> 148:          6.5         3.0          5.2         2.0      sv
+#> 149:          6.2         3.4          5.4         2.3      sv
+#> 150:          5.9         3.0          5.1         1.8      sv
+ new_iris %>%
+   replace_vars(from = .2,to = NA)
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
+#>             <num>       <num>        <num>       <num>  <char>
+#>   1:          5.1         3.5          1.4         2.0      SS
+#>   2:          4.9         3.0          1.4         2.0      SS
+#>   3:          4.7         3.2          1.3         2.0      SS
+#>   4:          4.6         3.1          1.5         2.0      SS
+#>   5:          5.0         3.6          1.4         2.0      SS
+#>  ---                                                          
+#> 146:          6.7         3.0          5.2         2.3      sv
+#> 147:          6.3         2.5          5.0         1.9      sv
+#> 148:          6.5         3.0          5.2         2.0      sv
+#> 149:          6.2         3.4          5.4         2.3      sv
+#> 150:          5.9         3.0          5.1         1.8      sv
+ new_iris %>%
+   replace_vars(is.numeric, from = function(x) x > 3, to = 9999 )
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
+#>             <num>       <num>        <num>       <num>  <char>
+#>   1:         9999      9999.0          1.4         2.0      SS
+#>   2:         9999         3.0          1.4         2.0      SS
+#>   3:         9999      9999.0          1.3         2.0      SS
+#>   4:         9999      9999.0          1.5         2.0      SS
+#>   5:         9999      9999.0          1.4         2.0      SS
+#>  ---                                                          
+#> 146:         9999         3.0       9999.0         2.3      sv
+#> 147:         9999         2.5       9999.0         1.9      sv
+#> 148:         9999         3.0       9999.0         2.0      sv
+#> 149:         9999      9999.0       9999.0         2.3      sv
+#> 150:         9999         3.0       9999.0         1.8      sv
+
+
+
+
-
+ + - - - + diff --git a/docs/reference/rowwise.html b/docs/reference/rowwise.html index 92220de..b0a77e2 100644 --- a/docs/reference/rowwise.html +++ b/docs/reference/rowwise.html @@ -1,74 +1,14 @@ - - - - - - - -Computation by rows — rowwise_mutate • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Computation by rows — rowwise_mutate • tidyft - - - - - - - - - - - - - + - -
-
- - -
-
+
@@ -138,89 +63,91 @@

Computation by rows

Only mutate and summarise are supported so far.

-
rowwise_mutate(.data, ...)
+    
+
rowwise_mutate(.data, ...)
+
+rowwise_summarise(.data, ...)
+
-rowwise_summarise(.data, ...)
+
+

Arguments

-

Arguments

- - - - - - - - - - -
.data

A data.table

...

Name-value pairs of expressions

-

Value

+
.data
+

A data.table

+ +
...
+

Name-value pairs of expressions

+ +
+
+

Value

A data.table

-

See also

- - - -

Examples

-
# without rowwise -df <- data.table(x = 1:2, y = 3:4, z = 4:5) -df %>% mutate(m = mean(c(x, y, z)))
#> x y z m -#> <int> <int> <int> <num> -#> 1: 1 3 4 3.166667 -#> 2: 2 4 5 3.166667
# with rowwise -df <- data.table(x = 1:2, y = 3:4, z = 4:5) -df %>% rowwise_mutate(m = mean(c(x, y, z)))
#> x y z m -#> <int> <int> <int> <num> -#> 1: 1 3 4 2.666667 -#> 2: 2 4 5 3.666667
- -# # rowwise is also useful when doing simulations -params = fread(" sim n mean sd - 1 1 1 1 - 2 2 2 4 - 3 3 -1 2") - -params %>% - rowwise_summarise(sim,z = rnorm(n,mean,sd))
#> sim z -#> <int> <num> -#> 1: 1 1.9353632 -#> 2: 2 2.7059544 -#> 3: 2 2.9747419 -#> 4: 3 2.2470978 -#> 5: 3 -0.7759238 -#> 6: 3 -1.2679940
-
-
- +
+

See also

+ +
+
+

Examples

+
# without rowwise
+df <- data.table(x = 1:2, y = 3:4, z = 4:5)
+df %>% mutate(m = mean(c(x, y, z)))
+#>        x     y     z        m
+#>    <int> <int> <int>    <num>
+#> 1:     1     3     4 3.166667
+#> 2:     2     4     5 3.166667
+# with rowwise
+df <- data.table(x = 1:2, y = 3:4, z = 4:5)
+df %>% rowwise_mutate(m = mean(c(x, y, z)))
+#>        x     y     z        m
+#>    <int> <int> <int>    <num>
+#> 1:     1     3     4 2.666667
+#> 2:     2     4     5 3.666667
+
+
+# # rowwise is also useful when doing simulations
+params = fread(" sim n mean sd
+  1  1     1   1
+  2  2     2   4
+  3  3    -1   2")
+
+params %>%
+  rowwise_summarise(sim,z = rnorm(n,mean,sd))
+#>      sim          z
+#>    <int>      <num>
+#> 1:     1  1.9353632
+#> 2:     2  2.7059544
+#> 3:     2  2.9747419
+#> 4:     3  2.2470978
+#> 5:     3 -0.7759238
+#> 6:     3 -1.2679940
+
+
+
+
-
+ + - - - + diff --git a/docs/reference/select.html b/docs/reference/select.html index a4d3538..bbf6e8b 100644 --- a/docs/reference/select.html +++ b/docs/reference/select.html @@ -1,74 +1,14 @@ - - - - - - - -Select/rename variables by name — select • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Select/rename variables by name — select • tidyft - - - - - - - - - - - + - - - -
-
- - -
-
+
@@ -138,182 +63,194 @@

Select/rename variables by name

rename() keeps all variables.

-
select(.data, ...)
-
-select_vars(.data, ..., rm.dup = TRUE)
-
-select_dt(.data, ..., cols = NULL, negate = FALSE)
-
-select_mix(.data, ..., rm.dup = TRUE)
-
-rename(.data, ...)
- -

Arguments

- - - - - - - - - - - - - - - - - - - - - - -
.data

A data.table

...

One or more unquoted expressions separated by commas. -Very flexible, same as tidyfst::select_dt and tidyfst::select_mix. -details find select_dt.

rm.dup

Should duplicated columns be removed? Defaults to TRUE.

cols

(Optional)A numeric or character vector.

negate

Applicable when regular expression and "cols" is used. -If TRUE, return the non-matched pattern. Default uses FALSE.

- -

Value

+
+
select(.data, ...)
+
+select_vars(.data, ..., rm.dup = TRUE)
+
+select_dt(.data, ..., cols = NULL, negate = FALSE)
+
+select_mix(.data, ..., rm.dup = TRUE)
+
+rename(.data, ...)
+
+ +
+

Arguments

+ + +
.data
+

A data.table

+ + +
...
+

One or more unquoted expressions separated by commas. +Very flexible, same as tidyfst::select_dt and tidyfst::select_mix. +details find select_dt.

+ + +
rm.dup
+

Should duplicated columns be removed? Defaults to TRUE.

-

A data.table

-

Details

+
cols
+

(Optional)A numeric or character vector.

+ + +
negate
+

Applicable when regular expression and "cols" is used. +If TRUE, return the non-matched pattern. Default uses FALSE.

+ +
+
+

Value

+

A data.table

+
+
+

Details

No copy is made. Once you select or rename a data.table, they would be changed forever. select_vars could select across different data types, names and index. See examples.

select_dt and select_mix is the safe mode of select and select_vars, they keey the original copy but are not memory-efficient when dealing with large data sets.

-

See also

- - - -

Examples

-
- a = as.data.table(iris) - a %>% select(1:3)
#> Sepal.Length Sepal.Width Petal.Length -#> <num> <num> <num> -#> 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 -#> --- -#> 146: 6.7 3.0 5.2 -#> 147: 6.3 2.5 5.0 -#> 148: 6.5 3.0 5.2 -#> 149: 6.2 3.4 5.4 -#> 150: 5.9 3.0 5.1
a
#> Sepal.Length Sepal.Width Petal.Length -#> <num> <num> <num> -#> 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 -#> --- -#> 146: 6.7 3.0 5.2 -#> 147: 6.3 2.5 5.0 -#> 148: 6.5 3.0 5.2 -#> 149: 6.2 3.4 5.4 -#> 150: 5.9 3.0 5.1
- a = as.data.table(iris) - a %>% select_vars(is.factor,"Se")
#> Sepal.Length Sepal.Width Species -#> <num> <num> <fctr> -#> 1: 5.1 3.5 setosa -#> 2: 4.9 3.0 setosa -#> 3: 4.7 3.2 setosa -#> 4: 4.6 3.1 setosa -#> 5: 5.0 3.6 setosa -#> --- -#> 146: 6.7 3.0 virginica -#> 147: 6.3 2.5 virginica -#> 148: 6.5 3.0 virginica -#> 149: 6.2 3.4 virginica -#> 150: 5.9 3.0 virginica
a
#> Sepal.Length Sepal.Width Species -#> <num> <num> <fctr> -#> 1: 5.1 3.5 setosa -#> 2: 4.9 3.0 setosa -#> 3: 4.7 3.2 setosa -#> 4: 4.6 3.1 setosa -#> 5: 5.0 3.6 setosa -#> --- -#> 146: 6.7 3.0 virginica -#> 147: 6.3 2.5 virginica -#> 148: 6.5 3.0 virginica -#> 149: 6.2 3.4 virginica -#> 150: 5.9 3.0 virginica
- a = as.data.table(iris) - a %>% select("Se") %>% - rename(sl = Sepal.Length, - sw = Sepal.Width)
#> sl sw -#> <num> <num> -#> 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
a
#> sl sw -#> <num> <num> -#> 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
- -DT = data.table(a=1:2,b=3:4,c=5:6) -DT
#> a b c -#> <int> <int> <int> -#> 1: 1 3 5 -#> 2: 2 4 6
DT %>% rename(B=b)
#> a B c -#> <int> <int> <int> -#> 1: 1 3 5 -#> 2: 2 4 6
-
-
- +
+

See also

+ +
+
+

Examples

+

+  a = as.data.table(iris)
+  a %>% select(1:3)
+#>      Sepal.Length Sepal.Width Petal.Length
+#>             <num>       <num>        <num>
+#>   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
+#>  ---                                      
+#> 146:          6.7         3.0          5.2
+#> 147:          6.3         2.5          5.0
+#> 148:          6.5         3.0          5.2
+#> 149:          6.2         3.4          5.4
+#> 150:          5.9         3.0          5.1
+  a
+#>      Sepal.Length Sepal.Width Petal.Length
+#>             <num>       <num>        <num>
+#>   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
+#>  ---                                      
+#> 146:          6.7         3.0          5.2
+#> 147:          6.3         2.5          5.0
+#> 148:          6.5         3.0          5.2
+#> 149:          6.2         3.4          5.4
+#> 150:          5.9         3.0          5.1
+
+  a = as.data.table(iris)
+  a %>% select_vars(is.factor,"Se")
+#>      Sepal.Length Sepal.Width   Species
+#>             <num>       <num>    <fctr>
+#>   1:          5.1         3.5    setosa
+#>   2:          4.9         3.0    setosa
+#>   3:          4.7         3.2    setosa
+#>   4:          4.6         3.1    setosa
+#>   5:          5.0         3.6    setosa
+#>  ---                                   
+#> 146:          6.7         3.0 virginica
+#> 147:          6.3         2.5 virginica
+#> 148:          6.5         3.0 virginica
+#> 149:          6.2         3.4 virginica
+#> 150:          5.9         3.0 virginica
+  a
+#>      Sepal.Length Sepal.Width   Species
+#>             <num>       <num>    <fctr>
+#>   1:          5.1         3.5    setosa
+#>   2:          4.9         3.0    setosa
+#>   3:          4.7         3.2    setosa
+#>   4:          4.6         3.1    setosa
+#>   5:          5.0         3.6    setosa
+#>  ---                                   
+#> 146:          6.7         3.0 virginica
+#> 147:          6.3         2.5 virginica
+#> 148:          6.5         3.0 virginica
+#> 149:          6.2         3.4 virginica
+#> 150:          5.9         3.0 virginica
+
+  a = as.data.table(iris)
+  a %>% select("Se") %>%
+    rename(sl = Sepal.Length,
+    sw = Sepal.Width)
+#>         sl    sw
+#>      <num> <num>
+#>   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
+  a
+#>         sl    sw
+#>      <num> <num>
+#>   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
+
+
+DT = data.table(a=1:2,b=3:4,c=5:6)
+DT
+#>        a     b     c
+#>    <int> <int> <int>
+#> 1:     1     3     5
+#> 2:     2     4     6
+DT %>% rename(B=b)
+#>        a     B     c
+#>    <int> <int> <int>
+#> 1:     1     3     5
+#> 2:     2     4     6
+
+
+
+
-
+ + - - - + diff --git a/docs/reference/separate.html b/docs/reference/separate.html index 9c9d4bb..0efa5e8 100644 --- a/docs/reference/separate.html +++ b/docs/reference/separate.html @@ -1,76 +1,14 @@ - - - - - - - -Separate a character column into two columns using -a regular expression separator — separate • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Separate a character column into two columns using a regular expression separator — separate • tidyft - - - - - - - - - - - - - + - -
-
- - -
-
+

Given either regular expression, separate() turns a single character column into two columns. -Analogous to tidyr::separate, but only split into two columns only.

+Analogous to tidyr::separate, but only split into two columns only.

-
separate(.data, separated_colname, into, sep = "[^[:alnum:]]+", remove = TRUE)
- -

Arguments

- - - - - - - - - - - - - - - - - - - - - - -
.data

A data frame.

separated_colname

Column name, string only.

into

Character vector of length 2.

sep

Separator between columns.

remove

If TRUE, remove input column from output data frame.

- -

Value

+
+
separate(.data, separated_colname, into, sep = "[^[:alnum:]]+", remove = TRUE)
+
+ +
+

Arguments

+ + +
.data
+

A data frame.

+ + +
separated_colname
+

Column name, string only.

+ + +
into
+

Character vector of length 2.

+ +
sep
+

Separator between columns.

+ + +
remove
+

If TRUE, remove input column from output data frame.

+ +
+
+

Value

A data.table

-

See also

- - - -

Examples

-
df <- data.table(x = c(NA, "a.b", "a.d", "b.c")) -df %>% separate(x, c("A", "B"))
#> A B -#> <char> <char> -#> 1: <NA> <NA> -#> 2: a b -#> 3: a d -#> 4: b c
# equals to -df <- data.table(x = c(NA, "a.b", "a.d", "b.c")) -df %>% separate("x", c("A", "B"))
#> A B -#> <char> <char> -#> 1: <NA> <NA> -#> 2: a b -#> 3: a d -#> 4: b c
-
- +
+

See also

+ +
+
+

Examples

+
df <- data.table(x = c(NA, "a.b", "a.d", "b.c"))
+df %>% separate(x, c("A", "B"))
+#>         A      B
+#>    <char> <char>
+#> 1:   <NA>   <NA>
+#> 2:      a      b
+#> 3:      a      d
+#> 4:      b      c
+# equals to
+df <- data.table(x = c(NA, "a.b", "a.d", "b.c"))
+df %>% separate("x", c("A", "B"))
+#>         A      B
+#>    <char> <char>
+#> 1:   <NA>   <NA>
+#> 2:      a      b
+#> 3:      a      d
+#> 4:      b      c
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/slice.html b/docs/reference/slice.html index 0f86c74..950fa93 100644 --- a/docs/reference/slice.html +++ b/docs/reference/slice.html @@ -1,78 +1,18 @@ - - - - - - - -Subset rows using their positions — slice • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Subset rows using their positions — slice • tidyft - - - - - - - - - - - - - + - -
-
- - -
-
+
@@ -146,217 +71,237 @@

Subset rows using their positions

of a variable.

-
slice(.data, ...)
-
-slice_head(.data, n)
-
-slice_tail(.data, n)
-
-slice_max(.data, order_by, n, with_ties = TRUE)
-
-slice_min(.data, order_by, n, with_ties = TRUE)
-
-slice_sample(.data, n, replace = FALSE)
- -

Arguments

- - - - - - - - - - - - - - - - - - - - - - - - - - -
.data

A data.table

...

Provide either positive values to keep, or negative values to drop. -The values provided must be either all positive or all negative.

n

When larger than or equal to 1, the number of rows. -When between 0 and 1, the proportion of rows to select.

order_by

Variable or function of variables to order by.

with_ties

Should ties be kept together? The default, `TRUE`, +

+
slice(.data, ...)
+
+slice_head(.data, n)
+
+slice_tail(.data, n)
+
+slice_max(.data, order_by, n, with_ties = TRUE)
+
+slice_min(.data, order_by, n, with_ties = TRUE)
+
+slice_sample(.data, n, replace = FALSE)
+
+ +
+

Arguments

+ + +
.data
+

A data.table

+ + +
...
+

Provide either positive values to keep, or negative values to drop. +The values provided must be either all positive or all negative.

+ + +
n
+

When larger than or equal to 1, the number of rows. +When between 0 and 1, the proportion of rows to select.

+ + +
order_by
+

Variable or function of variables to order by.

+ + +
with_ties
+

Should ties be kept together? The default, `TRUE`, may return more rows than you request. Use `FALSE` to ignore ties, -and return the first `n` rows.

replace

Should sampling be performed with (`TRUE`) or without -(`FALSE`, the default) replacement.

+and return the first `n` rows.

+ -

Value

+
replace
+

Should sampling be performed with (`TRUE`) or without +(`FALSE`, the default) replacement.

+
+
+

Value

A data.table

-

See also

- - - -

Examples

-
-a = as.data.table(iris) -slice(a,1,2)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 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 -#> <num> <num> <num> <num> <fctr> -#> 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 -#> <num> <num> <num> <num> <fctr> -#> 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 -#> <num> <num> <num> <num> <fctr> -#> 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 -#> <num> <num> <num> <num> <fctr> -#> 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 -#> <num> <num> <num> <num> <fctr> -#> 1: 7.7 3.0 6.1 2.3 virginica -#> 2: 6.3 3.4 5.6 2.4 virginica -#> 3: 6.4 3.1 5.5 1.8 virginica -#> 4: 6.0 3.0 4.8 1.8 virginica -#> 5: 6.9 3.1 5.4 2.1 virginica -#> 6: 6.7 3.1 5.6 2.4 virginica -#> 7: 6.9 3.1 5.1 2.3 virginica -#> 8: 5.8 2.7 5.1 1.9 virginica -#> 9: 6.8 3.2 5.9 2.3 virginica -#> 10: 6.7 3.3 5.7 2.5 virginica -#> 11: 6.7 3.0 5.2 2.3 virginica -#> 12: 6.3 2.5 5.0 1.9 virginica -#> 13: 6.5 3.0 5.2 2.0 virginica -#> 14: 6.2 3.4 5.4 2.3 virginica -#> 15: 5.9 3.0 5.1 1.8 virginica
slice_max(a,Sepal.Length,10)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 7.6 3.0 6.6 2.1 virginica -#> 2: 7.3 2.9 6.3 1.8 virginica -#> 3: 7.2 3.6 6.1 2.5 virginica -#> 4: 7.7 3.8 6.7 2.2 virginica -#> 5: 7.7 2.6 6.9 2.3 virginica -#> 6: 7.7 2.8 6.7 2.0 virginica -#> 7: 7.2 3.2 6.0 1.8 virginica -#> 8: 7.2 3.0 5.8 1.6 virginica -#> 9: 7.4 2.8 6.1 1.9 virginica -#> 10: 7.9 3.8 6.4 2.0 virginica -#> 11: 7.7 3.0 6.1 2.3 virginica
slice_max(a,Sepal.Length,10,with_ties = FALSE)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 7.6 3.0 6.6 2.1 virginica -#> 2: 7.3 2.9 6.3 1.8 virginica -#> 3: 7.2 3.6 6.1 2.5 virginica -#> 4: 7.7 3.8 6.7 2.2 virginica -#> 5: 7.7 2.6 6.9 2.3 virginica -#> 6: 7.7 2.8 6.7 2.0 virginica -#> 7: 7.2 3.2 6.0 1.8 virginica -#> 8: 7.4 2.8 6.1 1.9 virginica -#> 9: 7.9 3.8 6.4 2.0 virginica -#> 10: 7.7 3.0 6.1 2.3 virginica
slice_min(a,Sepal.Length,10)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 4.7 3.2 1.3 0.2 setosa -#> 2: 4.6 3.1 1.5 0.2 setosa -#> 3: 4.6 3.4 1.4 0.3 setosa -#> 4: 4.4 2.9 1.4 0.2 setosa -#> 5: 4.3 3.0 1.1 0.1 setosa -#> 6: 4.6 3.6 1.0 0.2 setosa -#> 7: 4.7 3.2 1.6 0.2 setosa -#> 8: 4.4 3.0 1.3 0.2 setosa -#> 9: 4.5 2.3 1.3 0.3 setosa -#> 10: 4.4 3.2 1.3 0.2 setosa -#> 11: 4.6 3.2 1.4 0.2 setosa
slice_min(a,Sepal.Length,10,with_ties = FALSE)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 4.7 3.2 1.3 0.2 setosa -#> 2: 4.6 3.1 1.5 0.2 setosa -#> 3: 4.6 3.4 1.4 0.3 setosa -#> 4: 4.4 2.9 1.4 0.2 setosa -#> 5: 4.3 3.0 1.1 0.1 setosa -#> 6: 4.6 3.6 1.0 0.2 setosa -#> 7: 4.4 3.0 1.3 0.2 setosa -#> 8: 4.5 2.3 1.3 0.3 setosa -#> 9: 4.4 3.2 1.3 0.2 setosa -#> 10: 4.6 3.2 1.4 0.2 setosa
slice_sample(a,10)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 4.6 3.2 1.4 0.2 setosa -#> 2: 6.4 2.9 4.3 1.3 versicolor -#> 3: 4.9 3.1 1.5 0.2 setosa -#> 4: 7.2 3.0 5.8 1.6 virginica -#> 5: 6.7 3.1 4.7 1.5 versicolor -#> 6: 7.6 3.0 6.6 2.1 virginica -#> 7: 7.4 2.8 6.1 1.9 virginica -#> 8: 4.9 3.6 1.4 0.1 setosa -#> 9: 5.6 2.9 3.6 1.3 versicolor -#> 10: 6.3 2.8 5.1 1.5 virginica
slice_sample(a,0.1)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 1: 5.9 3.0 5.1 1.8 virginica -#> 2: 6.3 2.5 5.0 1.9 virginica -#> 3: 4.9 3.0 1.4 0.2 setosa -#> 4: 6.1 2.9 4.7 1.4 versicolor -#> 5: 5.1 3.4 1.5 0.2 setosa -#> 6: 4.8 3.4 1.6 0.2 setosa -#> 7: 5.0 3.5 1.3 0.3 setosa -#> 8: 4.5 2.3 1.3 0.3 setosa -#> 9: 6.7 3.1 4.4 1.4 versicolor -#> 10: 5.8 2.7 5.1 1.9 virginica -#> 11: 7.1 3.0 5.9 2.1 virginica -#> 12: 5.0 3.0 1.6 0.2 setosa -#> 13: 5.2 2.7 3.9 1.4 versicolor -#> 14: 4.3 3.0 1.1 0.1 setosa -#> 15: 5.0 3.6 1.4 0.2 setosa
-
-
- +
+

See also

+ +
+
+

Examples

+

+a = as.data.table(iris)
+slice(a,1,2)
+#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
+#>           <num>       <num>        <num>       <num>  <fctr>
+#> 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
+#>           <num>       <num>        <num>       <num>  <fctr>
+#> 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
+#>           <num>       <num>        <num>       <num>  <fctr>
+#> 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
+#>            <num>       <num>        <num>       <num>  <fctr>
+#>  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
+#>           <num>       <num>        <num>       <num>    <fctr>
+#> 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
+#>            <num>       <num>        <num>       <num>    <fctr>
+#>  1:          7.7         3.0          6.1         2.3 virginica
+#>  2:          6.3         3.4          5.6         2.4 virginica
+#>  3:          6.4         3.1          5.5         1.8 virginica
+#>  4:          6.0         3.0          4.8         1.8 virginica
+#>  5:          6.9         3.1          5.4         2.1 virginica
+#>  6:          6.7         3.1          5.6         2.4 virginica
+#>  7:          6.9         3.1          5.1         2.3 virginica
+#>  8:          5.8         2.7          5.1         1.9 virginica
+#>  9:          6.8         3.2          5.9         2.3 virginica
+#> 10:          6.7         3.3          5.7         2.5 virginica
+#> 11:          6.7         3.0          5.2         2.3 virginica
+#> 12:          6.3         2.5          5.0         1.9 virginica
+#> 13:          6.5         3.0          5.2         2.0 virginica
+#> 14:          6.2         3.4          5.4         2.3 virginica
+#> 15:          5.9         3.0          5.1         1.8 virginica
+slice_max(a,Sepal.Length,10)
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>            <num>       <num>        <num>       <num>    <fctr>
+#>  1:          7.6         3.0          6.6         2.1 virginica
+#>  2:          7.3         2.9          6.3         1.8 virginica
+#>  3:          7.2         3.6          6.1         2.5 virginica
+#>  4:          7.7         3.8          6.7         2.2 virginica
+#>  5:          7.7         2.6          6.9         2.3 virginica
+#>  6:          7.7         2.8          6.7         2.0 virginica
+#>  7:          7.2         3.2          6.0         1.8 virginica
+#>  8:          7.2         3.0          5.8         1.6 virginica
+#>  9:          7.4         2.8          6.1         1.9 virginica
+#> 10:          7.9         3.8          6.4         2.0 virginica
+#> 11:          7.7         3.0          6.1         2.3 virginica
+slice_max(a,Sepal.Length,10,with_ties = FALSE)
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>            <num>       <num>        <num>       <num>    <fctr>
+#>  1:          7.6         3.0          6.6         2.1 virginica
+#>  2:          7.3         2.9          6.3         1.8 virginica
+#>  3:          7.2         3.6          6.1         2.5 virginica
+#>  4:          7.7         3.8          6.7         2.2 virginica
+#>  5:          7.7         2.6          6.9         2.3 virginica
+#>  6:          7.7         2.8          6.7         2.0 virginica
+#>  7:          7.2         3.2          6.0         1.8 virginica
+#>  8:          7.4         2.8          6.1         1.9 virginica
+#>  9:          7.9         3.8          6.4         2.0 virginica
+#> 10:          7.7         3.0          6.1         2.3 virginica
+slice_min(a,Sepal.Length,10)
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width Species
+#>            <num>       <num>        <num>       <num>  <fctr>
+#>  1:          4.7         3.2          1.3         0.2  setosa
+#>  2:          4.6         3.1          1.5         0.2  setosa
+#>  3:          4.6         3.4          1.4         0.3  setosa
+#>  4:          4.4         2.9          1.4         0.2  setosa
+#>  5:          4.3         3.0          1.1         0.1  setosa
+#>  6:          4.6         3.6          1.0         0.2  setosa
+#>  7:          4.7         3.2          1.6         0.2  setosa
+#>  8:          4.4         3.0          1.3         0.2  setosa
+#>  9:          4.5         2.3          1.3         0.3  setosa
+#> 10:          4.4         3.2          1.3         0.2  setosa
+#> 11:          4.6         3.2          1.4         0.2  setosa
+slice_min(a,Sepal.Length,10,with_ties = FALSE)
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width Species
+#>            <num>       <num>        <num>       <num>  <fctr>
+#>  1:          4.7         3.2          1.3         0.2  setosa
+#>  2:          4.6         3.1          1.5         0.2  setosa
+#>  3:          4.6         3.4          1.4         0.3  setosa
+#>  4:          4.4         2.9          1.4         0.2  setosa
+#>  5:          4.3         3.0          1.1         0.1  setosa
+#>  6:          4.6         3.6          1.0         0.2  setosa
+#>  7:          4.4         3.0          1.3         0.2  setosa
+#>  8:          4.5         2.3          1.3         0.3  setosa
+#>  9:          4.4         3.2          1.3         0.2  setosa
+#> 10:          4.6         3.2          1.4         0.2  setosa
+slice_sample(a,10)
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
+#>            <num>       <num>        <num>       <num>     <fctr>
+#>  1:          4.6         3.2          1.4         0.2     setosa
+#>  2:          6.4         2.9          4.3         1.3 versicolor
+#>  3:          4.9         3.1          1.5         0.2     setosa
+#>  4:          7.2         3.0          5.8         1.6  virginica
+#>  5:          6.7         3.1          4.7         1.5 versicolor
+#>  6:          7.6         3.0          6.6         2.1  virginica
+#>  7:          7.4         2.8          6.1         1.9  virginica
+#>  8:          4.9         3.6          1.4         0.1     setosa
+#>  9:          5.6         2.9          3.6         1.3 versicolor
+#> 10:          6.3         2.8          5.1         1.5  virginica
+slice_sample(a,0.1)
+#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
+#>            <num>       <num>        <num>       <num>     <fctr>
+#>  1:          5.9         3.0          5.1         1.8  virginica
+#>  2:          6.3         2.5          5.0         1.9  virginica
+#>  3:          4.9         3.0          1.4         0.2     setosa
+#>  4:          6.1         2.9          4.7         1.4 versicolor
+#>  5:          5.1         3.4          1.5         0.2     setosa
+#>  6:          4.8         3.4          1.6         0.2     setosa
+#>  7:          5.0         3.5          1.3         0.3     setosa
+#>  8:          4.5         2.3          1.3         0.3     setosa
+#>  9:          6.7         3.1          4.4         1.4 versicolor
+#> 10:          5.8         2.7          5.1         1.9  virginica
+#> 11:          7.1         3.0          5.9         2.1  virginica
+#> 12:          5.0         3.0          1.6         0.2     setosa
+#> 13:          5.2         2.7          3.9         1.4 versicolor
+#> 14:          4.3         3.0          1.1         0.1     setosa
+#> 15:          5.0         3.6          1.4         0.2     setosa
+
+
+
+
-
+ + - - - + diff --git a/docs/reference/summarise.html b/docs/reference/summarise.html index 95c896e..d7fe30a 100644 --- a/docs/reference/summarise.html +++ b/docs/reference/summarise.html @@ -1,73 +1,13 @@ - - - - - - - -Summarise columns to single values — summarise • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Summarise columns to single values — summarise • tidyft - + - - -
-
- - -
-
+
@@ -136,98 +61,100 @@

Summarise columns to single values

the variables of an existing data.table.

-
summarise(.data, ..., by = NULL)
+    
+
summarise(.data, ..., by = NULL)
+
+summarise_when(.data, when, ..., by = NULL)
+
+summarise_vars(.data, .cols = NULL, .func, ..., by)
+
+ +
+

Arguments

-summarise_when(.data, when, ..., by = NULL) -summarise_vars(.data, .cols = NULL, .func, ..., by)
+
.data
+

A data.table

-

Arguments

- - - - - - - - - - - - - - - - - - - - - - - - - - -
.data

A data.table

...

List of variables or name-value pairs of summary/modifications + +

...
+

List of variables or name-value pairs of summary/modifications functions for summarise_dt.Additional parameters to be passed to - parameter '.func' in summarise_vars.

by

Unquoted name of grouping variable of list of unquoted names of -grouping variables. For details see data.table

when

An object which can be coerced to logical mode

.cols

Columns to be summarised.

.func

Function to be run within each column, should return a value or vectors with same length.

- -

Value

+ parameter '.func' in summarise_vars.

-

A data.table

-

Examples

-
-a = as.data.table(iris) -a %>% summarise(sum = sum(Sepal.Length),avg = mean(Sepal.Length))
#> sum avg -#> <num> <num> -#> 1: 876.5 5.843333
- -a %>% - summarise_when(Sepal.Length > 5, avg = mean(Sepal.Length), by = Species)
#> Species avg -#> <fctr> <num> -#> 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 -#> <fctr> <num> <num> <num> <num> -#> 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
- -
-
- +
+

Value

+

A data.table

+
+ +
+

Examples

+

+a = as.data.table(iris)
+a %>% summarise(sum = sum(Sepal.Length),avg = mean(Sepal.Length))
+#>      sum      avg
+#>    <num>    <num>
+#> 1: 876.5 5.843333
+
+
+a %>%
+  summarise_when(Sepal.Length > 5, avg = mean(Sepal.Length), by = Species)
+#>       Species      avg
+#>        <fctr>    <num>
+#> 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
+#>        <fctr>        <num>       <num>        <num>       <num>
+#> 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
+
+
+
+
+
- + + - - - + diff --git a/docs/reference/sys_time_print.html b/docs/reference/sys_time_print.html index 1e75a55..76192f5 100644 --- a/docs/reference/sys_time_print.html +++ b/docs/reference/sys_time_print.html @@ -1,73 +1,13 @@ - - - - - - - -Convenient print of time taken — sys_time_print • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Convenient print of time taken — sys_time_print • tidyft - + - - -
-
- - -
-
+

Convenient printing of time elapsed. A wrapper of -data.table::timetaken, but showing the results more directly.

+data.table::timetaken, but showing the results more directly.

-
sys_time_print(expr)
+
+
sys_time_print(expr)
+
+ +
+

Arguments

-

Arguments

- - - - - - -
expr

Valid R expression to be timed.

-

Value

+
expr
+

Valid R expression to be timed.

+
+
+

Value

A character vector of the form HH:MM:SS, or SS.MMMsec if under 60 seconds. See examples.

-

See also

- - - -

Examples

-
-sys_time_print(Sys.sleep(1))
#> [1] "Finished in 1.010s elapsed (0.000s cpu)"
-a = as.data.table(iris) -sys_time_print({ - res = a %>% - mutate(one = 1) -})
#> [1] "Finished in 0.000s elapsed (0.000s cpu)"
res
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species one -#> <num> <num> <num> <num> <fctr> <num> -#> 1: 5.1 3.5 1.4 0.2 setosa 1 -#> 2: 4.9 3.0 1.4 0.2 setosa 1 -#> 3: 4.7 3.2 1.3 0.2 setosa 1 -#> 4: 4.6 3.1 1.5 0.2 setosa 1 -#> 5: 5.0 3.6 1.4 0.2 setosa 1 -#> --- -#> 146: 6.7 3.0 5.2 2.3 virginica 1 -#> 147: 6.3 2.5 5.0 1.9 virginica 1 -#> 148: 6.5 3.0 5.2 2.0 virginica 1 -#> 149: 6.2 3.4 5.4 2.3 virginica 1 -#> 150: 5.9 3.0 5.1 1.8 virginica 1
-
- +
+

See also

+ +
+
+

Examples

+

+sys_time_print(Sys.sleep(1))
+#> [1] "Finished in 1.030s elapsed (0.000s cpu)"
+
+a = as.data.table(iris)
+sys_time_print({
+  res = a %>%
+    mutate(one = 1)
+})
+#> [1] "Finished in 0.000s elapsed (0.000s cpu)"
+res
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species   one
+#>             <num>       <num>        <num>       <num>    <fctr> <num>
+#>   1:          5.1         3.5          1.4         0.2    setosa     1
+#>   2:          4.9         3.0          1.4         0.2    setosa     1
+#>   3:          4.7         3.2          1.3         0.2    setosa     1
+#>   4:          4.6         3.1          1.5         0.2    setosa     1
+#>   5:          5.0         3.6          1.4         0.2    setosa     1
+#>  ---                                                                  
+#> 146:          6.7         3.0          5.2         2.3 virginica     1
+#> 147:          6.3         2.5          5.0         1.9 virginica     1
+#> 148:          6.5         3.0          5.2         2.0 virginica     1
+#> 149:          6.2         3.4          5.4         2.3 virginica     1
+#> 150:          5.9         3.0          5.1         1.8 virginica     1
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/tidymat.html b/docs/reference/tidymat.html index bcd8f05..74fd2ad 100644 --- a/docs/reference/tidymat.html +++ b/docs/reference/tidymat.html @@ -1,73 +1,13 @@ - - - - - - - -Conversion between tidy table and named matrix — mat_df • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Conversion between tidy table and named matrix — mat_df • tidyft - + - - -
-
- - -
-
+
@@ -136,96 +61,100 @@

Conversion between tidy table and named matrix

tidy table and named matrix.

-
mat_df(m)
+    
+
mat_df(m)
+
+df_mat(df, row, col, value)
+
+ +
+

Arguments

+ + +
m
+

A matrix

-df_mat(df, row, col, value)
-

Arguments

- - - - - - - - - - - - - - - - - - - - - - -
m

A matrix

df

A data.frame with at least 3 columns, one for row name, +

df
+

A data.frame with at least 3 columns, one for row name, one for column name, and one for values. The names for column and -row should be unique.

row

Unquoted expression of column name for row

col

Unquoted expression of column name for column

value

Unquoted expression of column name for values

- -

Value

+row should be unique.

+ + +
row
+

Unquoted expression of column name for row

+ +
col
+

Unquoted expression of column name for column

+ + +
value
+

Unquoted expression of column name for values

+ +
+
+

Value

For mat_df, a data.frame. For df_mat, a named matrix.

+
-

Examples

-
-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
-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
-
-
- +
-
+ + - - - + diff --git a/docs/reference/uncount.html b/docs/reference/uncount.html index 76ba0f3..1f081f5 100644 --- a/docs/reference/uncount.html +++ b/docs/reference/uncount.html @@ -1,74 +1,14 @@ - - - - - - - -"Uncount" a data frame — uncount • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"Uncount" a data frame — uncount • tidyft - - - - - - - - - - - - - + - -
-
- - -
-
+
@@ -138,75 +63,75 @@

"Uncount" a data frame

Analogous to `tidyr::uncount`.

-
uncount(.data, wt, .remove = TRUE)
- -

Arguments

- - - - - - - - - - - - - - -
.data

A data.frame

wt

A vector of weights.

.remove

Should the column for weights be removed? -Default uses TRUE.

- -

Value

+
+
uncount(.data, wt, .remove = TRUE)
+
+ +
+

Arguments

+ + +
.data
+

A data.frame

+ +
wt
+

A vector of weights.

+ + +
.remove
+

Should the column for weights be removed? +Default uses TRUE.

+ +
+
+

Value

A data.table

-

See also

- - - -

Examples

-
-df <- data.table(x = c("a", "b"), n = c(1, 2)) -uncount(df, n)
#> x -#> <char> -#> 1: a -#> 2: b -#> 3: b
uncount(df,n,FALSE)
#> x n -#> <char> <num> -#> 1: a 1 -#> 2: b 2 -#> 3: b 2
-
- +
+

See also

+ +
+
+

Examples

+

+df <- data.table(x = c("a", "b"), n = c(1, 2))
+uncount(df, n)
+#>         x
+#>    <char>
+#> 1:      a
+#> 2:      b
+#> 3:      b
+uncount(df,n,FALSE)
+#>         x     n
+#>    <char> <num>
+#> 1:      a     1
+#> 2:      b     2
+#> 3:      b     2
+
+
+
-
+ + - - - + diff --git a/docs/reference/unite.html b/docs/reference/unite.html index 9931aa1..28ae642 100644 --- a/docs/reference/unite.html +++ b/docs/reference/unite.html @@ -1,73 +1,13 @@ - - - - - - - -Unite multiple columns into one by pasting strings together — unite • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Unite multiple columns into one by pasting strings together — unite • tidyft + - - - -
-
- - -
-
+

Convenience function to paste together multiple columns into one. -Analogous to tidyr::unite.

+Analogous to tidyr::unite.

-
unite(.data, united_colname, ..., sep = "_", remove = FALSE, na2char = FALSE)
- -

Arguments

- - - - - - - - - - - - - - - - - - - - - - - - - - -
.data

A data frame.

united_colname

The name of the new column, string only.

...

A selection of columns. If want to select all columns, -pass "" to the parameter. See example.

sep

Separator to use between values.

remove

If TRUE, remove input columns from output data frame.

na2char

If FALSE, missing values would be merged into NA, -otherwise NA is treated as character "NA". This is different from -tidyr.

+
+
unite(.data, united_colname, ..., sep = "_", remove = FALSE, na2char = FALSE)
+
+ +
+

Arguments

+ + +
.data
+

A data frame.

+ + +
united_colname
+

The name of the new column, string only.

+ + +
...
+

A selection of columns. If want to select all columns, +pass "" to the parameter. See example.

+ -

Value

+
sep
+

Separator to use between values.

+ +
remove
+

If TRUE, remove input columns from output data frame.

+ + +
na2char
+

If FALSE, missing values would be merged into NA, +otherwise NA is treated as character "NA". This is different from +tidyr.

+ +
+
+

Value

A data.table

-

See also

- - - -

Examples

-
df <- CJ(x = c("a", NA), y = c("b", NA)) -df
#> Key: <x, y> -#> x y -#> <char> <char> -#> 1: <NA> <NA> -#> 2: <NA> b -#> 3: a <NA> -#> 4: a b
-# Treat missing value as NA, default -df %>% unite("z", x:y, remove = FALSE)
#> Key: <x, y> -#> x y z -#> <char> <char> <char> -#> 1: <NA> <NA> <NA> -#> 2: <NA> b <NA> -#> 3: a <NA> <NA> -#> 4: a b a_b
# Treat missing value as character "NA" -df %>% unite("z", x:y, na2char = TRUE, remove = FALSE)
#> Key: <x, y> -#> x y z -#> <char> <char> <char> -#> 1: <NA> <NA> NA_NA -#> 2: <NA> b NA_b -#> 3: a <NA> a_NA -#> 4: a b a_b
# the unite has memory, "z" would not be removed in new operations -# here we remove the original columns ("x" and "y") -df %>% unite("xy", x:y,remove = TRUE)
#> z xy -#> <char> <char> -#> 1: NA_NA <NA> -#> 2: NA_b <NA> -#> 3: a_NA <NA> -#> 4: a_b a_b
-# Select all columns -iris %>% as.data.table %>% unite("merged_name","")
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <fctr> -#> 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 -#> merged_name -#> <char> -#> 1: 5.1_3.5_1.4_0.2_setosa -#> 2: 4.9_3_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_3.6_1.4_0.2_setosa -#> --- -#> 146: 6.7_3_5.2_2.3_virginica -#> 147: 6.3_2.5_5_1.9_virginica -#> 148: 6.5_3_5.2_2_virginica -#> 149: 6.2_3.4_5.4_2.3_virginica -#> 150: 5.9_3_5.1_1.8_virginica
-
- +
+

See also

+ +
+
+

Examples

+
df <- CJ(x = c("a", NA), y = c("b", NA))
+df
+#> Key: <x, y>
+#>         x      y
+#>    <char> <char>
+#> 1:   <NA>   <NA>
+#> 2:   <NA>      b
+#> 3:      a   <NA>
+#> 4:      a      b
+
+# Treat missing value as NA, default
+df %>% unite("z", x:y, remove = FALSE)
+#> Key: <x, y>
+#>         x      y      z
+#>    <char> <char> <char>
+#> 1:   <NA>   <NA>   <NA>
+#> 2:   <NA>      b   <NA>
+#> 3:      a   <NA>   <NA>
+#> 4:      a      b    a_b
+# Treat missing value as character "NA"
+df %>% unite("z", x:y, na2char = TRUE, remove = FALSE)
+#> Key: <x, y>
+#>         x      y      z
+#>    <char> <char> <char>
+#> 1:   <NA>   <NA>  NA_NA
+#> 2:   <NA>      b   NA_b
+#> 3:      a   <NA>   a_NA
+#> 4:      a      b    a_b
+# the unite has memory, "z" would not be removed in new operations
+# here we remove the original columns ("x" and "y")
+df %>% unite("xy", x:y,remove = TRUE)
+#>         z     xy
+#>    <char> <char>
+#> 1:  NA_NA   <NA>
+#> 2:   NA_b   <NA>
+#> 3:   a_NA   <NA>
+#> 4:    a_b    a_b
+
+# Select all columns
+iris %>% as.data.table %>% unite("merged_name",".")
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>             <num>       <num>        <num>       <num>    <fctr>
+#>   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
+#>                    merged_name
+#>                         <char>
+#>   1:    5.1_3.5_1.4_0.2_setosa
+#>   2:      4.9_3_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_3.6_1.4_0.2_setosa
+#>  ---                          
+#> 146:   6.7_3_5.2_2.3_virginica
+#> 147:   6.3_2.5_5_1.9_virginica
+#> 148:     6.5_3_5.2_2_virginica
+#> 149: 6.2_3.4_5.4_2.3_virginica
+#> 150:   5.9_3_5.1_1.8_virginica
+
+
+
-
- +
+ + - - - + diff --git a/docs/reference/utf8_encoding.html b/docs/reference/utf8_encoding.html index 1f70ca9..5ceab1e 100644 --- a/docs/reference/utf8_encoding.html +++ b/docs/reference/utf8_encoding.html @@ -1,75 +1,15 @@ - - - - - - - -Use UTF-8 for character encoding in a data frame — utf8_encoding • tidyft - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Use UTF-8 for character encoding in a data frame — utf8_encoding • tidyft + - - - -
-
- - -
-
+
@@ -140,70 +65,68 @@

Use UTF-8 for character encoding in a data frame

encoding of characters in a data frame.

-
utf8_encoding(.data, .cols)
+
+
utf8_encoding(.data, .cols)
+
-

Arguments

- - - - - - - - - - -
.data

A data.frame.

.cols

The columns you want to convert, usually a character column.

+
+

Arguments

-

Value

-

A data.table with characters in UTF-8 encoding

+
.data
+

A data.frame.

-

Examples

-
iris %>% - as.data.table() %>% - utf8_encoding(Species) # could also use `is.factor`
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species -#> <num> <num> <num> <num> <char> -#> 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
-
- +
+

Value

+

A data.table with characters in UTF-8 encoding

+
+ +
+

Examples

+
iris %>%
+  as.data.table() %>%
+  utf8_encoding(Species)  # could also use `is.factor`
+#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
+#>             <num>       <num>        <num>       <num>    <char>
+#>   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
+
+
+
-
- +
+ + - - - + diff --git a/docs/sitemap.xml b/docs/sitemap.xml new file mode 100644 index 0000000..b60f398 --- /dev/null +++ b/docs/sitemap.xml @@ -0,0 +1,46 @@ + +/404.html +/articles/index.html +/articles/Introduction.html +/authors.html +/index.html +/LICENSE-text.html +/LICENSE.html +/reference/arrange.html +/reference/as_fst.html +/reference/complete.html +/reference/count.html +/reference/cummean.html +/reference/distinct.html +/reference/drop_delete_na.html +/reference/dummy.html +/reference/fill.html +/reference/filter.html +/reference/fst.html +/reference/fst_io.html +/reference/group.html +/reference/index.html +/reference/join.html +/reference/lag_lead.html +/reference/long_wide.html +/reference/mutate.html +/reference/nest.html +/reference/nth.html +/reference/object_size.html +/reference/pull.html +/reference/read_csv.html +/reference/reexports.html +/reference/relocate.html +/reference/replace_vars.html +/reference/rowwise.html +/reference/select.html +/reference/separate.html +/reference/slice.html +/reference/summarise.html +/reference/sys_time_print.html +/reference/tidymat.html +/reference/uncount.html +/reference/unite.html +/reference/utf8_encoding.html + +