如何创建成对的Bray-Curtis比较数据框?

时间:2019-04-11 15:57:13

标签: r loops distance vegan

我有一个大型的植被数据集,并希望使用vegdist包中的函数vegan计算Bray-Curtis距离。

我有一个植被表(deltaveg),上面有370个配对的位置(永久象限)。 185,2001年和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")

1 个答案:

答案 0 :(得分:0)

split.data.frame 将您的data.frame deltaveg 分成 Site的子集data.frames列表列值。您可以使用 vegdist 将这些子集data.frames分别用作lapply函数的输入。

尝试一下:

lapply(X = split.data.frame(x = deltaveg,f = deltaveg$Site),function(t){vegdist(t[,4:6])})