I'm trying to transform a data frame with several categorical variables to frequency counts.
Data looks like this:
Site Date ID X1 X2 X3
A June - 01/16 1 aware resting resting
B June - 03/16 2 aware feeding feeding
C June - 01/16 1 resting aware aware
And I want to transform it into something like this:
site date ID aware resting feeding
A June - 01/16 1 3 2 1
B June - 01/16 2 1 0 2
I've tried to use dplyr but didn't manage to select all the variables I want (X1, X2 and X3)
data_frame %>%
dplyr::count((data_frame[c(1:3)]),cbind(data_frame[c(4:6)])) %>%
tidyr::spread(key = (data_frame[c(4:6)]),value = n)
This is an example of my data:
data_frame <- structure(data.frame(site = c("A", "B", "C", "A", "B", "C", "D"),
date = c("June - 01/16","June - 03/16", "June - 01/16", "June - 01/16", "June - 03/16", "June - 03/16", "June - 03/16"),
ID = c("1", "2", "1", "3", "1", "2", "3"),
X1= c("aware", "aware","resting","feeding","aware", "resting","feeding"),
X2 = c("resting","feeding","aware","na","na","aware","resting"),
X3 = c("resting","feeding","aware", "aware","resting","feeding","aware")))