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

Fix #162. Now add_column knows to recyle with data of length 1. #164

Merged
merged 2 commits into from Aug 30, 2016

Conversation

Projects
None yet
3 participants
@anhqle
Contributor

anhqle commented Aug 29, 2016

Fixes #162.

df_new <- add_column(df, b = 4)
expect_identical(ncol(df_new), ncol(df) + 1L)
expect_identical(df_new$b, rep(4, 3))
})

This comment has been minimized.

@anhqle

anhqle Aug 29, 2016

Contributor

Could you explain why we test with explicit details like this instead of the shorter expect_identical(df_new, tibble(a = 1:3, b = rep(4, 3))?

For consistency I used the current way of testing, but curious why.

This comment has been minimized.

@krlmlr

krlmlr Aug 29, 2016

Member

No particular reason. My test is weaker, too -- doesn't test order of columns. Feel free to adapt.

@codecov-io

This comment has been minimized.

codecov-io commented Aug 29, 2016

Current coverage is 99.58% (diff: 100%)

Merging #164 into master will increase coverage by 0.14%

@@             master       #164   diff @@
==========================================
  Files            16         16          
  Lines           716        721     +5   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits            712        718     +6   
+ Misses            4          3     -1   
  Partials          0          0          

Powered by Codecov. Last update cd336e5...0698165

R/add.R Outdated
@@ -102,7 +102,11 @@ add_column <- function(.data, ..., .before = NULL, .after = NULL) {
}

if (nrow(df) != nrow(.data)) {
stopc("Expected ", nrow(.data), " rows, got ", nrow(df))
if (nrow(df) == 1) {
df <- do.call(rbind, replicate(nrow(.data), df, simplify = FALSE))

This comment has been minimized.

@krlmlr

krlmlr Aug 29, 2016

Member

How about df <- df[rep(1L, nrow(.data)), ] ?

This comment has been minimized.

@anhqle

anhqle Aug 29, 2016

Contributor

I thought do.call(rbind) would pre-allocate memory and wouldn't be this slow. Yours is > 10 times faster.

@krlmlr krlmlr merged commit 411b246 into tidyverse:master Aug 30, 2016

3 checks passed

codecov/project 99.58% (+0.14%) compared to cd336e5
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@krlmlr

This comment has been minimized.

Member

krlmlr commented Aug 30, 2016

Thanks. See #167 for yet another corner case.

krlmlr added a commit that referenced this pull request Aug 30, 2016

Merge tag 'v1.2-12'
- Simplify tests for `add_row()` and `add_column()` (#165, #166, @LaDilettante).
- `add_column()` can add columns of length 1 (#162, #164, @LaDilettante).
- Singular and plural variants for error messages that mention a list of objects (#116, #138, @LaDilettante).

@anhqle anhqle deleted the anhqle:fix-add_column-fails-with-length-1-data branch Aug 30, 2016

@krlmlr krlmlr referenced this pull request Sep 21, 2016

Closed

Bug: add_column() #174

krlmlr added a commit that referenced this pull request Apr 1, 2017

Merge tag 'v1.3.0'
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment