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

spread with a data.frame of only two columns key and value #41

Closed
matthieugomez opened this Issue Oct 21, 2014 · 4 comments

Comments

Projects
None yet
5 participants
@matthieugomez

matthieugomez commented Oct 21, 2014

df <- data.frame(
    x = c(1, 2),
    y = c(3, 4)
)
spread(df, x, y) 

I expected df to have only one row. Is is really the intended behavior?

@joshualande

This comment has been minimized.

joshualande commented Dec 17, 2014

To be clear, the output is:

> df <- data.frame( x = c(1, 2), y = c(3, 4))
> spread(df, x, y) 
   1  2
1  3 NA
2 NA  4

I would have expected the output to have one row:

   1  2
1  3  4
@alexbbrown

This comment has been minimized.

alexbbrown commented Jan 16, 2015

Me too: in the absence of an identifying column it should all be assumed to have the same identifier. This is important in nested (group) processing. Right now it's necessary to ungroup before spread to bring the identifiers into scope.

@kleinschmidt

This comment has been minimized.

kleinschmidt commented Nov 6, 2015

I've also just run into this (tidyr 0.3.1). @hadley, can you weigh in on whether this is a bug or expected behavior?

FWIW I was trying to use spread on the output of broom::tidy to get the estimate values into a single row with columns named for term, as in:

library(tidyr)
library(broom)
library(dplyr)

d <- data_frame(x = runif(100, 0, 10),
                y = 5 - x + rnorm(100))

d %>%
  lm(y ~ x, data=.) %>%
  tidy() %>%
  select(term, estimate) %>%
  spread(term, estimate) %>%
  mutate(x_intercept = -`(Intercept)` / x)
#>   (Intercept)         x x_intercept
#> 1    5.148648        NA          NA
#> 2          NA -1.010463          NA

d %>%
  lm(y ~ x, data=.) %>%
  tidy() %>%
  select(term, estimate) %>%
  mutate(dummy='') %>%
  spread(term, estimate) %>%
  mutate(x_intercept = -`(Intercept)` / x)
#>   dummy (Intercept)         x x_intercept
#> 1          5.148648 -1.010463    5.095337

Seems like a perfectly good use case for tidyr + broom so I was surprised.

@hadley

This comment has been minimized.

Member

hadley commented Dec 30, 2015

More illuminating example:

data_frame(
  key = c("a", "b", "c"),
  value = c(1, 2, 3)
) %>%
  spread(key, value)

This is definitely a bug

@hadley hadley closed this in 972e48a Dec 30, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment