Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mibble() / frame_matrix() #140

Closed
lionel- opened this issue Aug 3, 2016 · 8 comments
Closed

mibble() / frame_matrix() #140

lionel- opened this issue Aug 3, 2016 · 8 comments

Comments

@lionel-
Copy link
Member

lionel- commented Aug 3, 2016

I think it would make sense to have a matrix equivalent of nibble() / frame_data(). It would be useful for defining small covariance and correlation matrices in a readable way. Such small matrices are probably a more common use case than small data frames.

It should support column names:

frame_matrix(
  ~col1, ~col2,
     10,     3,
      3,     2
)

As well as row names:

frame_matrix(
  ~row1, 10, 3,
  ~row2,  3, 2
)

Or both:

frame_matrix(
  NULL,  ~col1, ~col2,
  ~row1,    10,     3,
  ~row2,     3,     2
)

Most of the time matrices don't have column names so it'd be useful to provide an alternative way of figuring out the size of the matrix, with an argument:

Sigma <- frame_matrix(ncol = 2,
  10, 3
  3,  2
)

These are all a lot more readable in a script than:

matrix(c(10, 3, 3, 2), 2)
@lionel-
Copy link
Member Author

lionel- commented Aug 3, 2016

And then we could have other verbs for symmetric matrices? They would work in the same way but would assume the input is triangular:

frame_lower(ncol = 2,
  10,
   3, 2
)

frame_upper(ncol = 2,
  10, 3,
      2
)

frame_lower(
  NULL,  ~var1, ~var2,
  ~var1,    10,
  ~var2,     3,     2
)

@krlmlr krlmlr modified the milestone: 2.0 Aug 8, 2016
@hadley
Copy link
Member

hadley commented Aug 10, 2016

I think we'd accept a PR that implements this, but it's not high enough priority for us to do.

@anhqle
Copy link
Contributor

anhqle commented Aug 13, 2016

I could contribute a PR, but unsure how much more convenient this is.

Comparing:

frame_matrix(
  ~col1, ~col2,
     10,     3,
      3,     2
)

and

matrix(c(10, 3,
         3, 2), 2)

I would love to see the appeal of the frame_matrix more clearly.

@lionel-
Copy link
Member Author

lionel- commented Aug 13, 2016

You are right that this is already pretty close, though arguably not aesthetic:

c(
  1, 2,
  3, 4
) %>% matrix(2)

I'd say the appeal of frame_matrix() would be:

  • To provide a consistent way of specifying small tabular data structures in R.
  • With the triangular version there is less chance of data entry mistakes.
  • The rownames and colnames can be specified inline alongside the data.

And maybe also the dimnames? E.g. you could substitute the NULL in

frame_lower(
  NULL,  ~var1, ~var2,
  ~var1,    10,
  ~var2,     3,     2
)

with a rows ~ cols specification:

frame_lower(dim1 ~ dim2,
         ~var1, ~var2,
  ~var1,    10,
  ~var2,     3,     2
)

@krlmlr krlmlr modified the milestone: 1.2 Aug 19, 2016
@anhqle
Copy link
Contributor

anhqle commented Aug 23, 2016

@lionel- Would you be worry that something like

frame_lower(dim1 ~ dim2,
         ~var1, ~var2,
  ~var1,    10,
  ~var2,     3,     2
)

would require extensive white-space editing to make things line up? A single change in the cell value or row/column name means a whole lot of editing to make things align again. I'm a little concerned that it would actually cause more data entry problems.

@lionel-
Copy link
Member Author

lionel- commented Aug 24, 2016

I think that's a general problem with these functions, they are more readable once done but require some manual editing to get spacing right.

@krlmlr
Copy link
Member

krlmlr commented Aug 26, 2016

@LaDilettante: The reformatting problem is alleviated with #127. Happy to review a PR.

