楼主: woodpecker2013
1866 5

[问答] R中如何将样带两个时期的物种名录整理成包含两时期所有物种名称0-1格式的矩阵? [推广有奖]

  • 0关注
  • 0粉丝

小学生

57%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
52 点
帖子
6
精华
0
在线时间
6 小时
注册时间
2013-5-10
最后登录
2013-10-17

楼主
woodpecker2013 发表于 2013-5-12 20:50:36 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
周五发在学习版块了,好像是在这里求助更为合适


例如

一个样带包含四个样地

2010年样带物种组成信息为

plotname speciesname
plot1 sp1
plot1 sp2
plot1 sp3
plot1 sp4
plot2 sp2
plot2 sp4
plot2 sp5
plot2 sp6
plot2 sp7
plot3 sp3
plot3 sp5
plot3 sp7
plot3 sp8
plot3 sp9
plot3 sp10
plot3 sp11

2012年物种组成信息为
plotname speciesname
plot1 sp1
plot1 sp3
plot1 sp4
plot1 sp5
plot1 sp6
plot1 sp7
plot2 sp3
plot2 sp4
plot2 sp6
plot2 sp8
plot2 sp11
plot2 sp12
plot2 sp13
plot3 sp4
plot3 sp6
plot3 sp10
plot3 sp14
plot3 sp15
plot3 sp16

如何将2010年物种组成信息转化为

plotname sp1 sp2 sp3 sp4 sp5 sp6 sp7 sp8 sp9 sp10 sp11 sp12 sp13 sp14 sp15 sp16
plot1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
plot2 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0
plot3 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0
将2012年物种组成信息转化为
plotname sp1 sp2 sp3 sp4 sp5 sp6 sp7 sp8 sp9 sp10 sp11 sp12 sp13 sp14 sp15 sp16
plot1         1    0     1     1     1     1    1     0    0    0       0         0      0     0      0       0
plot2 0 0 1 1 0 1 0 1 0 0 1 1 1 0 0
plot3 0 0 0 1 0 1 0 0 0 1 0 0 1 1 1
即两个时期的物种组成矩阵维度相同,包含两时期出现所有物种名称及各物种在各样地的0-1信息?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:所有物 Species plot name SOSO 矩阵 如何

本帖被以下文库推荐

沙发
qoiqpwqr 发表于 2013-5-12 22:45:48
  1. d2010 <- read.table("clipboard", header = T)
  2. mat2010 <- matrix(0, 3, 16)
  3. colnames(mat2010) <- paste("sp", 1:16, sep = "")
  4. rownames(mat2010) <- paste("plot", 1:3, sep = "")
  5. for (i in 1:nrow(d2010)) {
  6.     mat2010[as.character(d2010[i, 1]), as.character(d2010[i, 2])] <- 1
  7. }

  8. d2012 <- read.table("clipboard", header = T)
  9. mat2012 <- matrix(0, 3, 16)
  10. colnames(mat2012) <- paste("sp", 1:16, sep = "")
  11. rownames(mat2012) <- paste("plot", 1:3, sep = "")
  12. for (i in 1:nrow(d2012)) {
  13.     mat2012[as.character(d2012[i, 1]), as.character(d2012[i, 2])] <- 1
  14. }
复制代码

藤椅
woodpecker2013 发表于 2013-5-13 15:17:27
qoiqpwqr 发表于 2013-5-12 22:45
好像是不行。

运行代码后出现如下信息

错误于mat2010[as.character(d2010[i, 1]), as.character(d2010[i, 2])] <- 1 :
  下标出界?

