简体   繁体   English

如何将数据框从扩展到单个观察扩展

[英]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 我们可以使用uncounttidyr

library(tidyverse)
uncount(df, Abundance) %>%
       mutate(Abundance = 1) 

You could use rep and slice , where we repeat every row Abundance number of times. 您可以使用repslice ,其中我们重复每一行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.

相关问题 扩展数据框架并进行中间观察 - Expand data frame with intervening observations 如何从数据框中删除少于 5 个观察值的个体 - How to remove individuals with fewer than 5 observations from a data frame 如何从数据框中提取观察值并创建一个显示观察值,列名和行名的表? - How to extract observations from a data frame and make a table showing observations, column name, and row name? 根据条件从汇总的列值创建新数据框 - Create new data frame from summed column values based on conditions 如何按数据框中的列名重命名观察结果? - How to rename observations by column name in data frame? 如何在数据框上插入缺失的观测值 - how to insert missing observations on a data frame 如何从汇总表扩展数据框 - How can I expand data frame from aggregated table 如何在不丢失R中的NA值的情况下有条件地从数据帧中删除观测值? - How can I remove observations from a data frame conditionally without losing NA values in R? 如何编写从时间序列数据框中收集特定观察值列表的函数 - How to write a function that collects a specific list of observations from a time series data frame 如何使用dplyr过滤器从另一个数据框中选择满足两个条件的观测值? - How to use dplyr filter to select observations satisfying two conditions from another data frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM