Заранее извиняюсь, это будет долгая подготовка, чтобы ответить на вопрос. Не знаю, как спросить проще. Я пытаюсь построить 20 графиков nMDS в ggplot, используя эту структуру. https://chrischizinski.github.io/rstats/vegan-ggplot2/
Я могу сделать это очень легко для 1 фрейма данных. Но я пытаюсь использовать от lapply
до go с помощью этих операций на всех 20 из них.
Я нахожусь в этой части ссылки - я не редактировал следующий фрагмент, чтобы он соответствовал моему коду:
data.scores <- as.data.frame(scores(vare.mds)) #Using the scores function from vegan to extract the site scores and convert to a data.frame
data.scores$site <- rownames(data.scores) # create a column of site names, from the rownames of data.scores
data.scores$grp <- grp # add the grp variable created earlier
head(data.scores) #look at the data
Я хотел бы реструктурировать их так, чтобы эти действия применялись ко всем фреймам данных в моем списке.
Это пример данных:
##SETTING UP DATA ###
Z<- structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("AYUNGON", "BINDOY"), class = "factor"), reserve_status = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Inside", "Outside",
"TURF"), class = "factor"), year = c(2012L, 2012L, 2012L, 2012L,
2012L, 2014L, 2014L, 2014L, 2014L, 2014L, 2015L, 2015L, 2015L,
2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L,
2017L, 2017L), transect = structure(c(347L, 352L, 357L, 362L,
367L, 345L, 351L, 355L, 360L, 365L, 346L, 349L, 356L, 361L, 366L,
348L, 353L, 358L, 363L, 368L, 344L, 350L, 354L, 359L), .Label = c("ACML1A0317",
"ACML1A0515", "ACML1A0516", "ACML1B0317", "ACML1B0515", "ACML1B0516",
"ACML2A0317", "ACML2A0515", "ACML2A0516", "ACML2B0317", "ACML2B0515",
"ACML2B0516", "ACML3A0317", "ACML3A0515", "ACML3A0516", "ACML3B0317",
"ACML3B0515", "ACML3B0516", "ALTN1A0317", "ALTN1A0515", "ALTN1A0516",
"ALTN1B0317", "ALTN1B0515", "ALTN1B0516", "ALTN2A0317", "ALTN2A0515",
"ALTN2A0516", "ALTN2B0317", "ALTN2B0515", "ALTN2B0516", "ALTN3A0317",
"ALTN3A0515", "ALTN3A0516", "ALTN3B0317", "ALTN3B0515", "ALTN3B0516",
"ALTN4A0317", "ALTN4A0515", "ALTN4A0516", "ALTN5A0317", "ALTN5A0515",
"ALTN5A0516", "ALTN6A0317", "ALTN6A0515", "ALTN6A0516", "ALTN6B0317",
"ALTN6B0515", "ALTN6B0516", "BICU1F0616", "BICU1F1214", "BICU1R0616",
"BICU1R1214", "BICU2R0616", "BICU2R1214", "BICU3F0616", "BICU3F1214",
"BICU3R0616", "BICU3R1214", "BIHM1F0412", "BIHM1F0511", "BIHM1F0616",
"BIHM1F0813", "BIHM1F1214", "BIHM1R0412", "BIHM1R0511", "BIHM1R0616",
"BIHM1R0813", "BIHM2F0412", "BIHM2F0511", "BIHM2F0616", "BIHM2F0813",
"BIHM2F1214", "BIHM2R0412", "BIHM2R0511", "BIHM2R0616", "BIHM2R0813",
"BIHM2R1214", "BIHM3R0412", "BIHM3R0511", "BIHM3R0616", "BIHM3R0813",
"BIHM3R1214", "BIHM4F0412", "BIHM4F0511", "BIHM4F0616", "BIHM4F0813",
"BIHM4F1214", "BIHM4R0412", "BIHM4R0511", "BIHM4R0616", "BIHM4R0813",
"BIHM4R1214", "BIHM5F0412", "BIHM5F0511", "BIHM5F0616", "BIHM5F0813",
"BIHM5F1214", "BIHM5R0412", "BIHM5R0511", "BIHM5R0616", "BIHM5R0813",
"BIHM5R1214", "BIHM6R0412", "BIHM6R0511", "BIHM6R0616", "BIHM6R0813",
"BIHM6R1214", "CMER1A0216", "CMER1A0415", "CMER1A0617", "CMER2A0216",
"CMER2A0415", "CMER2A0617", "CMER2B0216", "CMER2B0415", "CMER2B0617",
"CMER3A0216", "CMER3A0415", "CMER3A0617", "CMER3B0216", "CMER3B0415",
"CMER3B0617", "CMER4A0216", "CMER4A0415", "CMER4A0617", "CMER4B0216",
"CMER4B0415", "CMER4B0617", "CMER5A0216", "CMER5A0415", "CMER5A0617",
"CMER6A0216", "CMER6A0415", "CMER6A0617", "CMER6B0216", "CMER6B0415",
"CMER6B0617", "CSAG1F0316", "CSAG1F0414", "CSAG1F0415", "CSAG1F0617",
"CSAG1F0812", "CSAG1R0316", "CSAG1R0414", "CSAG1R0415", "CSAG1R0812",
"CSAG2R0316", "CSAG2R0414", "CSAG2R0415", "CSAG2R0617", "CSAG2R0812",
"CSAG3F0316", "CSAG3F0414", "CSAG3F0415", "CSAG3F0617", "CSAG3F0812",
"CSAG3R0316", "CSAG3R0414", "CSAG3R0415", "CSAG3R0617", "CSAG3R0812",
"CSAG4R0316", "CSAG4R0414", "CSAG4R0415", "CSAG4R0812", "CSAG5F0316",
"CSAG5F0414", "CSAG5F0415", "CSAG5F0617", "CSAG5F0812", "CSAG5R0316",
"CSAG5R0414", "CSAG5R0415", "CSAG5R0812", "CSAG6F0316", "CSAG6F0414",
"CSAG6F0415", "CSAG6F0617", "CSAG6F0812", "CSAG6R0316", "CSAG6R0414",
"CSAG6R0415", "CSAG6R0617", "CSAG6R0812", "CTAG1A0615", "CTAG1A0617",
"CTAG1A0713", "CTAG1A0716", "CTAG1B0615", "CTAG1B0617", "CTAG1B0713",
"CTAG1B0716", "CTAG1F0511", "CTAG1F0512", "CTAG1R0511", "CTAG1R0512",
"CTAG2A0615", "CTAG2A0617", "CTAG2A0716", "CTAG3A0615", "CTAG3A0617",
"CTAG3A0716", "CTAG3B0615", "CTAG3B0617", "CTAG3B0716", "CTCL1A0615",
"CTCL1A0617", "CTCL1A0716", "CTCL1B0615", "CTCL1B0617", "CTCL1B0716",
"CTCL1F0511", "CTCL1F0512", "CTCL1F0713", "CTCL1R0511", "CTCL1R0512",
"CTCL1R0713", "CTCL2A0615", "CTCL2A0617", "CTCL2A0716", "CTCL2B0615",
"CTCL2B0617", "CTCL2B0716", "CTCL2F0511", "CTCL2F0512", "CTCL2F0713",
"CTCL2R0511", "CTCL2R0512", "CTCL2R0713", "CTCL3A0615", "CTCL3A0617",
"CTCL3A0716", "CTCL3R0511", "CTCL3R0512", "CTCL3R0713", "CTCL4R0511",
"CTCL4R0512", "CTCL4R0713", "CTCL5A0615", "CTCL5A0617", "CTCL5A0716",
"CTCL5F0511", "CTCL5F0512", "CTCL5F0713", "CTCL5R0511", "CTCL5R0512",
"CTCL5R0713", "CTCL6A0615", "CTCL6A0617", "CTCL6A0716", "CTCL6B0615",
"CTCL6B0617", "CTCL6B0716", "CTCL6F0511", "CTCL6F0512", "CTCL6F0713",
"CTCL6R0511", "CTCL6R0512", "CTSN1A0615", "CTSN1A0617", "CTSN1A0716",
"CTSN1B0615", "CTSN1B0617", "CTSN1B0716", "CTSN2A0615", "CTSN2A0617",
"CTSN2A0716", "CTSN3A0615", "CTSN3A0617", "CTSN3A0716", "CTSN3B0615",
"CTSN3B0617", "CTSN3B0716", "MLBV1A0516", "MLBV1A0715", "MLBV1A0717",
"MLBV1B0516", "MLBV1B0715", "MLBV1B0717", "MLBV2A0516", "MLBV2A0715",
"MLBV2A0717", "MLBV3A0516", "MLBV3A0715", "MLBV3A0717", "MLBV3B0516",
"MLBV3B0715", "MLBV3B0717", "MLBV4A0516", "MLBV4A0715", "MLBV4A0717",
"MLBV4B0516", "MLBV4B0715", "MLBV4B0717", "MLBV5A0516", "MLBV5A0715",
"MLBV5A0717", "MLBV5B0516", "MLBV5B0715", "MLBV5B0717", "MLBV6A0516",
"MLBV6A0715", "MLBV6A0717", "MLCB1A0315", "MLCB1A0416", "MLCB1A0717",
"MLCB1B0315", "MLCB1B0416", "MLCB1B0717", "MLCB2A0315", "MLCB2A0416",
"MLCB2A0717", "MLCB3A0315", "MLCB3A0416", "MLCB3A0717", "MLCB3B0315",
"MLCB3B0416", "MLCB3B0717", "MLCB4A0315", "MLCB4A0416", "MLCB4A0717",
"MLCB4B0315", "MLCB4B0416", "MLCB4B0717", "MLCB5A0315", "MLCB5A0416",
"MLCB5A0717", "MLCB5B0315", "MLCB5B0416", "MLCB5B0717", "MLCB6A0315",
"MLCB6A0416", "MLCB6A0717","NAYU1F0417", "NAYU1F0514", "NAYU1F0515", "NAYU1F0712", "NAYU1F0816", "NAYU1R0417", "NAYU1R0514", "NAYU1R0515",
"NAYU1R0712", "NAYU1R0816", "NAYU2F0417", "NAYU2F0514", "NAYU2F0515",
"NAYU2F0712", "NAYU2F0816", "NAYU2R0415", "NAYU2R0417", "NAYU2R0514",
"NAYU2R0712", "NAYU2R0816", "NAYU3F0417", "NAYU3F0514", "NAYU3F0515",
"NAYU3F0712", "NAYU3F0816", "NAYU3R0417", "NAYU3R0514", "NAYU3R0515",
"NAYU3R0712", "NAYU3R0816", "NAYU4R0417", "NAYU4R0514", "NAYU4R0515",
"NAYU4R0712", "NAYU4R0816", "NAYU5F0417", "NAYU5F0514", "NAYU5F0515",
"NAYU5F0712", "NAYU5F0816", "NAYU5R0417", "NAYU5R0514", "NAYU5R0515",
"NAYU5R0712", "NAYU5R0816", "NAYU6R0514", "NAYU6R0515", "NAYU6R0712",
"NAYU6R0816"), class = "factor"),
Abudefduf.bengalensis = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24), Abudefduf.sexfasciatus = c(25,
26, 27, 28, 29, 30, 31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16), Abudefduf.sordidus = c(17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 1, 2, 3, 4, 5, 6, 7, 8), Abudefduf.vaigiensis = c(9,
10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)), row.names = c(NA, 24L), class = "data.frame")
df1<- matrix(data = c(1:32), nrow = 8, ncol = 4)
df2<- matrix(data = c(1:32), nrow = 8, ncol = 4)
df3<- matrix(data = c(1:16), nrow = 4, ncol = 4)
df4<- matrix(data = c(1:16), nrow = 4, ncol = 4)
Y<- list(df1, df2, df3, df4)
#NDMS, sqrt transformation, Bray default. Have to use pre-transformed data.
Y.nmds<- lapply(Y, metaMDS)
Теперь, чтобы График:
Y.nmds
data.scores.Y<- lapply(Y.nmds, as.data.frame(scores)) #Using the scores function from vegan to extract the site scores and convert to a data.frame- **this works.**
Итак, теперь у меня есть что-то похожее на это:
[[1]]
value.NMDS1 value.NMDS2
1 -0.225596839 3.205973e-09
2 -0.135301170 -2.518875e-09
3 -0.062437097 -1.622530e-09
4 -0.002401206 -8.839924e-10
5 0.047920139 -2.649592e-10
6 0.090708349 2.614043e-10
7 0.127537199 7.144582e-10
8 0.159570625 1.108521e-09
[[2]]
value.NMDS1 value.NMDS2
1 -0.225596839 3.205973e-09
2 -0.135301170 -2.518875e-09
3 -0.062437097 -1.622530e-09
4 -0.002401206 -8.839924e-10
5 0.047920139 -2.649592e-10
6 0.090708349 2.614043e-10
7 0.127537199 7.144582e-10
8 0.159570625 1.108521e-09
, и я хотел бы использовать lapply
, чтобы добавить столбцы в указанные выше фреймы данных. должно происходить от имен в "Z":
data.scores.Y$reserve<- lapply(data.scores.Y, function(x){ transform(x)<- Z$reserve_status}) # create a column of reserve status, from the rownames of Z - **DOESN'T WORK.**
data.scores.Y$year <- factor(Z$B_A) #create column of years-how I would do it for one dataframe
head(data.scores.Y)
```
to create something that looks like this (note I got rid of some rows):
```
[1]
value.NMDS1 value.NMDS2 reserve year
1 -0.225596839 3.205973e-09 inside before
2 -0.135301170 -2.518875e-09 outside before
3 -0.062437097 -1.622530e-09 inside after
4 -0.002401206 -8.839924e-10 outside after
5 0.047920139 -2.649592e-10 outside after
[[2]]
value.NMDS1 value.NMDS2 reserve year
1 -0.225596839 3.205973e-09 inside before
2 -0.135301170 -2.518875e-09 outside before
3 -0.062437097 -1.622530e-09 inside after
4 -0.002401206 -8.839924e-10 outside after
Думаю, как только я решу эту проблему, я смогу пройти через построение. Я искал в Google много разных решений, но ни одно, кажется, не дает полного ответа на этот вопрос, или я не могу понять, как адаптировать эти ответы.
Заранее благодарю. Синтаксис не моя сильная сторона.