板凳
qoiqpwqr 发表于 2013-5-13 21:28:33
应该不会有错的
  1. > d2010 <- read.table("clipboard", header = T)
  2. > d2010
  3.    plotname speciesname
  4. 1     plot1         sp1
  5. 2     plot1         sp2
  6. 3     plot1         sp3
  7. 4     plot1         sp4
  8. 5     plot2         sp2
  9. 6     plot2         sp4
  10. 7     plot2         sp5
  11. 8     plot2         sp6
  12. 9     plot2         sp7
  13. 10    plot3         sp3
  14. 11    plot3         sp5
  15. 12    plot3         sp7
  16. 13    plot3         sp8
  17. 14    plot3         sp9
  18. 15    plot3        sp10
  19. 16    plot3        sp11
  20. > mat2010 <- matrix(0, 3, 16)
  21. > colnames(mat2010) <- paste("sp", 1:16, sep = "")
  22. > rownames(mat2010) <- paste("plot", 1:3, sep = "")
  23. > for (i in 1:nrow(d2010)) {
  24. +     mat2010[as.character(d2010[i, 1]), as.character(d2010[i, 2])] <- 1
  25. + }
  26. > mat2010
  27.       sp1 sp2 sp3 sp4 sp5 sp6 sp7 sp8 sp9 sp10 sp11 sp12 sp13 sp14 sp15 sp16
  28. plot1   1   1   1   1   0   0   0   0   0    0    0    0    0    0    0    0
  29. plot2   0   1   0   1   1   1   1   0   0    0    0    0    0    0    0    0
  30. plot3   0   0   1   0   1   0   1   1   1    1    1    0    0    0    0    0


  31. > d2012 <- read.table("clipboard", header = T)
  32. > d2012
  33.    plotname speciesname
  34. 1     plot1         sp1
  35. 2     plot1         sp3
  36. 3     plot1         sp4
  37. 4     plot1         sp5
  38. 5     plot1         sp6
  39. 6     plot1         sp7
  40. 7     plot2         sp3
  41. 8     plot2         sp4
  42. 9     plot2         sp6
  43. 10    plot2         sp8
  44. 11    plot2        sp11
  45. 12    plot2        sp12
  46. 13    plot2        sp13
  47. 14    plot3         sp4
  48. 15    plot3         sp6
  49. 16    plot3        sp10
  50. 17    plot3        sp14
  51. 18    plot3        sp15
  52. 19    plot3        sp16
  53. > mat2012 <- matrix(0, 3, 16)
  54. > colnames(mat2012) <- paste("sp", 1:16, sep = "")
  55. > rownames(mat2012) <- paste("plot", 1:3, sep = "")
  56. > for (i in 1:nrow(d2012)) {
  57. +     mat2012[as.character(d2012[i, 1]), as.character(d2012[i, 2])] <- 1
  58. + }
  59. > mat2012
  60.       sp1 sp2 sp3 sp4 sp5 sp6 sp7 sp8 sp9 sp10 sp11 sp12 sp13 sp14 sp15 sp16
  61. plot1   1   0   1   1   1   1   1   0   0    0    0    0    0    0    0    0
  62. plot2   0   0   1   1   0   1   0   1   0    0    1    1    1    0    0    0
  63. plot3   0   0   0   1   0   1   0   0   0    1    0    0    0    1    1    1
复制代码

报纸
wooson 发表于 2013-5-14 23:38:37
  1. aa = read.table("clipboard",head=T)
  2. aa$num = 1
  3. xtabs(num~., data=aa)
复制代码
供参考
> aa = read.table("clipboard",head=T)
> aa
   plotname speciesname
1     plot1         sp1
2     plot1         sp2
3     plot1         sp3
4     plot1         sp4
5     plot2         sp2
6     plot2         sp4
7     plot2         sp5
8     plot2         sp6
9     plot2         sp7
10    plot3         sp3
11    plot3         sp5
12    plot3         sp7
13    plot3         sp8
14    plot3         sp9
15    plot3        sp10
16    plot3        sp11
> aa$num = 1
> aa
   plotname speciesname num
1     plot1         sp1   1
2     plot1         sp2   1
3     plot1         sp3   1
4     plot1         sp4   1
5     plot2         sp2   1
6     plot2         sp4   1
7     plot2         sp5   1
8     plot2         sp6   1
9     plot2         sp7   1
10    plot3         sp3   1
11    plot3         sp5   1
12    plot3         sp7   1
13    plot3         sp8   1
14    plot3         sp9   1
15    plot3        sp10   1
16    plot3        sp11   1
> xtabs(num~.,data=aa)
        speciesname
plotname sp1 sp10 sp11 sp2 sp3 sp4 sp5 sp6 sp7 sp8 sp9
   plot1   1    0    0   1   1   1   0   0   0   0   0
   plot2   0    0    0   1   0   1   1   1   1   0   0
   plot3   0    1    1   0   1   0   1   0   1   1   1

地板
wooson 发表于 2013-5-14 23:39:12
wooson 发表于 2013-5-14 23:38
供参考
> aa = read.table("clipboard",head=T)
> aa
   plotname speciesname
1     plot1         sp1
2     plot1         sp2
3     plot1         sp3
4     plot1         sp4
5     plot2         sp2
6     plot2         sp4
7     plot2         sp5
8     plot2         sp6
9     plot2         sp7
10    plot3         sp3
11    plot3         sp5
12    plot3         sp7
13    plot3         sp8
14    plot3         sp9
15    plot3        sp10
16    plot3        sp11
> aa$num = 1
> aa
   plotname speciesname num
1     plot1         sp1   1
2     plot1         sp2   1
3     plot1         sp3   1
4     plot1         sp4   1
5     plot2         sp2   1
6     plot2         sp4   1
7     plot2         sp5   1
8     plot2         sp6   1
9     plot2         sp7   1
10    plot3         sp3   1
11    plot3         sp5   1
12    plot3         sp7   1
13    plot3         sp8   1
14    plot3         sp9   1
15    plot3        sp10   1
16    plot3        sp11   1
> xtabs(num~.,data=aa)
        speciesname
plotname sp1 sp10 sp11 sp2 sp3 sp4 sp5 sp6 sp7 sp8 sp9
   plot1   1    0    0   1   1   1   0   0   0   0   0
   plot2   0    0    0   1   0   1   1   1   1   0   0
   plot3   0    1    1   0   1   0   1   0   1   1   1

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-26 07:58