r - 将新的(较短的)列添加到 tibble 并扩展 tibble 以保持整洁

标签 r dataframe dplyr tidyr

我有一个数据框,我需要添加一列以包含对应于现有数据框每一行的 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 个物种。我对所有匹配正确的事情都感到偏执。

最佳答案

如何使用 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")

关于r - 将新的(较短的)列添加到 tibble 并扩展 tibble 以保持整洁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49636755/

相关文章:

r - 如何获取在 R 中具有通用 S4 父类(super class)的类列表

python - Pandas DataFrame 根据条件删除 NaN 行?

python - 根据列名称中的子字符串重新排列数据框列

r - 将唯一 ID 分配给 dplyr 中分组变量中的连续行

R - 配对数据中的样本

python - 从 r 过渡到 Python - pandas 中类似 dplyr 的操作

r - knitr 与 xelatex 和 tikz : Ghostscript error on minimal example

r - 合并 data.tables 使用超过 10 GB RAM

r - "#"不能用作read.table 的字段分隔符?

python - CSV 数据(时间戳和事件)的时间序列图 : x-label constant