Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions exercises/concept/trees-and-tibbleations/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
## 3. Orchard copy of dataset

- You need to move columns around and sort rows (see the Introduction).
- The [`all_of()`][ref-allof] function may be useful for moving columns.
- See caution in the introduction about [data-masking][ref-datamask] which mentions [`pick()`][ref-pick].

## 4. Customer copy of dataset

- You will need to take a subset of columns.
- The [`all_of()`][ref-allof] function may be useful for subsetting.
- Only the trees between the minimum and maximum height *and* below the maximum weight should be in the copy.

[ref-allof]: https://tidyselect.r-lib.org/reference/all_of.html
Expand Down
49 changes: 21 additions & 28 deletions exercises/concept/trees-and-tibbleations/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,49 +60,42 @@ girth_n_weight(tree_data, 1) |> head(3)
#> 8.8 63 10.2 27.6 357.0
```

**Note:** For testing, the input dataset for `girth_n_weight` can be assumed to have `Diameter` and `Volume` columns.

## 3. Orchard copy of dataset

For each potential customer, the orchard keeps a special copy of the dataset to help with the sale.
This dataset has important columns moved to the front and is sorted by that leading column.
This dataset has the most relevant columns, `Height` and `Weight`, moved to the front and is sorted by `Height`.

Define the function `orchard_copy(data, important_cols)` which takes a dataframe and a vector of column names.
This should return a new dataframe with the important columns moved to the front and the rows sorted by the first column of that rearrangement.
Define the function `orchard_copy(data, important_cols)` which takes a dataframe as input.
This should return a new dataframe with `Height` and `Weight` moved to the front and the rows sorted by `Height`.
Comment thread
depial marked this conversation as resolved.
Outdated

```R
orchard_copy(tree_data, c('Height', 'Volume')) |> head(3)
#> A tibble: 3 × 3
#> Height Volume Diameter
#> <dbl> <dbl> <dbl>
#> 63 10.2 8.8
#> 64 24.9 13.8
#> 65 10.3 8.6
tree_data |> girth_n_weight() |> orchard_copy() |> head(3)
#> A tibble: 3 × 5
#> Weight Height Diameter Volume Girth
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 357 63 9 10 28
#> 360 70 8 10 26
#> 360 65 9 10 27
```

## 4. Customer copy of dataset

Each potential customer will get a personal version of the dataset based on their preferences.
To this end, the customer may specify two extra things:

1. The minimum height, the maximum height and the maximum weight of any single tree.
2. A list of attributes that are of interest.
To this end, the customer may specify: the minimum height, the maximum height and the maximum weight of any single tree.

Define the `customer_copy(data, attrbutes, min_height, max_height, max_weight)` function.
Return a customer copy of the dataframe with the requested attributes and within the constraints.
Define the `customer_copy(data, min_height, max_height, max_weight)` function.
Return a customer copy of the dataframe with `Height`, `Weight`, `Diameter` and `Girth` columns with rows that are within the constraints.

```R
tree_data |>
girth_n_weight(1) |>
orchard_copy(c('Height', 'Weight', 'Volume')) |>
customer_copy(c('Height', 'Weight', 'Girth'), 65, 75, 1500) |>
orchard_copy() |>
customer_copy(65, 75, 1500) |>
head(3)
#> A tibble: 3 × 3
#> Height Weight Girth
#> <dbl> <dbl> <dbl>
#> 65 360.5 27.0
#> 66 546.0 34.6
#> 69 745.5 36.8
#> A tibble: 3 × 4
#> Height Weight Diameter Girth
#> <dbl> <dbl> <dbl> <dbl>
#> 70 360.5 8.3 26.1
#> 65 360.5 8.6 27.0
#> 66 546.0 11.0 34.6
```

**Note:** For testing, the input dataset for `customer_copy` can be assumed to have `Height` and `Weight` columns.
28 changes: 0 additions & 28 deletions exercises/concept/trees-and-tibbleations/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,31 +302,3 @@ tbl |> arrange(languages)
#> 3 Python 1991 TRUE
#> 4 R 1993 TRUE
```

~~~~exercism/caution
Many functions, such as `arrange()`, `filter()` and `mutate()` are data-masking and require data-masking variables.
For this reason, non-data-masking arguments (e.g. character vectors) need to be converted to be used in data-masking functions.