anhqle pushed a commit to anhqle/tibble that referenced this issue Aug 30, 2016
- Test for frame_matrix() basic functionalities
- Extract the code that parses tribble(...) arguments. Re-use that code for both tribble and frame_matrix
anhqle pushed a commit to anhqle/tibble that referenced this issue Oct 9, 2016
- Test for frame_matrix() basic functionalities
- Extract the code that parses tribble(...) arguments. Re-use that code for both tribble and frame_matrix
anhqle pushed a commit to anhqle/tibble that referenced this issue Oct 10, 2016
- Test for frame_matrix() basic functionalities
- Extract the code that parses tribble(...) arguments. Re-use that code for both tribble and frame_matrix
anhqle pushed a commit to anhqle/tibble that referenced this issue Oct 10, 2016
- Test for frame_matrix() basic functionalities
- Extract the code that parses tribble(...) arguments. Re-use that code for both tribble and frame_matrix
@krlmlr krlmlr closed this as completed in 802fa26 Nov 30, 2016
krlmlr added a commit that referenced this issue Nov 30, 2016
- New `frame_matrix()` (#140, #168, @LaDilettante).
- The `max.print` option is ignored when printing a tibble (#194, #195, @t-kalinowski).
- Fix typo in `obj_sum` documentation (#193, @etiennebr).
- Keep column classes when adding row to empty tibble (#171, #177, @LaDilettante).
- Now explicitly stating minimum Rcpp version 0.12.3.
krlmlr pushed a commit that referenced this issue Jan 4, 2017
- Test for frame_matrix() basic functionalities
- Extract the code that parses tribble(...) arguments. Re-use that code for both tribble and frame_matrix
krlmlr added a commit that referenced this issue Apr 1, 2017
Bug fixes
=========

- Time series matrices (objects of class `mts` and `ts`) are now supported in `as_tibble()` (#184).
- The `all_equal()` function (called by `all.equal.tbl_df()`) now forwards to `dplyr` and fails with a helpful message if not installed. Data frames with list columns cannot be compared anymore, and differences in the declared class (`data.frame` vs. `tbl_df`) are ignored. The `all.equal.tbl_df()` method gives a warning and forwards to `NextMethod()` if `dplyr` is not installed; call `all.equal(as.data.frame(...), ...)` to avoid the warning. This ensures consistent behavior of this function, regardless if `dplyr` is loaded or not (#198).

Interface changes
=================

- Now requiring R 3.1.0 instead of R 3.1.3 (#189).
- Add `as.tibble()` as an alias to `as_tibble()` (#160, @LaDilettante).
- New `frame_matrix()`, similar to `frame_data()` but for matrices (#140, #168, @LaDilettante).
- New `deframe()` as reverse operation to `enframe()` (#146, #214).
- Removed unused dependency on `assertthat`.

Features
========

General
-------

- Keep column classes when adding row to empty tibble (#171, #177, @LaDilettante).
- Singular and plural variants for error messages that mention a list of objects (#116, #138, @LaDilettante).
- `add_column()` can add columns of length 1 (#162, #164, @LaDilettante).

Input validation
----------------

- An attempt to read or update a missing column now throws a clearer warning (#199).
- An attempt to call `add_row()` for a grouped data frame results in a helpful error message (#179).

Printing
--------

- Render Unicode multiplication sign as `x` if it cannot be represented in the current locale (#192, @ncarchedi).
- Backtick `NA` names in printing (#206, #207, @jennybc).
- `glimpse()` now uses `type_sum()` also for S3 objects (#185, #186, @holstius).
- The `max.print` option is ignored when printing a tibble (#194, #195, @t-kalinowski).

Documentation
=============

- Fix typo in `obj_sum` documentation (#193, @etiennebr).
- Reword documentation for `tribble()` (#191, @kwstat).
- Now explicitly stating minimum Rcpp version 0.12.3.

Internal
========

- Using registration of native routines.
@github-actions
Copy link
Contributor

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants