My data are ordered observations and I want to keep the ordering as much as possible while doing manipulations.
Take the answer for this question, I put "B" ahead of "A" in the dataframe. The resulting wide data are sorted by the column "name", i.e., "A" first, then "B".
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)
name V1_g1 V1_g2 V2_g1 V2_g2
1 A 20 30 1 7
2 B 10 40 6 3
Is there a way to keep the original ordering? like this:
name V1_g1 V1_g2 V2_g1 V2_g2
1 B 10 40 6 3
2 A 20 30 1 7
04/02 edit: I've just found the dplyr::summarise
does sorting as well. arrange(name, df$name)
still works to restore the order. But I wonder if the extra sorting is necessary from the design of the packages?
df %>%
group_by(name) %>%
summarise(n()) %>%
name n()
1 A 2
2 B 2