A full treatment of how data-masking works in R is beyond the scope of this concept, but it's useful to know there are ways of making this conversion which include options such as: `pick()`, `.data[[]]` and `!!sym()`.

```R
# arrange() with string input fails silently
tbl |> arrange("languages")
#> A tibble: 4 × 3
#> languages created has.syllabus
#> <chr> <dbl> <lgl>
#> 1 Fortran 1957 FALSE
#> 2 R 1993 TRUE
#> 3 Python 1991 TRUE
#> 4 Julia 2012 TRUE

tbl |> arrange(pick("languages"))
#> A tibble: 4 × 3
#> languages created has.syllabus
#> <chr> <dbl> <lgl>
#> 1 Fortran 1957 FALSE
#> 2 Julia 2012 TRUE
#> 3 Python 1991 TRUE
#> 4 R 1993 TRUE
```
~~~~
1 change: 1 addition & 0 deletions exercises/concept/trees-and-tibbleations/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The goal of this exercise is to teach the student how to use dataframes with the

## Out of scope

- Tidy evaluation
- Merging dataframes
- Dimension collapsing operations
- Data operations / data analysis
Expand Down
10 changes: 5 additions & 5 deletions exercises/concept/trees-and-tibbleations/.meta/exemplar.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ girth_n_weight <- function(data, rnd_digits) {
round(rnd_digits)
}

orchard_copy <- function(data, important_cols) {
orchard_copy <- function(data) {
data |>
relocate(all_of(important_cols)) |>
arrange(pick(important_cols[1]))
relocate(c(Weight, Height)) |>
arrange(Weight)
}

customer_copy <- function(data, attributes, min_height, max_height, max_weight) {
customer_copy <- function(data, min_height, max_height, max_weight) {
data |>
select(all_of(attributes)) |>
select(c(Height, Weight, Diameter, Girth)) |>
filter(between(Height, min_height, max_height) & Weight < max_weight)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ test_that("2. Add girth and weight columns", {
})

test_that("3. Orchard copy of dataset", {
test_data <- tree_data |> girth_n_weight(1) |> orchard_copy(c('Height', 'Weight'))
test_data <- tree_data |> girth_n_weight(1) |> orchard_copy()

expect_setequal(names(test_data), c('Height', 'Weight', 'Diameter', 'Volume', 'Girth'))
expect_equal(names(test_data)[1:2], c('Height', 'Weight'))
expect_equal(test_data$Height[15:18], c(76, 76, 77, 78))
expect_equal(test_data$Girth[21:24], c(44.6, 56.2, 56.5, 56.5))
expect_equal(test_data$Weight[5:9], c(745.5, 360.5, 899.5, 574.0, 1340.5))
expect_setequal(names(test_data), c('Weight', 'Height', 'Diameter', 'Volume', 'Girth'))
expect_equal(names(test_data)[1:2], c('Weight', 'Height'))
expect_equal(test_data$Height[15:18], c(80, 79, 64, 71))
expect_equal(test_data$Girth[21:24], c(40.5, 44.0, 45.6, 50.3))
expect_equal(test_data$Weight[5:9], c(574.0, 637.0, 658.0, 668.5, 689.5))
})

test_that("4. Customer copy of dataset", {
test_data <- tree_data |> girth_n_weight(1) |> customer_copy(c('Weight', 'Height', 'Girth'), 70, 75, 1000)
test_data <- tree_data |> girth_n_weight(1) |> customer_copy(70, 75, 1000)

expect_equal(names(test_data), c('Weight', 'Height', 'Girth'))
expect_equal(names(test_data), c('Height', 'Weight', 'Diameter', 'Girth'))
expect_equal(test_data$Weight[2:5], c(574.0, 637.0, 696.5, 668.5))
expect_equal(test_data$Height[4:7], c(75, 75, 74, 71))
expect_equal(test_data$Girth[1:4], c(26.1, 33.0, 34.6, 35.2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ girth_n_weight <- function(data, rnd_digits) {

}

orchard_copy <- function(data, rearrangement) {
orchard_copy <- function(data) {

}

customer_copy <- function(data, attributes, min_height, max_height, max_weight) {
customer_copy <- function(data, min_height, max_height, max_weight) {

}