2016-12-20 79 views
0

我计划使用iNEXT函数(来自iNEXT包)分析两种池塘类型之间的潜在差异。这些数据是存在/不存在的,并且按照物种基质在某个位置上。将数据转换为INEXT分析的正确格式

Pond.type <- c("A", "A", "A", "B", "B", "B") 
Sample.no <- c(1,2,3,1,2,3) 
Species1 <- c(0,1,1,1,0,0) 
Species2 <- c(0,1,1,0,1,0) 
Species3 <- c(1,1,1,1,0,1) 
Species4 <- c(0,1,0,1,0,0) 
Species5 <- c(1,1,1,0,0,0) 

mydata <- cbind.data.frame(Pond.type, Sample.no, Species1, Species2, Species3, Species4, Species5) 

如果我将我的数据框划分成池类型并将它们保存为矩阵,我可以运行该函数,例如, iNEXT(pond.type.a.dataframe),但后来我无法比较一个地块上不同的池塘类型。

我的问题是,有没有办法将我的数据转换为与iNEXT库中提供的纤毛虫示例数据相同的格式?这是作为矩阵列表给出的。

+0

你能把它们分成两个矩阵吗?一个用于A型池塘(样品No.1,2,3)和一个用于池塘B型(样品No.1,2,3)。然后你可以比较类型A和B. –

回答

0

如果你有2个.csvs结构这样

池A型:

Species, Sample1, Sample2, Sample3 
Species1, 0,1,1 
Species2, 0,1,1 
Species3, 1,1,1 
Species4, 0,1,0 
Species5, 1,1,1 

池塘B型:

Species, Sample1, Sample2, Sample3 
Species1, 1,0,0 
Species2, 0,1,0 
Species3, 1,0,1 
Species4, 1,0,0 
Species5, 0,0,0 

然后假设你叫 “读CSV” S庞达和pondB你可以做到以下几点:

library(iNEXT) 
library(ggplot2) 

# make a matrix from pondA as type "integer" 
mPondA <- as.matrix(apply(pondA[,-1],2,as.integer)) 

# use your species names as row names 
row.names(mPondA) <- pondA[,1] 

# do the same for pondB 
mPondB <- as.matrix(apply(pondB[,-1],2,as.integer)) 
row.names(mPondB) <- pondB[,1] 

# create a list of your matrices (named so the output looks nice) 
pondABM = list(A = mPondA, B = mPondB) 

# have a look at the raw data 
out.raw <- iNEXT(pondABM, datatype="incidence_raw", endpoint=20) 
ggiNEXT(out.raw) 

,这将给你一个像这样的输出:

raw output from OP sample data

如果你想要做更多使用这种输入型的数据,不要忘了更改数据类型:

# note that datatype has been changed to "incidence_raw" instead of "abundance" 
iNEXT(pondABM, q=0, datatype="incidence_raw", size=NULL, se=TRUE, conf=0.95, nboot=50) 
+0

我来到了一个非常类似的解决方案,我将发布,谢谢! – AlanL

0

我来到了一个解决方案,可能比它需要的更啰嗦,但它的工作原理。

# I've altered the dataset as I usually type these data in long format 
Pond.type <- c(rep("A", 15), rep("B", 15)) 
Site.no <- rep(seq(1,6, by=1), 5) 
Species <- c(rep("Spp1", 6), rep("Spp2", 6), rep("Spp3", 6), rep("Spp4", 6), rep("Spp5", 6)) 
Presence <- rep(1, 30) 

# join the vectors together into a dataframe 
mydata.long <- cbind.data.frame(Pond.type, Site.no, Species, Presence) 

# I then cast the data into a species x site matrix (dcast is from the reshape2 library) 
mydata.cast <- dcast(mydata.long, Species + Pond.type ~ Site.no) 

# Changes the NAs to zeros. 
mydata.cast[is.na(mydata.cast)] <- 0 

# Separate the dataframe into each pond tyoe 
pondA <- mydata.cast[grep("A", mydata.cast$Pond.type),] 
pondB <- mydata.cast[grep("B", mydata.cast$Pond.type),] 

# Calculate the frequency counts using the function from the iNEXT library 
pondA.freq <- as.incfreq(pondA[,3:ncol(pondA)]) 
pondB.freq <- as.incfreq(pondB[,3:ncol(pondB)]) 

# join them as a list 
pond.list = list(A = pondA.freq, B = pondB.freq) 

# ready for comparison 
pond.out <- iNEXT(pond.list, datatype = "incidence_freq", q=0) 
pond.out 
0

我试图从奥利弗伯德其中大工作的答案 - 感谢

任一方式作出是否使用大量的数据 - 应用as.abucount函数矩阵列表,运行iNEXT前功能

# create a list of your matrices (named so the output looks nice) 
pondABM = list(A = mPondA, B = mPondB) 

# apply as.abucount to the list of matrices 
pondABM2 = lapply(pondABM, as.abucount) 

# run the iNEXT function 
iNEXT(pondABM2, datatype="abundance") 
相关问题