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

add_column fails with only data of length 1 #162

Closed
MikeBadescu opened this issue Aug 28, 2016 · 6 comments
Closed

add_column fails with only data of length 1 #162

MikeBadescu opened this issue Aug 28, 2016 · 6 comments

Comments

@MikeBadescu
Copy link

@MikeBadescu MikeBadescu commented Aug 28, 2016

From the documentation:

This works

df <- tibble(x = 1:3, y = 3:1)
add_column(df, z = -1:1, w = 0)

This does not work ("Error: Expected 3 rows, got 1")

df <- tibble(x = 1:3, y = 3:1)
add_column(df, w = 0)
@anhqle
Copy link
Contributor

@anhqle anhqle commented Aug 29, 2016

This is caused here in add.R

When both z and w are specified, the tibble to-be-added has 3 rows. In contrst, when only w is specified, the tibble to be added only has 1 row, and thus cannot be added to the original tibble.

add_column <- function(.data, ..., .before = NULL, .after = NULL) {
  df <- tibble(...)

  if (nrow(df) != nrow(.data)) {
    stopc("Expected ", nrow(.data), " rows, got ", nrow(df))
}

@krlmlr Seems like solution is for add_column() to check whether one column is being added. If yes, expand the new tibble to have the same rows as the original tibble. Any thoughts?

@krlmlr
Copy link
Member

@krlmlr krlmlr commented Aug 29, 2016

@LaDilettante: The following should work, too:

add_column(iris, a = 1, b = 2)

However, the code below still should give an error:

add_column(iris, a = 1:2)

@krlmlr krlmlr closed this in #164 Aug 30, 2016
krlmlr added a commit that referenced this issue Aug 30, 2016
…ength-1-data

- `add_column()` can add columns of length 1 (#162, #164, @LaDilettante).
krlmlr added a commit that referenced this issue Aug 30, 2016
- 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).
@MikeBadescu
Copy link
Author

@MikeBadescu MikeBadescu commented Aug 30, 2016

Thank you!

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.
@hbeale
Copy link

@hbeale hbeale commented Nov 17, 2017

I've updated my tidyverse and I still encounter this issue.

> packageVersion("tibble")
[1] ‘1.3.4’
> tibble(cars) %>% add_column(newCol="sfdsdfs")
Error: Each variable must be a 1d atomic vector or list.
Problem variables: 'cars'

@anhqle
Copy link
Contributor

@anhqle anhqle commented Nov 17, 2017

@hbeale Your error is in the tibble(cars) part, not in the add_column() part.

Use as_tibble(cars) %>% add_column(newCol="sfdsdfs")

@github-actions
Copy link

@github-actions github-actions bot commented Dec 12, 2020

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 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants