2016-12-20 59 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") 
相關問題