helianthus
helianthus

Reputation: 73

frequency table with conditions inside dplyr

I have this piece of code that produces a frequency table based on the values of the column Dist per day (from the column date). This works for one individual.

# A tibble: 3,233 × 5
# Groups:   bird_ID [3]
   bird_ID longitude latitude date          Dist
   <chr>       <dbl>    <dbl> <date>       <dbl>
 1 375E         53.3     67.8 2022-05-21   2532.
 2 375E         53.3     67.8 2022-05-21  82568.
 3 375E         53.3     67.8 2022-05-21 350535.
 4 375E         53.3     67.8 2022-05-21   2532.
 5 375E         53.3     67.8 2022-05-21  82573.
 6 375E         53.3     67.8 2022-05-21 350541.
 7 375E         53.3     67.8 2022-05-21   2541.
 8 375E         53.3     67.8 2022-05-21  82576.
 9 375E         53.3     67.8 2022-05-21 350544.
10 375E         53.3     67.8 2022-05-21   2537.
# … with 3,223 more rows
# ℹ Use `print(n = ...)` to see more rows

The script and the output:

visits50 <- data.frame(table(subset(df, Dist < 50)$date))

print(visits50)
Var1 Freq
1 2022-06-02   18
2 2022-06-03   18
3 2022-06-04   19
4 2022-06-05   17
5 2022-06-06   16
6 2022-06-08   11

I wanted to create a pipe with dplyr, in order to apply this to multiple individuals:

test <- df %>%
  group_by(bird_ID) %>%
  {table(subset(df, Dist < 50)$date, df$bird_ID)}

And I keep getting this error:

Error in table(subset(df, Dist < 50)$date, df$bird_ID) : 
  all arguments must have the same length

Any ideas on how to approach this? Because obviously the frequencies are having different lengths.

Upvotes: 1

Views: 208

Answers (1)

Seth
Seth

Reputation: 3864

If you share a sample of your data using dput() it will be easier for people to consider your question.

The following dplyr approach should work:

library(dplyr)

df %>%
  filter(Dist < 50)
  group_by(bird_ID) %>%
  count(date)

Upvotes: 1

Related Questions