[英]How to expand a data frame to from summed to single observations
I have some Abundance data for Observations of an insect at different dates and places. 我有一些丰富的数据,用于观察不同日期和地点的昆虫。 And I want to spread the data frame, so that i get one row for each individual insect, that was observed.
我想传播数据框,这样我就可以得到每一只昆虫的一行。
set.seed(1234)
df <- expand.grid(factor = c("A", "B"),
date = seq(as.Date("2019-05-04"), as.Date("2019-05-08"),"day"))
df$Abundance <- sample(seq(3,10,1), nrow(df), replace = T)
What I have is: 我有的是:
factor date Abundance
1 A 2019-05-04 3
2 B 2019-05-04 7
3 A 2019-05-05 7
4 B 2019-05-05 7
5 A 2019-05-06 9
6 B 2019-05-06 8
7 A 2019-05-07 3
8 B 2019-05-07 4
9 A 2019-05-08 8
10 B 2019-05-08 7
And now I want to transform the data frame, that it looks like that: 现在我想转换数据框,看起来像这样:
factor date Abundance
1 A 2019-05-04 1
2 A 2019-05-04 1
3 A 2019-05-04 1
4 B 2019-05-04 1
5 B 2019-05-04 1
6 B 2019-05-04 1
7 B 2019-05-04 1
8 B 2019-05-04 1
9 B 2019-05-04 1
10 B 2019-05-04 1
...
Does anybody know how to do that with dplyr? 有人知道怎么用dplyr做到这一点?
Thanks for your help! 谢谢你的帮助!
We can use uncount
from tidyr
我们可以使用
uncount
的tidyr
library(tidyverse)
uncount(df, Abundance) %>%
mutate(Abundance = 1)
You could use rep
and slice
, where we repeat every row Abundance
number of times. 您可以使用
rep
和slice
,其中我们重复每一行Abundance
次数。
library(dplyr)
df %>%
slice(rep(1:n(), Abundance)) %>%
mutate(Abundance = 1)
# factor date Abundance
#1 A 2019-05-04 1
#2 A 2019-05-04 1
#3 A 2019-05-04 1
#4 B 2019-05-04 1
#5 B 2019-05-04 1
#6 B 2019-05-04 1
#7 B 2019-05-04 1
#8 B 2019-05-04 1
#9 B 2019-05-04 1
#10 B 2019-05-04 1
#....
Same using base R would be 使用基数R也是如此
transform(df[rep(1:nrow(df), df$Abundance), ], Abundance = 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.