I have a data.list like so:
list(structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), species = structure(c(3L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 2L, 4L, 1L, 4L, 2L, 3L, 3L, 3L, 2L, 2L), .Label =
c("Apiaceae",
"Ceyperaceae", "Magnoliaceae", "Vitaceae"), class = "factor"),
N = c(2L, 2L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 3L, 2L, 3L, 2L,
3L, 1L, 4L, 3L, 4L, 1L, 2L, 2L, 2L, 1L, 1L)), class = "data.frame",
row.names = c(NA,
-24L)), structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), species = structure(c(3L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 2L, 4L, 1L, 4L, 2L, 3L, 3L, 3L, 2L, 2L), .Label =
c("Apiaceae",
"Ceyperaceae", "Magnoliaceae", "Vitaceae"), class = "factor"),
N = c(2L, 2L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 3L, 2L, 3L, 2L,
3L, 1L, 4L, 3L, 4L, 1L, 2L, 2L, 2L, 1L, 1L)), class = "data.frame",
row.names = c(NA,
-24L)))
I want to apply my.fun which was written within dplyr package to this list of data. First, I grouped data by "group" and get an output of the function which has already in R and then I applied this function to the data list. But output is 0. there isn't any output. Can you help me to find out the mistake?
my.fun <- function(x, y){
group_by(x, !!as.name(group)) %>%
mutate(out = diversity(N, "shannon"))
}
check <- lapply(colnames(list), function(x) {
my.fun(x$group, x$N)
})
Thanks a lot!