Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将数据转换为不精确分析的正确格式_R_List_Matrix_Inext - Fatal编程技术网

R 将数据转换为不精确分析的正确格式

R 将数据转换为不精确分析的正确格式,r,list,matrix,inext,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

我计划使用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)

Pond.type如果您有这样的2.csv结构

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”的pondA和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)

如果你有这样的2.csv结构

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”的pondA和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)

我找到了一个解决方案,可能比它需要的更冗长,但它是有效的

# 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
#我更改了数据集,因为我通常以长格式键入这些数据

Pond.type我找到了一个解决方案,可能比它需要的更冗长,但它是有效的

# 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
#我更改了数据集,因为我通常以长格式键入这些数据

Pond.type我试过Oliver Burdekin的答案,效果很好-谢谢

如果使用大量数据,则需要添加一个函数-在运行iNEXT函数之前,将as.abucount函数应用于矩阵列表

# 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")

我尝试了奥利弗·伯德金的答案,效果很好——谢谢

如果使用大量数据,则需要添加一个函数-在运行iNEXT函数之前,将as.abucount函数应用于矩阵列表

# 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")

你能把它们分成两个矩阵吗?一个用于A型池塘(sampleNo 1、2、3),一个用于B型池塘(sampleNo 1、2、3)。然后你可以比较A型和B型。你能把它们分成两个矩阵吗?一个用于A型池塘(sampleNo 1、2、3),一个用于B型池塘(sampleNo 1、2、3)。然后你就可以比较A型和B型了。我找到了一个非常相似的解决方案,我会发布,谢谢!我找到了一个非常类似的解决方案,我会发布,谢谢!