您应该首先考虑为什么要合并文件。据我所知,您的文件最好分开保存,因为在您的第一个文件中,您记录了两个阶段的通用指标(标题相同),而在第二个文件中,您记录了两个阶段的不同指标(标题是不同的)。
由于第二个文件包含两个阶段的不同标题,因此无法将其转换为与第一个文件类似的形式。但是,可以将第一个文件转换为第二个文件的格式,从而允许您合并这两个文件。但是我强烈警告不要这样做,因为它可能会阻止您以这种方式快速分析您的数据。
library(ggplot2)
dat <- read.csv("file1.csv")
# This plots a boxplot comparing the evenness of the 2 phases
ggplot(dat, aes(x = as.factor(period), y = evenness)) + geom_boxplot()
但是,如果您坚持,这里是将 file1 重新格式化为每个条目的一行以与 file2 组合的代码
# One more warning, depending on how you
# want to eventually wrangle your data,
# doing this might make your life more difficult
library(dplyr)
f1 <- read.csv("file1.csv", stringAsFactors = FALSE)
dat1 <- dat[f1$phase == "incubating",]
dat2 <- dat[f1$phase == "chickrearing",]
dat2$phase <- dat1$phase <- NULL
names(dat1) <- c("bird.year", paste0("incubating.", names(dat1)[2:length(names(dat1))]))
names(dat2) <- c("bird.year", paste0("chickrearing.", names(dat2)[2:length(names(dat2))]))
f1.combined <- merge(dat1, dat2, by = "bird.year")
f2 <- read.csv("file2.csv")
f2 <- mutate(f2, bird.year = paste(Individual, year))
combined.files <- merge(f1.combined, f2, by = "bird.year")