I'm learning dplyr, having come from plyr, and I want to generate (per group) columns (per interaction) from the output of xtabs.
Short summary: I'm getting
A B
1 NA
NA 2
when I wanted
A B
1 2
xtabs data looks like this:
> xtabs(data=data.frame(P=c(F,T,F,T,F),A=c(F,F,T,T,T)))
A
P FALSE TRUE
FALSE 1 2
TRUE 1 1
now do(
wants it's data in data frames, like this:
> xtabs(data=data.frame(P=c(F,T,F,T,F),A=c(F,F,T,T,T))) %>% as.data.frame
P A Freq
1 FALSE FALSE 1
2 TRUE FALSE 1
3 FALSE TRUE 2
4 TRUE TRUE 1
Now I want a single row output with columns being the interaction of levels. Here's what I'm looking for:
FALSE_FALSE TRUE_TRUE FALSE_TRUE TRUE_FALSE
1 1 2 1
But instead I get
> xtabs(data=data.frame(P=c(F,T,F,T,F),A=c(F,F,T,T,T))) %>%
as.data.frame %>%
unite(S,A,P) %>%
spread(S,Freq)
FALSE_FALSE FALSE_TRUE TRUE_FALSE TRUE_TRUE
1 1 NA NA NA
2 NA 1 NA NA
3 NA NA 2 NA
4 NA NA NA 1
I'm clearly misunderstanding something here. I'm looking for the equivalent of reshape2's code here (using magrittr pipes for consistency):
> xtabs(data=data.frame(P=c(F,T,F,T,F),A=c(F,F,T,T,T))) %>%
as.data.frame %>% # can be omitted. (safely??)
melt %>%
mutate(S=interaction(P,A),value=value) %>%
dcast(NA~S)
Using P, A as id variables
NA FALSE.FALSE TRUE.FALSE FALSE.TRUE TRUE.TRUE
1 NA 1 1 2 1
(note NA is used here because I don't have a grouping variable in this simplified example)
Update - interestingly, adding a single grouping column seems to fix this - why does it synthesise (presumably from row_name) a grouping column without me telling it?
> xtabs(data=data.frame(h="foo",P=c(F,T,F,T,F),A=c(F,F,T,T,T))) %>%
as.data.frame %>%
unite(S,A,P) %>%
spread(S,Freq)
h FALSE_FALSE FALSE_TRUE TRUE_FALSE TRUE_TRUE
1 foo 1 1 2 1
This seems like a partial solution.
See Question&Answers more detail:
os