将数据转换为正确的INEXT分析格式

时间:2016-12-20 11:33:02

标签: r list matrix 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库中提供的ciliates示例数据相同的格式?这是作为矩阵列表给出的。

3 个答案:

答案 0 :(得分:2)

如果您有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”的池塘A和池塘B,您可以执行以下操作:

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)

答案 1 :(得分:2)

我尝试了Oliver Burdekin的答案,效果很好 - 谢谢

使用丰度数据时要做的一个补充 - 在运行iNEXT函数之前将 as.abucount 函数应用于矩阵列表

[1] 08 02 2017 13:03:20.768:INFO [karma]: Karma v1.4.1 server started
... 
[1] 08 02 2017 13:03:22.579:WARN [web-server]: 404: /app.component.html
    Chrome 56.0.2924 (Linux 0.0.0) AppComponent should create component FAILED
[1]     Error: This test module uses the component AppComponent which is using a "templateUrl" or "styleUrls", but they were never compiled. Please call "TestBed.compileComponents" before your test.
[1]     Expected undefined to be defined.

答案 2 :(得分: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