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
``` 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
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!
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
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 ?
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.