将列添加到 R 列表中的多个数据框
Adding columns to multiple dataframes in a list in R
提前道歉,这将是一个很长的设置来解决这个问题。不知道如何问更简单。
我正在尝试使用此结构在 ggplot 中绘制 20 个 nMDS 图。 https://chrischizinski.github.io/rstats/vegan-ggplot2/
我可以很容易地为 1 个数据帧执行此操作。但是我正在尝试使用 lapply
对所有 20 个进行这些操作。
我在 link 的这一部分 - 我没有编辑以下块以适合我的代码:
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
将列添加到上面的 datascores 数据帧中,数据应该来自“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
我想一旦我解决了这个问题,我就能通过绘图了。我用谷歌搜索了很多不同的解决方案,但 none 似乎完全回答了这个问题,或者我不知道如何调整这些回复。
提前谢谢你。语法不是我的强项。
根据评论,'Y' 被创建为 list
来自 split
通过 'site' 列
调整 'Z'
Y <- split(Z, Z$site)
Y.nmds <- lapply(Y, function(x) metaMDS(x[-(1:4)]))
现在,我们可以使用 Map
循环遍历 'Y.nmds' 的相应 list
元素,并使用 'Y' 创建列
lst1 <- Map(function(nmd, dat, site) {
dat1 <- as.data.frame(scores(nmd))
transform(dat1, site = site,
reserve_status = dat$reserve_status,
year = dat$year)
}, Y.nmds, Y, names(Y))
-输出
lst1
#$AYUNGON
# NMDS1 NMDS2 site reserve_status year
#1 -0.5405524 -0.208839893 AYUNGON Inside 2012
#2 -0.4679394 -0.127144729 AYUNGON Inside 2012
#3 -0.4470720 -0.035047840 AYUNGON Inside 2012
#4 -0.3723951 -0.004604424 AYUNGON Inside 2012
#5 -0.3527766 0.062692135 AYUNGON Inside 2012
#6 -0.3214464 0.117212445 AYUNGON Inside 2014
#7 -0.2585269 0.118163340 AYUNGON Inside 2014
#8 -0.2434562 0.173341542 AYUNGON Inside 2014
#9 0.8334110 -0.008478317 AYUNGON Inside 2014
#10 0.6918728 -0.048824777 AYUNGON Inside 2014
#11 0.5889373 0.008377321 AYUNGON Inside 2015
#12 0.4882185 -0.028821935 AYUNGON Inside 2015
#13 0.4017254 -0.018024867 AYUNGON Inside 2015
#$BINDOY
# NMDS1 NMDS2 site reserve_status year
#14 -0.4973781 0.0312765162 BINDOY Inside 2015
#15 -0.4901276 -0.0008288276 BINDOY Inside 2015
#16 -0.4816021 -0.0282379296 BINDOY Inside 2016
#17 0.1943789 0.0619863596 BINDOY Inside 2016
#18 0.1924750 0.0348895563 BINDOY Inside 2016
#19 0.1876516 0.0165441678 BINDOY Inside 2016
#20 0.1853833 0.0016281922 BINDOY Inside 2016
#21 0.1832898 -0.0126894179 BINDOY Inside 2017
#22 0.1774036 -0.0243657601 BINDOY Inside 2017
#23 0.1750507 -0.0370687919 BINDOY Inside 2017
#24 0.1734749 -0.0431340651 BINDOY Inside 2017
提前道歉,这将是一个很长的设置来解决这个问题。不知道如何问更简单。 我正在尝试使用此结构在 ggplot 中绘制 20 个 nMDS 图。 https://chrischizinski.github.io/rstats/vegan-ggplot2/
我可以很容易地为 1 个数据帧执行此操作。但是我正在尝试使用 lapply
对所有 20 个进行这些操作。
我在 link 的这一部分 - 我没有编辑以下块以适合我的代码:
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
将列添加到上面的 datascores 数据帧中,数据应该来自“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
我想一旦我解决了这个问题,我就能通过绘图了。我用谷歌搜索了很多不同的解决方案,但 none 似乎完全回答了这个问题,或者我不知道如何调整这些回复。
提前谢谢你。语法不是我的强项。
根据评论,'Y' 被创建为 list
来自 split
通过 'site' 列
Y <- split(Z, Z$site)
Y.nmds <- lapply(Y, function(x) metaMDS(x[-(1:4)]))
现在,我们可以使用 Map
循环遍历 'Y.nmds' 的相应 list
元素,并使用 'Y' 创建列
lst1 <- Map(function(nmd, dat, site) {
dat1 <- as.data.frame(scores(nmd))
transform(dat1, site = site,
reserve_status = dat$reserve_status,
year = dat$year)
}, Y.nmds, Y, names(Y))
-输出
lst1
#$AYUNGON
# NMDS1 NMDS2 site reserve_status year
#1 -0.5405524 -0.208839893 AYUNGON Inside 2012
#2 -0.4679394 -0.127144729 AYUNGON Inside 2012
#3 -0.4470720 -0.035047840 AYUNGON Inside 2012
#4 -0.3723951 -0.004604424 AYUNGON Inside 2012
#5 -0.3527766 0.062692135 AYUNGON Inside 2012
#6 -0.3214464 0.117212445 AYUNGON Inside 2014
#7 -0.2585269 0.118163340 AYUNGON Inside 2014
#8 -0.2434562 0.173341542 AYUNGON Inside 2014
#9 0.8334110 -0.008478317 AYUNGON Inside 2014
#10 0.6918728 -0.048824777 AYUNGON Inside 2014
#11 0.5889373 0.008377321 AYUNGON Inside 2015
#12 0.4882185 -0.028821935 AYUNGON Inside 2015
#13 0.4017254 -0.018024867 AYUNGON Inside 2015
#$BINDOY
# NMDS1 NMDS2 site reserve_status year
#14 -0.4973781 0.0312765162 BINDOY Inside 2015
#15 -0.4901276 -0.0008288276 BINDOY Inside 2015
#16 -0.4816021 -0.0282379296 BINDOY Inside 2016
#17 0.1943789 0.0619863596 BINDOY Inside 2016
#18 0.1924750 0.0348895563 BINDOY Inside 2016
#19 0.1876516 0.0165441678 BINDOY Inside 2016
#20 0.1853833 0.0016281922 BINDOY Inside 2016
#21 0.1832898 -0.0126894179 BINDOY Inside 2017
#22 0.1774036 -0.0243657601 BINDOY Inside 2017
#23 0.1750507 -0.0370687919 BINDOY Inside 2017
#24 0.1734749 -0.0431340651 BINDOY Inside 2017