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

Named append #99

Closed
hadley opened this Issue Jun 17, 2016 · 5 comments

Comments

Projects
None yet
3 participants
@hadley
Member

hadley commented Jun 17, 2016

It would be useful to have some way to add a single column to a specific position in a data frame.

Here's a basic implementation that does what I want, but could clearly be optimised.

insert_col <- function(data, x, after, name = deparse(substitute(x))) {
  as.data.frame(append(data, setNames(list(x), name), after))
}

mtcars %>% insert_col(1:32, 0, "id")

The example gives something I want to do fairly commonly: insert an id column at the front of a data frame. The name of the id column is often given by a variable, so it's useful to have an interface that doesn't use names.

It might also be useful to have insert_cols() which worked like append, but preserved the output class. That would be useful if you had multiple columns you wanted to insert.

@jennybc

This comment has been minimized.

Member

jennybc commented Jun 17, 2016

If something like this could be used from dplyr::mutate(), that would really make my day. It is common to have a pipeline that ends with select() just to hitch newly created variables up into position.

@jennybc

This comment has been minimized.

Member

jennybc commented Jul 22, 2016

This is very related to the request in #85, but over there it's described as "shelved"? I also need to add a variable to the front of a tibble, which is kind of awkward right now.

@krlmlr

This comment has been minimized.

Member

krlmlr commented Jul 30, 2016

We have add_row(), so add_column() is a logical extension, and much simpler than a generic cbind() (#85). I'd support both before and after arguments (as name or index, one of them takes precedence).

@hadley

This comment has been minimized.

Member

hadley commented Jul 30, 2016

@krlmlr I like that idea!

@krlmlr krlmlr added the ready label Jul 30, 2016

@jennybc

This comment has been minimized.

Member

jennybc commented Jul 30, 2016

Will the before/after flexibility propagate back to add_row()? That also comes up. Also would make the lack of rbind() slightly less painful (#34).

@krlmlr krlmlr modified the milestone: 2.0 Aug 8, 2016

@krlmlr krlmlr self-assigned this Aug 15, 2016

@krlmlr krlmlr added in progress and removed ready labels Aug 15, 2016

krlmlr added a commit that referenced this issue Aug 15, 2016

Merge branch 'f-#99-add-column'
- New `add_column()`, analogously to `add_row()` (#99).

@krlmlr krlmlr closed this in 46f89af Aug 16, 2016

@krlmlr krlmlr removed the in progress label Aug 16, 2016

krlmlr added a commit that referenced this issue Aug 16, 2016

Merge tag 'v1.1-6'
- Internal: Cleanup of formatting code.
- `add_row()` now correctly handles existing columns of type `list` that are not updated (#148).
- `nibble()` has been renamed to `tribble()`, stands for "transposed tibble" (#143).
- `add_row()` and `add_column()` gain `.before` and `.after` arguments which indicate the row (by number) or column (by number or name) before or after which the new data are inserted. Updated or added columns cannot be named `.before` or `.after` (#99).
- New `add_column()`, analogously to `add_row()` (#99).
- `add_row()` now can add multiple rows, with recycling (#142, @jennybc).

krlmlr added a commit that referenced this issue Aug 26, 2016

Merge tag 'v1.2'
- The `tibble.width` option is used for `glimpse()` only if it is finite (#153, @kwstat).
- New `as_tibble.poly()` to support conversion of a `poly` object to a tibble (#110).
- `add_row()` now correctly handles existing columns of type `list` that are not updated (#148).
- `all.equal()` doesn't throw an error anymore if one of the columns is named `na.last`, `decreasing` or `method` (#107, @BillDunlap).

- New `add_column()`, analogously to `add_row()` (#99).
- `print.tbl_df()` gains `n_extra` method and will have the same interface as `trunc_mat()` from now on.
- `add_row()` and `add_column()` gain `.before` and `.after` arguments which indicate the row (by number) or column (by number or name) before or after which the new data are inserted. Updated or added columns cannot be named `.before` or `.after` (#99).
- Rename `frame_data()` to `tribble()`, stands for "transposed tibble". The former is still available as alias (#132, #143).

- `add_row()` now can add multiple rows, with recycling (#142, @jennybc).
- Use multiply character `×` instead of `x` when printing dimensions (#126). Output tests had to be disabled for this on Windows.
- Back-tick non-semantic column names on output (#131).
- Use `dttm` instead of `time` for `POSIXt` values (#133), which is now used for columns of the `difftime` class.
- Better output for 0-row results when total number of rows is unknown (e.g., for SQL data sources).

- New object summary vignette that shows which methods to define for custom vector classes to be used as tibble columns (#151).
- Added more examples for `print.tbl_df()`, now using data from `nycflights13` instead of `Lahman` (#121), with guidance to install `nycflights13` package if necessary (#152).
- Minor changes in vignette (#115, @helix123).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment