我可以使用dplyr管而不是在r中的列表上使用lapply吗?
我想知道我是否可以使用Tidyverse来完成我到目前为止使用列表的任务。 我每个图都有一个物种丰度的矩阵,我想用 vegdist 从 vegan 包装中计算差异指数。之后,我想以较长的格式删除自动比较等。 它的工作方式像 dplyr 的魅力一样,很容易:
library(tidyverse)
library(vegan)
df <- data.frame(spec1=sample.int(50,10,replace=T),
spec2=sample.int(75,10,replace=T),
spec3=sample.int(10,10,replace=T),
spec4=sample.int(40,10,replace=T),
spec5=sample.int(50,10,replace=T),
spec6=sample.int(5,10,replace=T))
df%>%
vegdist() %>%
as.matrix() %>%
as_tibble(rownames= "rownames") %>%
pivot_longer(-rownames) %>%
filter(rownames < name)
现在我想做同样的事情,但是该物种属于不同的类别,每个类别都必须获得自己的距离矩阵,只有在它才能放回原处之后到单个长格式数据框架或tibble。
cat <- data.frame(spec=c("spec1","spec2","spec3","spec4","spec5","spec6"),
group=c("a","b","c","b","a","c"))
df%>%
pivot_longer(cols = everything(),values_to="abundance",names_to="spec")%>%
left_join(cat, by="spec")
开始非常简单,但是在我用来通过列 group 将数据拆分为列表的点,我正在努力寻找解决方案。我尝试了 group_by + pivot_wider + vegdist 或 group_split 的组合一个工作解决方案。有人是否有建议,还是我应该坚持此类案例的列表?
I'd like to know if I can use tidyverse for tasks I used lists in R so far.
I got a matrix of species abundances per plot for which I want to calculate dissimilarity indices with vegdist from the vegan package. After that I'd like to put it in long format remove auto comparisons etc.
It works like charm with dplyr for an easy example:
library(tidyverse)
library(vegan)
df <- data.frame(spec1=sample.int(50,10,replace=T),
spec2=sample.int(75,10,replace=T),
spec3=sample.int(10,10,replace=T),
spec4=sample.int(40,10,replace=T),
spec5=sample.int(50,10,replace=T),
spec6=sample.int(5,10,replace=T))
df%>%
vegdist() %>%
as.matrix() %>%
as_tibble(rownames= "rownames") %>%
pivot_longer(-rownames) %>%
filter(rownames < name)
Now I want to do the same however the species belong to different categories and each category has to get its own distance matrix and only after it can be put back to a single long format data frame or tibble.
cat <- data.frame(spec=c("spec1","spec2","spec3","spec4","spec5","spec6"),
group=c("a","b","c","b","a","c"))
df%>%
pivot_longer(cols = everything(),values_to="abundance",names_to="spec")%>%
left_join(cat, by="spec")
The beginning is pretty straight forward but at the point where I am used to split the data to a list by the column group I am struggling to find a solution. I tried combinations of group_by + pivot_wider + vegdist or also group_split but unfortunately wasn't able to come up with a working solution. Does anybody have suggestions, or should I stick with lists for such cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以
split
“ group”'spec'''基于list
的元素的列,并应用vegdist
-Output,
如果我们想要宽的格式,
We may
split
the 'spec' by 'group' into alist
, loop over thelist
and thenselect
the columns based on the elements from thelist
and apply thevegdist
-output
If we want a wide format,