If we need the count of 'A', 'B' and 'C', it may be better to reshape. We convert the 'data.frame' to 'data.table' (setDT(data_frame)
) and use dcast
from data.table
to reshape from 'long' to 'wide' format, specifying the fun.aggregate
as length
.
library(data.table)
dcast(setDT(data_frame), station+date~classification, length)
# station date A B C
#1: 1 June - 01/16 2 0 0
#2: 1 June - 03/16 1 0 0
#3: 2 June - 03/16 0 3 0
#4: 7 June - 01/16 0 0 1
A dplyr
option is
library(dplyr)
data_frame %>%
group_by(station, date, classification) %>%
tally()
# station date classification n
# (int) (chr) (chr) (int)
#1 1 June - 01/16 A 2
#2 1 June - 03/16 A 1
#3 2 June - 03/16 B 3
#4 7 June - 01/16 C 1
data
data_frame <- structure(list(station = c(1L, 2L, 1L, 7L, 1L, 2L, 2L),
date = c("June - 01/16",
"June - 03/16", "June - 01/16", "June - 01/16", "June - 03/16",
"June - 03/16", "June - 03/16"), classification = c("A", "B",
"A", "C", "A", "B", "B")), .Names = c("station", "date", "classification"
), class = "data.frame", row.names = c(NA, -7L))