I have a large data frame that I'd like to split/group by three variables and then use the 'circular' package to compute, for now, the mean phase of some degree values. The depth of my knowledge using dplyr and so on is limited to grouping, mutating and summarising. Unfortunately, that is insufficient for my current problem.
My data looks something like this:
data <- data.frame(
groupA = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "A",
"A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "A", "A",
"A", "A", "A", "B", "B", "B", "B", "B", "B"),
groupB = c("L", "M", "N", "L", "M", "L", "M", "N", "L", "M", "N", "L",
"M", "N", "L", "M", "L", "M", "N", "L", "M", "N", "L", "M",
"N", "L", "M", "L", "M", "N", "L", "M", "N"),
groupC = c("X", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "Y", "X", "X",
"X", "X", "Y", "Y", "X", "X", "Y", "Y", "Y", "X", "X", "X",
"X", "Y", "Y", "X", "X", "Y", "Y", "Y", "X"),
degrees = c(10, 111.5, 360, 90, 120, 180, 50, 60, 70, 80, 90, 10, 100,
360, 90, 120, 180, 50, 60, 70, 80, 90, 10, 100, 360, 90, 120,
180, 50, 60, 70, 80, 90)
)
What I would like to do is group the data by all three groups. Normally, I'd do this:
data %>%
group_by(groupA, groupB, groupC) %>%
....
Which works, but then I need to combine this with the circular package. I've only just begun to use this package so could be wrong, but my understanding is that I first need to convert the data type to circular
, and then I can compute the circular mean:
library(circular)
x <- c(350, 90)
test_circular <- circular(x, units = "degrees", rotation = "clock")
mean.circular(test_circular)
Circular Data:
Type = angles
Units = degrees
Template = none
Modulo = asis
Zero = 0
Rotation = clock
[1] 40
That throws out answers that make sense, but I can't seem to combine the above with dplyr methods. I've tried basically guessing the syntax involved but nothing seems to work, e.g.:
data %>%
group_by(groupA, groupB, groupC) %>%
do(circular(.$degrees))
Error: Results 1, 2, 3, 4, 5, ... must be data frames, not circular/numeric
This is the closest I've come, but I lose all other columns, so matching it to my groupings is difficult...
data %>%
group_by(groupA, groupB, groupC) %>%
do(x = as.data.frame(.$degrees)) %>%
do(circ_data = circular(.$x,
units = "degrees",
zero = 0,
rotation = "clock")) %>%
summarise(circ_mean = mean(circ_data))