-
Notifications
You must be signed in to change notification settings - Fork 416
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 changes factor order #47
Comments
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 |
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.
|
I've just found
|
Instead of 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) |
Hi, and thank you for this package!
One question arose, when using tidyr version 0.2.0 from cran.
I want to reshape a longtable to a wide one, which is no problem, but the outcome (ordering) is somehow unexpected.
When using
unite()
, the new variabel is of type character, which does not contain any information about category ordering. So the output columns are sorted alphabetically, after usingspread()
. Is there any way, to keep the factor-ordering information - or to avoidunite()
(submitting > 1 keys tospread()
) - i fear currently this is not possible?Would this be possible in future versions? It would be extremely helpful for creating tables automatically, when a certain order of columns is given a-priori.
Thank you!
Manuel
The text was updated successfully, but these errors were encountered: