You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by th...@apache.org on 2021/09/09 16:38:39 UTC

[arrow-cookbook] branch main updated: ARROW-13718: [Doc][Cookbook] Creating Arrays - R (#65)

This is an automated email from the ASF dual-hosted git repository.

thisisnic pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-cookbook.git


The following commit(s) were added to refs/heads/main by this push:
     new 9c2d77f  ARROW-13718: [Doc][Cookbook] Creating Arrays - R (#65)
9c2d77f is described below

commit 9c2d77fff4ff876be7a7108dfebd9ddaad415bcd
Author: Nic <th...@gmail.com>
AuthorDate: Thu Sep 9 16:38:34 2021 +0000

    ARROW-13718: [Doc][Cookbook] Creating Arrays - R (#65)
    
    * Add recipe for creating an Array
---
 r/content/creating_arrow_objects.Rmd | 72 +++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 38 deletions(-)

diff --git a/r/content/creating_arrow_objects.Rmd b/r/content/creating_arrow_objects.Rmd
index a1e6c8c..ba7e530 100644
--- a/r/content/creating_arrow_objects.Rmd
+++ b/r/content/creating_arrow_objects.Rmd
@@ -1,10 +1,33 @@
 # Creating Arrow Objects
 
-## Build an Arrow Table from native language types
+## Create an Arrow Array from an R object
 
-### Manually create a Table from an R object
+You to convert an existing vector in R to an Arrow Array object.
 
-You may want to convert an existing data frame in R to an Arrow Table object.
+### Solution
+
+```{r, array_create}
+# Create an example vector
+score = c(99, 97, 99)
+
+# Convert to Arrow Array
+score_array <- Array$create(score)
+
+# View Array
+score_array
+```
+```{r, test_array_create, opts.label = "test"}
+test_that("array_create works as expected", {
+  expect_s3_class(score_array, "Array")
+  expect_identical(as.vector(score_array), score)
+})
+```
+
+## Create a Arrow Table from an R object
+
+You want to convert an existing data frame in R to an Arrow Table object.
+
+### Solution
 
 ```{r, table_create}
 # Create an example data frame
@@ -20,9 +43,11 @@ test_that("table_create works as expected", {
   expect_identical(dplyr::collect(my_table), my_tibble)
 })
 ```
-#### View the contents of an Arrow Table
+## View the contents of an Arrow Table or RecordBatch
+
+You want to view the contents of an Arrow Table or RecordBatch.
 
-You can view the contents of an Arrow Table using `dplyr::collect()`
+### Solution
 
 ```{r, table_collect}
 # View Table
@@ -34,9 +59,11 @@ test_that("table_collect works as expected", {
 })
 ```
 
-### Manually create a RecordBatch
+## Manually create a RecordBatch from an R object.
 
-You may want to convert an existing data frame in R to an Arrow RecordBatch object.
+You want to convert an existing data frame in R to an Arrow RecordBatch object.
+
+### Solution
 
 ```{r, record_batch_create}
 # Create an example data frame
@@ -52,34 +79,3 @@ test_that("record_batch_create works as expected", {
   expect_identical(dplyr::collect(my_record_batch), my_tibble)
 })
 ```
-#### View the contents of a RecordBatch
-
-You can view the contents of a RecordBatch using `dplyr::collect()`
-
-```{r, rb_collect}
-# View RecordBatch
-dplyr::collect(my_record_batch)
-```
-```{r, test_rb_collect, opts.label = "test"}
-test_that("rb_collect works as expected", {
-  expect_identical(dplyr::collect(my_record_batch), my_tibble)
-})
-```
-
-## Storing Categorical Data in Arrow
-
-An Arrow Dictionary object is similar to a factor in R, in that it allows for efficient storage of categorical data by allowing you to map between indices and values, reducing the amount of storage.  If you have an R data frame containing factors, converting it to an Arrow object will automatically encode that column as a dictionary.
-
-```{r}
-class(iris$Species)
-```
-
-```{r, dictionary}
-iris_rb <- record_batch(iris)
-iris_rb
-```
-```{r, test_dictionary, opts.label = "test"}
-test_that("dictionary works as expected", {
-  expect_s3_class(iris_rb$Species, "DictionaryArray")
-})
-```