Coder Social home page Coder Social logo

Comments (5)

sjackman avatar sjackman commented on September 27, 2024

In the following example, spread.integer has the expected shape—three columns y, a and b—but spread.factor does not have the expected shape. It has instead two columns, and the second column is a matrix.

> str(spread.integer)
'data.frame':   2 obs. of  3 variables:
 $ y: Factor w/ 2 levels "c","d": 1 2
 $ a: int  1 2
 $ b: int  3 4
> str(spread.factor)
'data.frame':   2 obs. of  2 variables:
 $ y      : Factor w/ 2 levels "c","d": 1 2
 $ ordered: factor [1:2, 1:2] w x y z
  ..- attr(*, "levels")= chr  "w" "x" "y" "z"
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "a" "b"

from tidyr.

hadley avatar hadley commented on September 27, 2024

Slightly simpler MRE

library(dplyr)
library(tidyr)

data <- data.frame(x = c("a", "a", "b", "b"), y = c("c", "d", "c", "d"), z = c("w", "x", "y", "z"))
data %>% spread(x, z) %>% str()
data %>% mutate(z = as.integer(z)) %>% spread(x, z) %>% str()

from tidyr.

hadley avatar hadley commented on September 27, 2024

Root cause is that as.data.frame() does not work nicely on 2-d factors:

x <- factor(letters[1:4])
dim(x) <- c(2, 2)
as.data.frame(x)

from tidyr.

hadley avatar hadley commented on September 27, 2024

Better now, although the output columns are characters. I'm not sure if this reasonable, or if each column should be a factor with the same levels.

from tidyr.

sjackman avatar sjackman commented on September 27, 2024

Thanks, Hadley. Better would be that the output columns are factors with the same levels as the input.

from tidyr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.