ianmcook commented on a change in pull request #1:
URL: https://github.com/apache/arrow-cookbook/pull/1#discussion_r674975554



##########
File path: r/content/manipulating_data.Rmd
##########
@@ -0,0 +1,66 @@
+# Manipulating Data
+
+## Computing Mean/Min/Max, etc value of an array
+
+Many base R functions such as `mean()`, `min()`, and `max()` have been mapped 
to their Arrow equivalents, and so can be called on Arrow Array objects in the 
same way.  They will return Arrow object themselves.
+
+```{r, array_mean_na}
+my_values <- Array$create(c(1:5, NA))
+mean(my_values, na.rm = TRUE)
+```
+```{r, test_array_mean_na, opts.label = "test"}
+test_that("array_mean_na works as expected", {
+  expect_equal(mean(my_values, na.rm = TRUE), Scalar$create(3))
+})
+```
+If you want to use an R function which does not have an Arrow mapping, you can 
use `as.vector()` to convert Arrow objects to base R vectors.
+
+```{r, fivenum}
+fivenum(as.vector(my_values))
+```
+```{r, test_fivenum, opts.label = "test"}
+test_that("fivenum works as expected", {
+  expect_equal(fivenum(as.vector(my_values)), 1:5)
+})
+```
+
+## Counting occurrences of elements in an array
+
+You can use `value_count()` to count elements in an Array.
+
+```{r, value_counts}
+repeated_vals <- Array$create(c(1, 1, 2, 3, 3, 3, 3, 3))
+value_counts(repeated_vals)
+```
+```{r, test_value_counts, opts.label = "test"}
+test_that("value_counts works as expected", {
+  expect_equal(as.vector(value_counts(repeated_vals)), tibble::tibble(values = 
c(1, 2, 3), counts = c(2L, 1L, 5L)))

Review comment:
       Best to test this directly on the input data instead of hard-coding an 
expected result:
   ```suggestion
     expect_equal(as.vector(value_counts(repeated_vals)),
     tibble(
       values = as.numeric(names(table(as.vector(repeated_vals)))),
       counts = as.vector(table(as.vector(repeated_vals)))
     )
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to