Coder Social home page Coder Social logo

spread changes factor order about tidyr HOT 4 CLOSED

tidyverse avatar tidyverse commented on August 17, 2024
spread changes factor order

from tidyr.

Comments (4)

mrdwab avatar mrdwab commented on August 17, 2024

This actually seems to also affect the "id" variables. See, for example, here: http://stackoverflow.com/q/29381069/1270695

df = data.frame(name=c("B","B","A","A"),
                group=c("g1","g2","g1","g2"),
                V1=c(10,40,20,30),
                V2=c(6,3,1,7))

gather(df, Var, Val, V1:V2) %>% 
  unite(VarG, Var, group) %>% 
  spread(VarG, Val)

Note the factor levels for "name" in the input and output.

> str(.Last.value)
'data.frame':   2 obs. of  5 variables:
 $ name : Factor w/ 2 levels "A","B": 1 2
 $ V1_g1: num  20 10
 $ V1_g2: num  30 40
 $ V2_g1: num  1 6
 $ V2_g2: num  7 3
> str(df)
'data.frame':   4 obs. of  4 variables:
 $ name : Factor w/ 2 levels "A","B": 2 2 1 1
 $ group: Factor w/ 2 levels "g1","g2": 1 2 1 2
 $ V1   : num  10 40 20 30
 $ V2   : num  6 3 1 7

from tidyr.

dataRangler avatar dataRangler commented on August 17, 2024

I asked the SO question:-) I am a beginner but when I looked at the spread.R source code, line 79 seems to be the start of the sorting. Is it necessary? I am new to R and github and do not know how to test this yet.

# Add in missing values, if necessary
if (length(overall) < n) {
  overall <- match(seq_len(n), overall, nomatch = NA)
} else {
  overall <- order(overall)
}

from tidyr.

dataRangler avatar dataRangler commented on August 17, 2024

I've just found dplyr::summarise does sorting as well. Is this a design philosophy?

df %>% 
+ group_by(name) %>% 
+ summarise(n()
+ )
Source: local data frame [2 x 2]

  name n()
1    A   2
2    B   2

from tidyr.

hadley avatar hadley commented on August 17, 2024

Instead of unite() do:

d1 %>% mutate(univar = f1:f2, f1 = NULL, f2 = NULL) %>% spread(univar,val)
# OR
d1 %>% mutate(univar = interaction(f1, f2), f1 = NULL, f2 = NULL) %>% spread(univar,val)

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.