-
Notifications
You must be signed in to change notification settings - Fork 420
Closed
Description
Trying to create a cross tabulation using tally() combined with spread() works nicely, unless there are missing values in the key columns.
mtcars %>%
group_by(cyl, gear) %>%
tally() %>%
spread(gear, n, fill = 0)
Source: local data frame [3 x 4]
cyl 3 4 5
1 4 1 8 2
2 6 2 4 1
3 8 12 0 2
mtcars %>%
mutate(
cyl = ifelse(cyl > 6, NA, cyl),
gear = ifelse(gear > 4, NA, gear)
) %>%
group_by(cyl, gear) %>%
tally()
Source: local data frame [8 x 3]
Groups: cyl
cyl gear n
1 4 3 1
2 4 4 8
3 4 NA 2
4 6 3 2
5 6 4 4
6 6 NA 1
7 NA 3 12
8 NA NA 2
# DITTO # %>%
spread(gear, n)
Error in if (any(names2(x) == "")) { :
missing value where TRUE/FALSE needed
It seems like a good solution would be to either treat NA values as another key or to drop them from the resulting table, controlled by an option switch. Like useNA = "always" in the base table() function, but with better syntax.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels