一种更简单的方法来操纵我的数据?

时间:2018-03-15 21:42:17

标签: dataframe dplyr reshape2

我有一个物种x位点矩阵,每个采样点附有处理类型和月份。我设法使用一堆循环和额外的代码行以我想要的方式重构我的数据,但我很好奇我如何能够以更有效的方式对其进行编码,以便在新数据进入时起作用

数据:

time<-structure(list(Month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("February", "March"), class = "factor"), Treatment = structure(c(2L, 
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 2L, 1L, 1L), .Label = c("C", "T"), class = "factor"), 
    Site = structure(c(13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
    8L, 9L, 10L, 11L, 12L), .Label = c("SHA10RA", "SHA10RB", 
    "SHA10RC", "SHA10RD", "SHA10RE", "SHA10RF", "SHA11RA", "SHA11RB", 
    "SHA11RC", "SHA11RD", "SHA11RE", "SHA11RF", "SHA1RA", "SHA1RB", 
    "SHA1RC", "SHA4RA", "SHA5RA", "SHA5RB", "SHA5RC", "SHA5RD"
    ), class = "factor"), Pheidole.adrianoi = c(0L, 5L, 0L, 1L, 
    0L, 0L, 0L, 0L, 3L, 4L, 1L, 4L, 3L, 2L, 0L, 0L, 0L, 1L, 0L, 
    0L, 1L, 5L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 
    0L, 0L, 0L, 4L, 0L, 0L), Pheidole.floridana = c(1L, 1L, 6L, 
    1L, 7L, 6L, 0L, 0L, 0L, 0L, 1L, 0L, 3L, 1L, 4L, 3L, 3L, 5L, 
    5L, 0L, 0L, 0L, 2L, 1L, 6L, 5L, 1L, 3L, 0L, 0L, 3L, 0L, 2L, 
    1L, 4L, 5L, 5L, 0L, 7L, 0L), Pheidole.metallescens = c(5L, 
    0L, 4L, 2L, 0L, 0L, 0L, 0L, 2L, 2L, 2L, 1L, 0L, 4L, 0L, 0L, 
    0L, 1L, 0L, 0L, 3L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 2L, 1L, 
    0L, 2L, 3L, 0L, 0L, 0L, 0L, 0L, 0L), Solenopsis.carolinensis = c(0L, 
    6L, 2L, 3L, 5L, 2L, 0L, 5L, 2L, 5L, 5L, 7L, 5L, 4L, 8L, 4L, 
    4L, 6L, 4L, 7L, 2L, 0L, 0L, 2L, 2L, 3L, 0L, 4L, 0L, 3L, 4L, 
    0L, 2L, 2L, 4L, 0L, 1L, 5L, 1L, 7L), Solenopsis.invicta = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Solenopsis.nickersoni = c(0L, 
    0L, 0L, 0L, 0L, 4L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Solenopsis.pergandei = c(2L, 
    2L, 1L, 1L, 0L, 1L, 4L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
    1L, 0L, 0L, 3L, 0L, 0L, 3L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 1L, 4L, 3L, 0L, 0L, 1L, 3L), Solenopsis.tennesseensis = c(0L, 
    0L, 0L, 2L, 1L, 1L, 1L, 2L, 0L, 2L, 1L, 1L, 1L, 4L, 3L, 2L, 
    3L, 1L, 2L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 
    0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L), Solenopsis.tonsa = c(0L, 
    0L, 1L, 0L, 1L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("Month", 
"Treatment", "Site", "Pheidole.adrianoi", "Pheidole.floridana", 
"Pheidole.metallescens", "Solenopsis.carolinensis", "Solenopsis.invicta", 
"Solenopsis.nickersoni", "Solenopsis.pergandei", "Solenopsis.tennesseensis", 
"Solenopsis.tonsa"), class = "data.frame", row.names = c(NA, 
-40L))

代码:

Species<-colnames(time)[4:12]
Sites<-time$Site
Treatment<-time$Treatment
Treatment<-as.character(Treatment)
Month<-time$Month
Month<-as.character(Month)
Species<-rep(Species,40)
Sites<-as.character(Sites)

Site<-NULL
for (i in 1:length(Sites)) {
  Site1<-rep(Sites[i],9)
  Site<-append(Site,Site1)
}
Site
as.vector(Site)
length(Species)
length(Site)

Period<-NULL
for (i in 1:length(Month)) {
  Month1<-rep(Month[i],9)
  Period<-append(Period,Month1)
}
Period
Type<-NULL
for (i in 1:length(Treatment)) {
  Treat1<-rep(Treatment[i],9)
  Type<-append(Type,Treat1)
}
Type

Abundance<-NULL
for (i in 1:40) {
  Occurence<-t(time[i,4:12])
  Abundance<-append(Abundance,Occurence)
}
Abundance
length(Abundance)
class(Abundance)

df1<-data.frame(Period,Type,Site, Species, Abundance)
df1

我知道包reshape2可能对我有所帮助,但是我无法理解我是如何优化它以供我使用的。任何帮助都会很棒!

0 个答案:

没有答案