Reputation: 1441
I'm trying to calculate some home range estimates for some animal tracking data. To do so the authors of the amt package recommend grouping by ID and then nesting.
But when I do so I get the following error:
Error: The
groups
attribute is not a data frame with its last column called.rows
.
Here's an example that reproduces the error:
library(amt)
library(tidyverse)
data(deer)
mini_deer <- deer[1:100, ]
nesttrk <- mini_deer %>% group_by(burst_) %>%
nest()
nesttrk
The overall aim is to get a home range size estimate per ID (or burst_ in this case)
mini_deer %>% group_by(burst_) %>% nest(-burst_) %>%
mutate(mcparea = map(data, ~ hr_mcp(., levels = c(0.95)) %>% hr_area)) %>%
dplyr::select(burst_, mcparea) %>% unnest()
Upvotes: 1
Views: 893
Reputation: 887128
We could use nest_by
with mutate
itself and then unnest
the list
column
library(amt)
library(dplyr)
library(tidyr)
mini_deer %>%
nest_by(burst_) %>%
transmute(mcparea = list(hr_mcp(data, levels = 0.95) %>%
hr_area)) %>%
ungroup %>%
unnest(mcparea)
# A tibble: 5 x 4
burst_ level what area
<dbl> <dbl> <chr> <dbl>
1 1 0.95 estimate 180927.
2 2 0.95 estimate 1886603.
3 3 0.95 estimate 0
4 4 0.95 estimate 0
5 5 0.95 estimate 14476.
Upvotes: 3
Reputation: 388982
I have not used amt
package but from the error message using nest
it seems that it requires data in some specific format as nesting with dataframe works without any problem. (mini_deer %>% as.data.frame() %>% group_by(burst_) %>% nest()
) but we loose the class as we are converting to dataframe.
Split and combine approach works -
library(amt)
library(tidyverse)
mini_deer %>%
group_split(burst_, .keep = TRUE) %>%
map_df(~ hr_mcp(., levels = c(0.95)) %>% hr_area) %>%
mutate(burst_ = unique(mini_deer$burst_), .before = 1)
# burst_ level what area
# <dbl> <dbl> <chr> <dbl>
#1 1 0.95 estimate 180927.
#2 2 0.95 estimate 1886603.
#3 3 0.95 estimate 0
#4 4 0.95 estimate 0
#5 5 0.95 estimate 14476.
Upvotes: 3