【问题标题】:Add new (shorter) column to tibble and expand tibble to remain tidy将新的(较短的)列添加到 tibble 并扩展 tibble 以保持整洁
【发布时间】:2018-09-13 04:10:58
【问题描述】:

我有一个数据框,我需要添加一列以包含对应于现有数据框每一行的 3 个物种。希望下面的例子能说明问题:

Site    Year    Trt
A       2016    bowl
A       2016    vane
A       2017    target
A       2017    stick
B       2016    bowl
B       2016    vane
B       2017    target
B       2017    stick

species<-c("species1", "species2", "species3")

那我想去

Site    Year    Trt     Species
A       2016    bowl    species1
A       2016    vane    species1
A       2017    target  species1
A       2017    stick   species1
A       2016    bowl    species2
A       2016    vane    species2
A       2017    target  species2
A       2017    stick   species2
A       2016    bowl    species3
A       2016    vane    species3
A       2017    target  species3
A       2017    stick   species3
B       2016    bowl    species1
B       2016    vane    species1
B       2017    target  species1
B       2017    stick   species1
B       2016    bowl    species2
B       2016    vane    species2
B       2017    target  species2
B       2017    stick   species2
B       2016    bowl    species3
B       2016    vane    species3
B       2017    target  species3
B       2017    stick   species3

我想一些rep 方法可以,但真正的数据集有两个额外的列和141 个物种。我对所有匹配正确的事情都感到偏执。

【问题讨论】:

  • tibble 应该如何知道重复物种值的次数?它应该在每个“碗”值处重置吗?目前尚不清楚您希望它如何工作。
  • @MrFlick 似乎在每个站点的bowl, vane, target, stick 电池之后,会生成一个新物种。也许最简单的事情是按站点拆分数据并应用每个出现 4 个条目的物种的逻辑。但我同意,一些进一步的澄清、尝试和可重复的例子将是非常有益的。

标签: r dataframe dplyr tidyr


【解决方案1】:

利用rep 方法的基本 R 解决方案:

transform(
    do.call('rbind', rep(list(df), length(species))), 
    Species = rep(species, each=nrow(df))
)

#   Site Year    Trt  Species
#1     A 2016   bowl species1
#2     A 2016   vane species1
#3     A 2017 target species1
#4     A 2017  stick species1
#5     B 2016   bowl species1
#6     B 2016   vane species1
#7     B 2017 target species1
#8     B 2017  stick species1
#9     A 2016   bowl species2
#10    A 2016   vane species2
#11    A 2017 target species2
#12    A 2017  stick species2
#13    B 2016   bowl species2
#14    B 2016   vane species2
#15    B 2017 target species2
#16    B 2017  stick species2
#17    A 2016   bowl species3
#18    A 2016   vane species3
#19    A 2017 target species3
#20    A 2017  stick species3
#21    B 2016   bowl species3
#22    B 2016   vane species3
#23    B 2017 target species3
#24    B 2017  stick species3

【讨论】:

    【解决方案2】:

    如何使用tidyr::expand 将数据扩展为:

    library(tidyverse)
    expand(data, nesting(Site, Year, Trt), species) %>% as.data.frame()
    
    #     Site Year    Trt  species
    # 1     A 2016   bowl species1
    # 2     A 2016   bowl species2
    # 3     A 2016   bowl species3
    # 4     A 2016   vane species1
    # 5     A 2016   vane species2
    # 6     A 2016   vane species3
    # 7     A 2017  stick species1
    # 8     A 2017  stick species2
    # 9     A 2017  stick species3
    # 10    A 2017 target species1
    # 11    A 2017 target species2
    # 12    A 2017 target species3
    # 13    B 2016   bowl species1
    # 14    B 2016   bowl species2
    # 15    B 2016   bowl species3
    # 16    B 2016   vane species1
    # 17    B 2016   vane species2
    # 18    B 2016   vane species3
    # 19    B 2017  stick species1
    # 20    B 2017  stick species2
    # 21    B 2017  stick species3
    # 22    B 2017 target species1
    # 23    B 2017 target species2
    # 24    B 2017 target species3
    

    数据

    data <- read.table(text = 
    "Site    Year    Trt
    A       2016    bowl
    A       2016    vane
    A       2017    target
    A       2017    stick
    B       2016    bowl
    B       2016    vane
    B       2017    target
    B       2017    stick",
    header = TRUE, stringsAsFactors = FALSE)
    
    species<-c("species1", "species2", "species3")
    

    【讨论】:

      【解决方案3】:

      另一个base-R 解决方案是这样的(您可以稍后sort 基于Site 列):

      cbind(dat, Species = rep(species, each = nrow(dat)))
      
      #    Site Year    Trt  Species
      # 1     A 2016   bowl species1
      # 2     A 2016   vane species1
      # 3     A 2017 target species1
      # 4     A 2017  stick species1
      # 5     B 2016   bowl species1
      # 6     B 2016   vane species1
      # 7     B 2017 target species1
      # 8     B 2017  stick species1
      # 9     A 2016   bowl species2
      # 10    A 2016   vane species2
      # 11    A 2017 target species2
      # 12    A 2017  stick species2
      # 13    B 2016   bowl species2
      # 14    B 2016   vane species2
      # 15    B 2017 target species2
      # 16    B 2017  stick species2
      # 17    A 2016   bowl species3
      # 18    A 2016   vane species3
      # 19    A 2017 target species3
      # 20    A 2017  stick species3
      # 21    B 2016   bowl species3
      # 22    B 2016   vane species3
      # 23    B 2017 target species3
      # 24    B 2017  stick species3
      

      【讨论】:

        猜你喜欢
        • 2020-02-14
        • 1970-01-01
        • 1970-01-01
        • 2021-09-29
        • 2020-08-13
        • 1970-01-01
        • 2019-12-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多