如何创建具有成对 Bray-Curtis 比较的数据框?
Posted
技术标签:
【中文标题】如何创建具有成对 Bray-Curtis 比较的数据框?【英文标题】:How to create a data frame with pairwise Bray-Curtis comparisons? 【发布时间】:2019-04-11 15:57:12 【问题描述】:我有一个大型植被数据集,想使用 vegan
包中的函数 vegdist
计算 Bray-Curtis 距离。
我有一个植被表 (deltaveg),其中包含 370 个配对地点(永久象限)。 2001年记录185次,2018年再次记录。如果使用该功能
bray<-vegdist(deltaveg)
bray<-as.matrix (bray)
这给了我一个 370*370 比较的数据矩阵。我只想要一个矩阵来比较具有相同代码的站点(即站点 A1 与 A1,站点 B3 与 B3 等)并最终得到一个可行的表。
这里是一些示例数据:
Year<-c(2001, 2018, 2001, 2018, 2001, 2018)
Site<-c("A","A", "B","B", "C", "C")
Code<-c("A1", "A1", "B1", "B1", "C1", "C1")
spec1<-c(5, 5, 12.5, 75, 75, 50)
spec2<-c(0, 12.5, 0, 0, 0, 0)
spec3<-c(50,50,50,50,75,50)
deltaveg<-data.frame(Year, Site, Code, spec1, spec2, spec3)
BC<-vegdist(deltaveg[,4:6]
是否可以使用计算出的距离矩阵并仅从中提取成对比较?例如
Site BC-index
A x
B x
C x
我已经为此苦苦挣扎了几天,希望有人可以帮助我。 提前致谢。
头部输出:
structure(list(Agrimonia.eupatoria = c(6L, 0L), Agrostis.capillaris = c(6L,
0L), Agrostis.gigantea = c(6L, 0L), Agrostis.stolonifera = c(6L,
0L), Aira.praecox = c(6L, 3L), Alisma.gramineum = c(0L, 0L),
Ammophila.arenaria = c(6L, 0L), Anthyllis.vulneraria = c(6L,
0L), Apium.nodiflorum = c(6L, 0L), Arabis.hirsuta.s..hirsuta = c(0L,
0L), Arenaria.serpyllifolia = c(0L, 0L), Barbarea.vulgaris = c(6L,
0L), Betula.pendula = c(0L, 0L), Betula.pubescens = c(6L,
0L), Bolboschoenus.maritimus = c(0L, 0L), Brachythecium.albicans = c(9L,
2L), Brachythecium.rutabulum = c(9L, 0L), Bromus.hordeaceus.s..hordeaceus = c(6L,
0L), Bryum.pseudotriquetrum = c(9L, 0L), Bryum.spec. = c(9L,
0L), Calamagrostis.epigejos = c(6L, 18L), Calliergonella.cuspidata = c(9L,
0L), Campylium.stellatum = c(9L, 0L), Cardamine.hirsuta = c(6L,
0L), Cardamine.pratensis = c(6L, 0L), Carex.arenaria = c(6L,
4L), Carex.flacca = c(6L, 0L), Carex.nigra = c(6L, 2L), Carex.nigra.x.trinervis = c(6L,
1L), Carex.oederi.s..oederi = c(6L, 0L), Carex.pseudocyperus = c(0L,
0L), Carex.trinervis = c(6L, 0L), Carlina.vulgaris = c(6L,
0L), Centaurium.littorale = c(6L, 0L), Centunculus.minimus = c(6L,
0L), Cerastium.arvense = c(6L, 0L), Cerastium.fontanum.s..vulgare = c(6L,
0L), Cerastium.semidecandrum = c(0L, 0L), Ceratodon.purpureus = c(9L,
2L), Cetraria.aculeata = c(0L, 0L), Chara.vulgaris = c(6L,
0L), Chenopodium.foliosum = c(0L, 0L), Chenopodium.rubrum = c(0L,
0L), Cirsium.arvense = c(6L, 0L), Cirsium.vulgare = c(6L,
0L), Cladonia.foliacea = c(0L, 0L), Cladonia.furcata = c(9L,
0L), Cladonia.gracilis = c(9L, 0L), Cladonia.portentosa = c(0L,
0L), Cladonia.ramulosa = c(0L, 0L), Cladonia.spec. = c(9L,
0L), Cochlearia.danica = c(0L, 0L), Conyza.canadensis = c(6L,
0L), Corynephorus.canescens = c(6L, 3L), Crataegus.monogyna = c(6L,
18L), Crepis.capillaris = c(0L, 0L), Cynoglossum.officinale = c(6L,
1L), Dactylorhiza.incarnata = c(6L, 0L), Dactylorhiza.majalis.s..praetermissa = c(6L,
0L), Dactylorhiza.majalis.s.l. = c(6L, 0L), Danthonia.decumbens = c(6L,
0L), Dicranum.scoparium = c(9L, 0L), Dipsacus.fullonum = c(6L,
0L), Drepanocladus.aduncus = c(9L, 0L), Drepanocladus.polygamus = c(9L,
0L), Echium.vulgare = c(6L, 0L), Eleocharis.palustris.s.str. = c(6L,
0L), Elymus.spec. = c(6L, 0L), Elytrigia.atherica = c(6L,
0L), Elytrigia.repens = c(6L, 8L), Epilobium.hirsutum = c(0L,
0L), Epilobium.montanum = c(6L, 0L), Epilobium.parviflorum = c(0L,
0L), Epilobium.spec. = c(6L, 0L), Epipactis.helleborine = c(6L,
0L), Epipactis.palustris = c(6L, 0L), Equisetum.arvense = c(6L,
1L), Equisetum.fluviatile = c(6L, 0L), Equisetum.palustre = c(0L,
0L), Erodium.cicutarium.s.l. = c(6L, 2L), Erophila.verna = c(0L,
0L), Euonymus.europaeus = c(6L, 0L), Eupatorium.cannabinum = c(6L,
0L), Euphrasia.officinalis.s.l. = c(6L, 0L), Fallopia.convolvulus = c(6L,
0L), Festuca.arenaria = c(6L, 0L), Festuca.arundinacea = c(0L,
0L), Festuca.filiformis = c(6L, 0L), Festuca.rubra = c(6L,
0L), Galium.mollugo = c(6L, 2L), Galium.palustre.s.l. = c(0L,
0L), Galium.uliginosum = c(6L, 0L), Galium.verum.s.l. = c(6L,
8L), Gentiana.cruciata = c(6L, 0L), Gentianella.amarella = c(6L,
0L), Geranium.molle = c(6L, 0L), Geum.urbanum = c(6L, 0L),
Gnaphalium.luteo.album = c(6L, 0L), Helictotrichon.pubescens = c(0L,
0L), Hieracium.pilosella = c(6L, 0L), Hieracium.umbellatum.s.l. = c(0L,
0L), Hippophae.rhamnoides = c(6L, 0L), Hippuris.vulgaris = c(0L,
0L), Holcus.lanatus = c(6L, 0L), Hydrocotyle.vulgaris = c(6L,
0L), Hypericum.perforatum = c(6L, 0L), Hypericum.spec. = c(6L,
0L), Hypnum.cupressiforme.s.l. = 9:8, Hypochaeris.radicata = c(6L,
2L), Iris.pseudacorus = c(6L, 0L), Jacobaea.vulgaris.s.l. = c(6L,
0L), Juncus.subnodulosus = c(6L, 0L), Juncus.anceps = c(6L,
0L), Juncus.articulatus = c(6L, 0L), Juncus.balticus = c(0L,
0L), Juncus.bufonius = c(0L, 0L), Koeleria.macrantha = c(6L,
1L), Lathyrus.pratensis = c(6L, 0L), Leontodon.saxatilis = c(6L,
0L), Ligustrum.vulgare = c(0L, 0L), Linaria.vulgaris = c(6L,
0L), Linum.catharticum = c(6L, 0L), Lophocolea.bidentata = c(9L,
0L), Lotus.corniculatus.s.str. = c(6L, 0L), Lotus.pedunculatus = c(6L,
0L), Luzula.campestris = c(6L, 0L), Lycopus.europaeus = c(6L,
0L), Lysimachia.nummularia = c(6L, 0L), Lythrum.salicaria = c(6L,
0L), Mentha.aquatica = c(6L, 0L), Myosotis.laxa.s..cespitosa = c(6L,
0L), Myosotis.ramosissima = c(0L, 0L), Myriophyllum.spicatum = c(6L,
0L), Nasturtium.spec. = c(6L, 0L), Oenothera.spec. = c(6L,
0L), Ononis.repens.s..repens = c(6L, 1L), Parnassia.palustris = c(6L,
0L), Pellia.endiviifolia = c(9L, 0L), Peltigera.spec. = c(0L,
0L), Phleum.arenarium = c(6L, 2L), Phleum.pratense.s..pratense = c(0L,
0L), Phragmites.australis = c(6L, 2L), Picris.hieracioides = c(6L,
0L), Plantago.lanceolata = c(6L, 0L), Plantago.major.s.l. = c(6L,
0L), Pleurozium.schreberi = c(9L, 0L), Poa.annua = c(6L,
0L), Poa.pratensis = c(6L, 0L), Poa.trivialis = c(0L, 0L),
Polygala.vulgaris = c(6L, 0L), Polygonatum.odoratum = c(6L,
2L), Populus.spec. = c(6L, 0L), Populus.tremula = c(6L, 0L
), Potentilla.reptans = c(0L, 0L), Potentilla.spec. = c(6L,
0L), Prunella.vulgaris = c(6L, 0L), Pseudoscleropodium.purum = c(9L,
0L), Pulicaria.dysenterica = c(6L, 0L), Ranunculus.acris = c(6L,
0L), Ranunculus.aquatilis.s.l. = c(0L, 0L), Ranunculus.flammula = c(6L,
0L), Ranunculus.repens = c(6L, 0L), Ranunculus.sceleratus = c(0L,
0L), Rhinanthus.angustifolius = c(6L, 0L), Rosa.canina.s.l. = c(6L,
0L), Rosa.rubiginosa.s.l. = c(0L, 0L), Rosa.spec. = c(4L,
0L), Rubus.caesius = c(6L, 8L), Rumex.acetosella = c(0L,
0L), Rumex.conglomeratus = c(0L, 0L), Rumex.crispus = c(6L,
0L), Rumex.maritimus = c(0L, 0L), Rumex.spec. = c(6L, 0L),
Sagina.nodosa = c(6L, 0L), Sagina.procumbens = c(0L, 0L),
Salix.alba = c(0L, 0L), Salix.aurita.cinerea = c(4L, 0L),
Salix.repens = c(6L, 0L), Salix.spec. = c(4L, 0L), Samolus.valerandi = c(6L,
0L), Schoenoplectus.lacustris.s.str. = c(0L, 0L), Sedum.acre = c(0L,
0L), Senecio.inaequidens = c(6L, 2L), Senecio.sylvaticus = c(0L,
0L), Silene.nutans = c(0L, 0L), Solanum.dulcamara = c(0L,
0L), Sonchus.arvensis.s.l. = c(6L, 0L), Spirodela.polyrhiza = c(6L,
0L), Stellaria.pallida = c(0L, 0L), Syntrichia.ruralis.v..arenicola = c(9L,
18L), Taraxacum.sect..Erythrosperma = c(6L, 0L), Taraxacum.spec. = c(6L,
0L), Thymus.pulegioides = c(6L, 0L), Trifolium.pratense = c(6L,
0L), Trifolium.repens = c(6L, 0L), Tussilago.farfara = c(6L,
0L), Valeriana.officinalis = c(6L, 0L), Verbascum.spec. = c(6L,
0L), Verbena.officinalis = c(6L, 0L), Veronica.anagallis.aquatica = c(0L,
0L), Veronica.catenata = c(6L, 0L), Veronica.chamaedrys = c(0L,
0L), Veronica.officinalis = c(6L, 0L), Viola.curtisii = c(6L,
0L), Viola.hirta = c(6L, 0L), Viola.riviniana = c(0L, 0L),
Viola.rupestris = c(6L, 0L), Viola.spec. = c(6L, 0L), Year = c(2018L,
2018L), Block = structure(c(10L, 10L), .Label = c("", "A",
"B", "C", "D", "E", "F", "G", "H", "I", "J", "X"), class = "factor"),
Plot = 1:2, Site = structure(c(188L, 199L), .Label = c("",
"A1", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17",
"A18", "A19", "A2", "A20", "A3", "A4", "A5", "A6", "A7",
"A8", "A9", "B1", "B10", "B11", "B12", "B13", "B14", "B15",
"B16", "B17", "B18", "B19", "B2", "B20", "B3", "B4", "B5",
"B6", "B7", "B8", "B9", "C1", "C10", "C11", "C12", "C13",
"C14", "C15", "C16", "C17", "C18", "C19", "C2", "C20", "C3",
"C4", "C5", "C6", "C7", "C8", "C9", "D1", "D10", "D11", "D12",
"D13", "D14", "D15", "D16", "D17", "D18", "D19", "D2", "D20",
"D3", "D4", "D5", "D6", "D7", "D8", "D9", "E1", "E10", "E11",
"E12", "E13", "E14", "E15", "E16", "E17", "E18", "E19", "E2",
"E20", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "F1", "F10",
"F11", "F12", "F13", "F14", "F15", "F16", "F17", "F18", "F19",
"F2", "F20", "F21", "F22", "F23", "F24", "F25", "F26", "F27",
"F28", "F29", "F3", "F30", "F31", "F32", "F33", "F34", "F35",
"F36", "F37", "F38", "F39", "F4", "F40", "F41", "F42", "F43",
"F44", "F45", "F46", "F5", "F6", "F7", "F8", "F9", "G1",
"G10", "G11", "G12", "G13", "G14", "G15", "G16", "G17", "G18",
"G19", "G2", "G20", "G3", "G4", "G5", "G6", "G7", "G8", "G9",
"H1", "H10", "H11", "H12", "H13", "H14", "H15", "H16", "H17",
"H18", "H19", "H2", "H20", "H3", "H4", "H5", "H6", "H7",
"H8", "H9", "I1", "I10", "I11", "I12", "I13", "I14", "I15",
"I16", "I17", "I18", "I19", "I2", "I20", "I3", "I4", "I5",
"I6", "I7", "I8", "I9", "J1", "J10", "J11", "J12", "J13",
"J14", "J15", "J16", "J17", "J18", "J19", "J2", "J20", "J3",
"J4", "J5", "J6", "J7", "J8", "J9", "X1", "X2"), class = "factor"),
Grouping_Code = structure(c(25L, 25L), .Label = c("", "2001_A",
"2001_B", "2001_C", "2001_D", "2001_E", "2001_F", "2001_G",
"2001_H", "2001_I", "2001_J", "2001_X", "2008_A", "2008_C",
"2008_D", "2008_E", "2008_I", "2008_J", "2018_A", "2018_B",
"2018_C", "2018_D", "2018_E", "2018_F", "2018_I", "2018_J"
), class = "factor"), S_OM. = c(NA, 8.131875894), S_Moist. = c(NA,
37.50593169), pH_H2O = c(NA, 7.24), Av_NO3..mg.l. = c(NA,
0), Av_NH4.mg.l. = c(NA, 0), Av_Po4.mg.l. = c(NA, 0), Av_K.mg.l. = c(NA,
4.866666667), Av_Na.mg.l. = c(NA, 68.63888889), Ntot = c(NA,
124.8092235), Ptot = c(NA, 3.666926474), DCA1 = c(0.499175791,
0.770171329), DCA2 = c(-0.832369034, -1.930965722)), row.names = c("18I01",
"18I02"), class = "data.frame")
【问题讨论】:
我想距离只在最后 3 列计算? 是的。仅适用于物种数据(参见最后一段代码)。 它对你有用吗? 我会在接下来的一个小时左右尝试一下。我会提前通知您并感谢您! 【参考方案1】:split.data.frame
会将您的 data.frame deltaveg
拆分为由 Site
列值组成的子集 data.frames 列表.您可以使用 lapply
将这些子集 data.frame 中的每一个单独用作vegdist
函数的输入。
试试这个:
lapply(X = split.data.frame(x = deltaveg,f = deltaveg$Site),function(t)vegdist(t[,4:6]))
【讨论】:
不幸的是我不能让它工作。在我在这里发布的小例子中,您的代码运行良好。在更大的数据集上它没有,我还不确定为什么。当我运行代码时: BC1 80) : 需要 TRUE/FALSE 的缺失值此外,没有逻辑比较制作。 @TomvanHeusden 你能在你的 data.frame 中只分享 2 行数据吗?为此,请分享以下输出:dput(head(Dataveg.bray,2))
您确定所有列 (1:208) 都是数字或采用vegdist
函数所期望的格式吗?
@TomvanHeusden 您是否检查了数据中的空值或 NA?
@ tushaR,我在最初的帖子中添加了 dput。当我检查 NA 的 (sum(is.na(Dataveg.bray[,1:208]) 时,我得到 '0' 作为返回值。数据中有很多零值,但这对于vegdist 功能。另外,非常感谢您投入的时间。非常感谢。
我明白了。我无法在最后重现错误。您是否可以通过某种方式共享整个数据或其中的大部分数据。以上是关于如何创建具有成对 Bray-Curtis 比较的数据框?的主要内容,如果未能解决你的问题,请参考以下文章