Reputation: 4309
I created a dataset to illustrate the problem that I have.
My data looks like this
id time act
1 1 time1 a
2 1 time2 a
3 1 time3 a
4 1 time101 a
5 1 time103 a
6 1 time1001 b
7 1 time1003 b
9 1 time10000 b
10 1 time100010 c
What I want is to spread
the data with time
in the correct order, like this :
id 1 2 3 101 103 1001 1003 1004 10000 100010
1 a a a a a b b b b c
Here is what I do not fully understand. When I spread
my data I get something like
library(dplyr)
library(tidyr)
dt %>% spread(time, act)
id time1 time10000 time100010 time1001 time1003 time1004 time101 time103 time2 time3
1 1 a b c b b b a a a a
So R
seems to recognise so some of numerical order but considers that time10000
is prior to 2
or 3
.
Why is it so ? and I could I solve this problem.
What I would like is this :
id time1 time2 time3 time101 time103 time1001 time1003 time1004 time10000 time100010
1 1 a a a a a b b b b c
The data
dt = structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
time = structure(c(1L, 9L, 10L, 7L, 8L, 4L, 5L, 6L, 2L, 3L
), .Label = c("time1", "time10000", "time100010", "time1001",
"time1003", "time1004", "time101", "time103", "time2", "time3"
), class = "factor"), act = structure(c(1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L), .Label = c("a", "b", "c"), class = "factor")), .Names = c("id",
"time", "act"), class = "data.frame", row.names = c(NA, -10L))
Upvotes: 0
Views: 65
Reputation: 3728
Reorder your factor levels:
> dt$time<-factor(dt$time, as.character(dt$time))
> dt %>% spread(time, act)
id time1 time2 time3 time101 time103 time1001 time1003 time1004 time10000
1 1 a a a a a b b b b
time100010
1 c
Upvotes: 4