Skip to content

Commit 2b168c6

Browse files
author
Symbolics
committed
Document how to add rows to data frame
Miscellaneous other minor updates.
1 parent f1d23e9 commit 2b168c6

File tree

4 files changed

+45
-43
lines changed

4 files changed

+45
-43
lines changed

content/en/docs/Examples/statistics.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ To run the notebooks you will have to install a third-party library,
1414

1515
After installing `cl-jupyter`, clone the IPS repository into your `~/common-lisp/` directory.
1616

17-
{{< alert title="Note" >}}Be careful when upgrading `common-lisp-jupyter`. Breaking changes are often introduced without warning. If you experience problems, use cl-jupyter revision `b1021ab` by using the [get checkout command](https://www.git-tower.com/learn/git/faq/git-checkout-commits/).
17+
{{< alert title="Note" >}}Be careful when upgrading `common-lisp-jupyter`. Breaking changes are often introduced without warning. If you experience problems, use cl-jupyter revision `b1021ab` by using the [git checkout command](https://www.git-tower.com/learn/git/faq/git-checkout-commits/).
1818
{{< /alert >}}
1919

2020

content/en/docs/Manuals/data-frame.md

+40-40
Original file line numberDiff line numberDiff line change
@@ -1363,41 +1363,6 @@ Let's change the `print-object` back to our convenience method.
13631363
(df:print-data df stream nil)))
13641364
```
13651365

1366-
### stacking
1367-
1368-
Stacking is done with the [array-operations stacking functions](/docs/manuals/array-operations/#stacking). Since these functions operate on both arrays and data frames, we can use them to stack data frames, arrays, or a mixture of both, providing they have a rank of 2. Here's an example using the `mtcars` data frame:
1369-
1370-
```
1371-
(defparameter boss-mustang
1372-
#("Boss Mustang" 12.7d0 8 302 405 4.11d0 2.77d0 12.5d0 0 1 4 4))
1373-
```
1374-
and now stack it onto the `mtcars` data set (load it with `(data :mtcars)` if you haven't already done so):
1375-
```
1376-
(matrix-df
1377-
(keys mtcars)
1378-
(stack-rows mtcars boss-mustang))
1379-
```
1380-
This is the functional equivalent of R's `rbind` function. You can also add columns with the `stack-cols` function.
1381-
1382-
An often asked question is: why don't you have a dedicated `stack-rows` function? Well, if you want one it might look like this:
1383-
```
1384-
(defun stack-rows (df &rest objects)
1385-
"Stack rows that works on matrices and/or data frames."
1386-
(matrix-df
1387-
(keys df)
1388-
(apply #'aops:stack-rows (cons df objects))))
1389-
```
1390-
But now the data frame must be the first parameter passed to the function. Or perhaps you want to rename the columns? Or you have matrices as your starting point? For all those reasons, it makes more sense to pass in the column keys than a data frame:
1391-
```
1392-
(defun stack-rows (col-names &rest objects)
1393-
"Stack rows that works on matrices and/or data frames."
1394-
(matrix-df
1395-
(keys col-names)
1396-
(stack-rows objects)))
1397-
```
1398-
However this means we have two `stack-rows` functions, and you don't really gain anything except an extra function call. So use the above definition if you like; we use the first example and call `matrix-df` and `stack-rows` to stack data frames.
1399-
1400-
14011366

14021367
## Column operations
14031368

@@ -1438,7 +1403,7 @@ You can also return a subset of the columns by passing in a selection:
14381403
;; #(#(21 21 22.8d0 21.4d0 18.7d0) #(2.62d0 2.875d0 2.32d0 3.215d0 3.44d0))
14391404
```
14401405

1441-
### Add columns
1406+
### add columns
14421407

14431408
There are two 'flavors' of add functions, destructive and
14441409
non-destructive. The latter return a **new** data frame as the
@@ -1523,7 +1488,7 @@ Now let's add multiple columns destructively using `add-columns!`
15231488
```
15241489

15251490

1526-
### Remove columns
1491+
### remove columns
15271492

15281493
Let's remove the columns `a` and `b` that we just added above with
15291494
the `remove-columns` function. Since it returns a new data frame,
@@ -1544,7 +1509,7 @@ To remove columns destructively, meaning modifying the original data,
15441509
use the `remove-column!` or `remove-columns!` functions.
15451510

15461511

1547-
### Rename columns
1512+
### rename columns
15481513

15491514
Sometimes data sources can have variable names that we want to change.
15501515
To do this, use the `rename-column!` function. This example will
@@ -1607,7 +1572,7 @@ mtcars:model
16071572
"Ford Pantera L" "Ferrari Dino" "Maserati Bora" "Volvo 142E")
16081573
```
16091574

1610-
### Replace columns
1575+
### replace columns
16111576

16121577
Columns are "setf-able" places and the simplest way to replace a
16131578
column is set the field to a new value. We'll complement the `sex`
@@ -1659,7 +1624,7 @@ instead of `setf`-ing `*d*`:
16591624
;; 4 Male 30 170 79.4
16601625
```
16611626

1662-
### Transform columns
1627+
### transform columns
16631628

16641629
There are two functions for column transformations, `replace-column`
16651630
and `map-columns`.
@@ -1721,6 +1686,41 @@ categorical values like gender/sex.
17211686
As the name suggests, row operations operate on each row, or
17221687
observation, of a data set.
17231688

1689+
### add rows
1690+
1691+
Adding rows is done with the [array-operations stacking functions](/docs/manuals/array-operations/#stacking). Since these functions operate on both arrays and data frames, we can use them to stack data frames, arrays, or a mixture of both, providing they have a rank of 2. Here's an example of adding a row to the `mtcars` data frame:
1692+
1693+
```lisp
1694+
(defparameter boss-mustang
1695+
#("Boss Mustang" 12.7d0 8 302 405 4.11d0 2.77d0 12.5d0 0 1 4 4))
1696+
```
1697+
and now stack it onto the `mtcars` data set (load it with `(data :mtcars)` if you haven't already done so):
1698+
```lisp
1699+
(matrix-df
1700+
(keys mtcars)
1701+
(stack-rows mtcars boss-mustang))
1702+
```
1703+
This is the functional equivalent of R's `rbind` function. You can also add columns with the `stack-cols` function.
1704+
1705+
An often asked question is: why don't you have a dedicated `stack-rows` function? Well, if you want one it might look like this:
1706+
```lisp
1707+
(defun stack-rows (df &rest objects)
1708+
"Stack rows that works on matrices and/or data frames."
1709+
(matrix-df
1710+
(keys df)
1711+
(apply #'aops:stack-rows (cons df objects))))
1712+
```
1713+
But now the data frame must be the first parameter passed to the function. Or perhaps you want to rename the columns? Or you have matrices as your starting point? For all those reasons, it makes more sense to pass in the column keys than a data frame:
1714+
```lisp
1715+
(defun stack-rows (col-names &rest objects)
1716+
"Stack rows that works on matrices and/or data frames."
1717+
(matrix-df
1718+
(keys col-names)
1719+
(stack-rows objects)))
1720+
```
1721+
However this means we have two `stack-rows` functions, and you don't really gain anything except an extra function call. So use the above definition if you like; we use the first example and call `matrix-df` and `stack-rows` to stack data frames.
1722+
1723+
17241724
### count-rows
17251725

17261726
This function is used to determine how many rows meet a certain

content/en/docs/Manuals/select.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ corresponding axis. The selection specifications are found below.
3939

4040
To select a column, pass in `t` for the rows `selection1`, and the
4141
columns names (for a data frame) or column number (for an array) for
42-
`selection2`. For example, to select the first column of this array:
42+
`selection2`. For example, to select the second column of this array
43+
(remember Common Lisp has zero based arrays, so the second column is
44+
at index 1.
4345

4446
```lisp
4547
(select #2A((C0 C1 C2)

content/en/docs/Tutorials/plotting.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ and compare it with the equivalent Lisp-Stat version:
8585
:y (:field :miles-per-gallon :type :quantitative)))))
8686
```
8787

88-
Note that in the Lisp-Stat version we are embedding the specification,
88+
Note that in the Lisp-Stat version we are embedding the data
8989
using the `:values` keyword, as opposed to obtaining it from a server
9090
with `:url`. You can try plotting this now: click on the `copy` button
9191
in the upper right corner of the code box and paste it into the REPL.

0 commit comments

Comments
 (0)