Skip to content

Commit

Permalink
fix vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Jun 23, 2024
1 parent b9f0972 commit 0771056
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 76 deletions.
6 changes: 3 additions & 3 deletions vignettes/polars.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ To go from long to wide, we use the `$pivot()` method.
Here we pivot the data so that every subject takes its own column.

```{r}
indo_wide = indo$pivot(values = "conc", index = "time", columns = "Subject")
indo_wide = indo$pivot(values = "conc", index = "time", on = "Subject")
indo_wide
```

To go from wide to long, we use the `$melt()` method.

```{r}
# indo_wide$melt(id_vars = "time") # default column names are "variable" and "value"
indo_wide$melt(id_vars = "time", variable_name = "subject", value_name = "conc")
indo_wide$unpivot(index = "time", variable_name = "subject", value_name = "conc")
```

Basic functionality aside, it should be noted that `$pivot()` can perform
Expand All @@ -356,7 +356,7 @@ different combinations of transmission type (`am`) and engine shape (`vs`)?
dat$pivot(
values = "mpg",
index = c("am", "vs"),
columns = "cyl",
on = "cyl",
aggregate_function = "median" # aggregating function
)
```
Expand Down
143 changes: 70 additions & 73 deletions vignettes/userguide.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -271,77 +271,76 @@ dataset$
collect()
```

````{comment}
```{r}
compute_age = function() 2021 - pl$col("birthday")$dt$year()
<!-- ```{r} -->
<!-- compute_age = function() 2021 - pl$col("birthday")$dt$year() -->

<!-- avg_birthday = function(gender) { -->
<!-- compute_age()$filter(pl$col("gender") == gender)$mean()$alias(sprintf("avg %s birthday", gender)) -->
<!-- } -->

<!-- q = ( -->
<!-- dataset$lazy()$ -->
<!-- group_by("state")$ -->
<!-- agg( -->
<!-- avg_birthday("M"), -->
<!-- avg_birthday("F"), -->
<!-- (pl$col("gender") == "M")$sum()$alias("# male"), -->
<!-- (pl$col("gender") == "F")$sum()$alias("# female") -->
<!-- )$ -->
<!-- limit(5) -->
<!-- ) -->
<!-- q$collect() -->

<!-- # -->
<!-- # get_person <- function() pl$col("first_name") + pl$lit(" ") + pl$col("last_name") -->
<!-- # q = ( -->
<!-- # dataset$lazy() -->
<!-- # $sort("birthday", descending=True) -->
<!-- # $group_by(["state"]) -->
<!-- # $agg( -->
<!-- # [ -->
<!-- # get_person()$first()$alias("youngest"), -->
<!-- # get_person()$last()$alias("oldest"), -->
<!-- # ] -->
<!-- # ) -->
<!-- # $limit(5) -->
<!-- # ) -->
<!-- # q$collect() -->
<!-- # -->
<!-- # get_person <- function() pl$col("first_name") + pl$lit(" ") + pl$col("last_name") -->
<!-- # q = ( -->
<!-- # dataset$lazy() -->
<!-- # $sort("birthday", descending=True) -->
<!-- # $group_by(["state"]) -->
<!-- # $agg( -->
<!-- # [ -->
<!-- # get_person()$first()$alias("youngest"), -->
<!-- # get_person()$last()$alias("oldest"), -->
<!-- # get_person()$sort()$first()$alias("alphabetical_first"), -->
<!-- # ] -->
<!-- # ) -->
<!-- # $limit(5) -->
<!-- # ) -->
<!-- # q$collect() -->
<!-- # -->
<!-- # q = ( -->
<!-- # dataset$lazy() -->
<!-- # $sort("birthday", descending=True) -->
<!-- # $group_by(["state"]) -->
<!-- # $agg( -->
<!-- # [ -->
<!-- # get_person()$first()$alias("youngest"), -->
<!-- # get_person()$last()$alias("oldest"), -->
<!-- # get_person()$sort()$first()$alias("alphabetical_first"), -->
<!-- # pl$col("gender")$sort_by("first_name")$first()$alias("gender"), -->
<!-- # ] -->
<!-- # ) -->
<!-- # $sort("state") -->
<!-- # $limit(5) -->
<!-- # ) -->
<!-- # q$collect() -->
<!-- ``` -->

avg_birthday = function(gender) {
compute_age()$filter(pl$col("gender") == gender)$mean()$alias(sprintf("avg %s birthday", gender))
}
q = (
dataset$lazy()$
group_by("state")$
agg(
avg_birthday("M"),
avg_birthday("F"),
(pl$col("gender") == "M")$sum()$alias("# male"),
(pl$col("gender") == "F")$sum()$alias("# female")
)$
limit(5)
)
q$collect()
#
# get_person <- function() pl$col("first_name") + pl$lit(" ") + pl$col("last_name")
# q = (
# dataset$lazy()
# $sort("birthday", descending=True)
# $group_by(["state"])
# $agg(
# [
# get_person()$first()$alias("youngest"),
# get_person()$last()$alias("oldest"),
# ]
# )
# $limit(5)
# )
# q$collect()
#
# get_person <- function() pl$col("first_name") + pl$lit(" ") + pl$col("last_name")
# q = (
# dataset$lazy()
# $sort("birthday", descending=True)
# $group_by(["state"])
# $agg(
# [
# get_person()$first()$alias("youngest"),
# get_person()$last()$alias("oldest"),
# get_person()$sort()$first()$alias("alphabetical_first"),
# ]
# )
# $limit(5)
# )
# q$collect()
#
# q = (
# dataset$lazy()
# $sort("birthday", descending=True)
# $group_by(["state"])
# $agg(
# [
# get_person()$first()$alias("youngest"),
# get_person()$last()$alias("oldest"),
# get_person()$sort()$first()$alias("alphabetical_first"),
# pl$col("gender")$sort_by("first_name")$first()$alias("gender"),
# ]
# )
# $sort("state")
# $limit(5)
# )
# q$collect()
```
````


## Folds
Expand Down Expand Up @@ -451,8 +450,6 @@ df$sort("Type 1")$select(
# List context and row wise computations

````{comment}
```
grades = pl$DataFrame(
"student" = c("bas", "laura", "tim", "jenny"),
"arithmetic" = c(10, 5, 6, 8),
Expand Down Expand Up @@ -520,7 +517,7 @@ out = df$select(
)
print(out)
```
````
# R examples
Expand Down

0 comments on commit 0771056

Please sign in to comment.