Calculate a mean by groups with "circular"

Hello,
I'm trying to calculate the mean of degree values. My data set is composed of three groups (dir), which I want to compare. First, I tried to calculate this with the circular package, but there I got "values", which I can't split into groups.
With the following attempt, I got an error. I hope, anybody here can help me. Thank you!

``` r
dat<-tibble::tribble(
         ~dir, ~degree,
          "A",      2L,
          "A",     12L,
          "B",     78L,
          "B",    257L,
          "C",     57L,
          "C",    198L
         )
head(dat)
#> # A tibble: 6 x 2
#>   dir   degree
#>   <chr>  <int>
#> 1 A          2
#> 2 A         12
#> 3 B         78
#> 4 B        257
#> 5 C         57
#> 6 C        198

Created on 2022-01-27 by the reprex package (v2.0.1)

``` r
library(circular)
#> Warning: Paket 'circular' wurde unter R Version 4.0.5 erstellt
#> 
#> Attache Paket: 'circular'
#> The following objects are masked from 'package:stats':
#> 
#>     sd, var
library(dplyr)
#> Warning: Paket 'dplyr' wurde unter R Version 4.0.5 erstellt
#> 
#> Attache Paket: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
dat %>%
  group_by(dir) %>%
  summarise(
    circ_mean <-
      degree %>%
      circular(degree, units = "degrees", rotation = "counter", zero=0, modulo="2pi") %>%#data to circular format
      mean.circular()) %>% #mean
  ungroup()
#> Error in group_by(., dir): Objekt 'dat' nicht gefunden

Created on 2022-01-27 by the reprex package (v2.0.1)

See the FAQ: How to do a minimal reproducible example reprex for beginners for how to add enough data to allow better insight into the problem.

library(dplyr) should help. However don't know what you mean by:

circ_mean ->
      degree %>%

Thanks for pointing that out, I forgot when creating the reprex.

  circ_mean ->
      degree %>%
      circular(degree, units = "degrees", rotation = "counter", zero=0, modulo="2pi")

With this I want to put the data into a circular format.
I hope, I'm right, my knowledge of R is unfortunately limited...

Do you want to assign the value of the expression to circ_mean? If so,

circ_mean <-

Omg, that was really a bit hasty. I'm sorry. But it is still coming to an error message...

Just needed some simplification

library(circular)
#> 
#> Attaching package: 'circular'
#> The following objects are masked from 'package:stats':
#> 
#>     sd, var
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
dat <- tibble::tribble(
  ~dir, ~degree,
  "A",      2L,
  "A",     12L,
  "B",     78L,
  "B",    257L,
  "C",     57L,
  "C",    198L
)

dat %>%
  group_by(dir) %>%
  summarise(
    circ_mean =
      circular(degree, units = "degrees", rotation = "counter", zero=0, modulo="2pi") %>%
      #data to circular format
  mean.circular())
#> # A tibble: 3 × 2
#>   dir   circ_mean 
#>   <chr> <circular>
#> 1 A       7.0     
#> 2 B     167.5     
#> 3 C     127.5

# ungrouping not needed
dat
#> # A tibble: 6 × 2
#>   dir   degree
#>   <chr>  <int>
#> 1 A          2
#> 2 A         12
#> 3 B         78
#> 4 B        257
#> 5 C         57
#> 6 C        198
ungroup(dat)
#> # A tibble: 6 × 2
#>   dir   degree
#>   <chr>  <int>
#> 1 A          2
#> 2 A         12
#> 3 B         78
#> 4 B        257
#> 5 C         57
#> 6 C        198
dat
#> # A tibble: 6 × 2
#>   dir   degree
#>   <chr>  <int>
#> 1 A          2
#> 2 A         12
#> 3 B         78
#> 4 B        257
#> 5 C         57
#> 6 C        198

@technocrat Thank you very much, you help me enormously. Two questions:

The reprex works as planned, but with the real data, I get a new error...

``` r
data %>%
  group_by(dir) %>%
  summarise(
    circ_mean =
      circular(degree, units = "degrees", rotation = "counter", zero=0, modulo="2pi")%>%#data to circular format
      mean.circular())
#> Error in data %>% group_by(dir) %>% summarise(circ_mean = circular(degree, : konnte Funktion "%>%" nicht finden

My second question: what was my mistake, I can use your improvements, but I want to understand...
Thanks!

Did you include loading this?

Yes, dyplr is included.

Means that the magrittr package is not in namespace, which it should be if dplyr is loaded. Try adding

library (magrittr)

If not found, reinstall dyplr

Or replace %>% with |>, should work as well :slight_smile:

1 Like

Thanks for the hint, but it still doesn't work.

You should say more about the problems you have if you want help to solve them.
For better assistance , you are recommended to justify to the forum users trying to help you that 'it still doesn't work' by quoting back the code you are trying to use, and also the error or incorrect result that you get..

@nirgrahamuk I'm sorry, I completely forgot. Enclosed the reprex.

``` r
library(dplyr)
#> Warning: Paket 'dplyr' wurde unter R Version 4.0.5 erstellt
#> 
#> Attache Paket: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(magrittr)
#> Warning: Paket 'magrittr' wurde unter R Version 4.0.5 erstellt
data %>%
  group_by(dir) %>%
  summarise(
    circ_mean =
      circular(degree, units = "degrees", rotation = "counter", zero=0, modulo="2pi")%>%#data to circular format
      mean.circular())
#> Error in UseMethod("group_by"): nicht anwendbare Methode für 'group_by' auf Objekt der Klasse "function" angewendet

Created on 2022-01-31 by the reprex package (v2.0.1)

if your starting data.frame that is loaded into your environment is called dat but you typed out data in your most recent variation, that would explain the error...
Can you clarify your use of data ?

Oh, excuse me. First, I created an example, where I used "dat" as dataset. In the real dataset, it's called data.

lets review...
does the following give you an error ?

dat <- tibble::tribble(
  ~dir, ~degree,
  "A",      2L,
  "A",     12L,
  "B",     78L,
  "B",    257L,
  "C",     57L,
  "C",    198L
)
library(circular)
dat %>%
  group_by(dir) %>%
  summarise(
    circ_mean =
      circular(degree, units = "degrees", rotation = "counter", zero=0, modulo="2pi")%>%#data to circular format
      mean.circular())

No, everything works as planned.

Do you require additional support ?
Your earlier error Error in UseMethod("group_by"): nicht anwendbare Methode für 'group_by' auf Objekt der Klasse "function" angewendet
tells me that at the time your code was run, the data object name was not bound to your loaded data, but rather to the default utils::data() function.

1 Like