我已将名称导入并分配给两个数据集: 1.'CC.sp.ba'-物种(列)x群落(行)数据框,其中包含树的基础面积值 2.'CC.traits'-包含特征值的特征(列)x种类(行)数据框。
我想用一段代码来计算社区1(CC.sp.ba中的'com1')所有特征的社区均值。
给定性状的群落平均值是指与基础面积> 0的群落1中的物种相关的所有性状值的平均值。
我已经提取了社区1中所有物种的特征值,但是,用函数mean()包装相同的代码会返回错误。我是否必须将数据框转换为矩阵?
#import data
CC.sp.ba = read.table("CC.sp.basalarea_v2.txt", header = T, row.names = 1)
CC.traits = read.table("CC.sp.trait.matrix.v2.txt", sep = "\t", header = T, row.names = 1)
#check imports
str(CC.sp.ba)
dim(CC.sp.ba)
str(CC.traits)
dim(CC.traits)
#extract species trait values present in com1
CC.traits[names(CC.sp.ba[1, CC.sp.ba[1,] > 0]), ]
#what is the community mean for each trait in com1?
mean(CC.traits[names(CC.sp.ba[1, CC.sp.ba[1,] > 0]), ], na.rm = T)
这是我的两个数据集:
> dput(CC.sp.ba)
structure(list(GYMLUC = c(20.9, 21.54, 50.2, 17.67, 142.08, 16.85,
184.29, 316.83, 0.71, 1261.405644, 827.1221911), CRORHA = c(10.23,
7.68, 29.03, 9.11, 58.79, 10.26, 112.25, 129.68, 0, 686.8964985,
0), COCKRU = c(0.48, 0, 0, 0, 0, 0, 0, 0, 0, 667.4407852, 206.0152755
), COCMIC = c(8.52, 15.85, 36.93, 12.63, 119.19, 17.02, 186.17,
282.43, 3.8, 589.6194399, 590.4755239), THOPOR = c(7.84, 7.77,
29.17, 9.15, 76.48, 9.99, 109.22, 227.88, 5.27, 549.6187194,
929.4234417), COCDIV = c(0, 0, 0, 0, 0, 0, 0, 0, 0.61, 390.0786726,
827.7592275), ERIFRU = c(1.37, 1.61, 5.35, 3.15, 19.35, 2.98,
32.56, 50.49, 0, 265.8502052, 0), PICACU = c(2.005, 4.3, 10.64,
1.95, 24.35, 3.49, 38.2, 58.05, 0, 254.6088015, 873.7749197),
EUGXER = c(2.01, 3.08, 5.52, 0, 14.52, 0, 11.97, 0, 0, 245.7902725,
102.262609), PISALB = c(0, 0, 0, 0, 0, 0, 0, 0, 6.96, 225.2963288,
1389.722521), BOUSUC = c(0.94, 0, 8.88, 2.15, 0, 1.81, 19.85,
0, 0, 206.4674292, 111.1708305), EUGFOE = c(8.08, 9.15, 31.9,
8.23, 59.15, 7.41, 81.08, 86.81, 0.67, 176.1366878, 390.3889316
), STEACU = c(0, 0, 0, 0, 0, 0, 0, 0, 0.49, 121.0073925,
0), BURSIM = c(0.37, 4.23, 23.65, 7.59, 37.06, 6.04, 66.09,
81.57, 2.82, 108.6285752, 1661.919686), CROGLA = c(10.44,
9.95, 26.57, 13.52, 88.12, 11.23, 126.88, 121.77, 0, 0, 37.69447735
), COMDOD = c(1.84, 0, 0, 0, 0, 0, 9.3, 0, 0.48, 0, 40.91107547
), EXOCAR = c(1.57, 2.28, 0, 0, 0, 0, 2.9, 0, 9.75, 0, 703.4056179
), CRODIS = c(0.34, 0, 0, 0, 0, 0, 3.16, 0, 0, 0, 0), GUEKRU = c(1.1,
2.76, 10.05, 4.38, 21.04, 2.82, 30.82, 37.07, 1.5, 0, 0),
GUEELL = c(0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 150.3278763),
JACBER = c(0.61, 0, 0, 0, 0, 0, 2.04, 0, 0, 0, 608.3438816
), KRUFER = c(0.05, 0, 0, 0, 0, 0, 0, 0, 0.49, 0, 707.9391999
), LANINV = c(0, 1.59, 5.08, 0, 0, 1.8, 19.77, 0, 3.67, 0,
0), WEDLAN = c(0, 0.44, 0, 2.35, 27.67, 0, 36.22, 0, 0.48,
0, 0), AMYELE = c(0, 0, 0, 0, 0, 0, 3.98, 0, 1.2, 0, 4065.510833
), BUCBUC = c(0, 0, 0, 0, 0, 0, 0, 0, 19.64, 0, 2395.229602
), SALSES = c(0, 0, 0, 0, 0, 0, 0, 0, 4.22, 0, 735.1889814
), LEULEU = c(0, 0, 0, 0, 0, 0, 0, 0, 2.05, 0, 701.6687382
), TABHET = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 532.7651747),
EUGRHO = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 394.7627872), PITUNG = c(0,
0, 0, 0, 0, 0, 0, 0, 6.81, 0, 290.7742657), HELJAM = c(0,
0, 0, 0, 0, 0, 0, 0, 8.06, 0, 87.07756413), SAMDOD = c(0,
0, 0, 0, 0, 0, 0, 0, 3.03, 0, 86.60915267), REYGUA = c(0,
0, 0, 0, 0, 0, 0, 0, 0.52, 0, 77.54133833), PLUALB = c(0,
0, 0, 0, 0, 0, 0, 0, 0.48, 0, 33.18307184), CASXYL = c(0,
0, 0, 0, 0, 0, 0, 0, 0.48, 0, 21.67934513), SWIMAH = c(0,
0, 0, 0, 0, 0, 9.73, 0, 0, 0, 0)), class = "data.frame", row.names = c("com1",
"com2", "com3", "com4", "com5", "com6", "com7", "com8", "com9",
"com10", "com11"))
> dput(CC.traits)
structure(list(SLA = c(75.9, 103.5, 91.7, 147.2, 37, 79.2, 73.85,
83.4, 84, 63.5, 84.13, 134.3, 54, 81, 68.4, 80, 120.3, 119.3,
78.7, 81.9, 151, 58.3, 106.6, 101.33, 127.55, 113.3, 109.8, 119.1,
74.4, 57.8, 146, 138.32, 108.49, 78.15, 77, 84, 117.3), LDMC = c(0.55,
0.38, 0.48, 0.28, 0.5, 0.46, 0.46, 0.53, 0.35, 0.41, 0.34, 0.41,
0.36, 0.45, 0.44, 0.47, 0.38, 0.44, 0.46, 0.45, 0.44, 0.45, 0.45,
0.36, 0.39, 0.47, 0.32, 0.49, 0.23, 0.54, 0.51, 0.42, 0.33, 0.53,
0.45, 0.47, 0.34), LA = c(16.94, 17.21, 10.03, 99.69, 2.74, 22.18,
10.86, 11.56, 33.13, 3.22, 4.24, 10.07, 11.63, 3.37, 4.05, 13.51,
10.6, 7.5, 23.66, 7.56, 85.86, 1.57, 7.92, 5.21, 99.97, 24.9,
22.14, 11.82, 64.7, 13.75, 21.74, 22.66, 3.94, 54.34, 10.8, 25.33,
8.85), LNC = c(1.9188088, 1.7614906, 1.290438, 1.689876, 0.8302528,
0.95263, NA, 1.2416738, 1.6589502, 1.3450672, NA, NA, 0.8582886,
1.3142324, 0.861374, 0.8860538, 2.0102576, 1.295464, 1.3981406,
1.6320698, 2.314981937, 0.9077578, 1.9488048, 1.002340704, 2.1624114,
2.0907072, 2.173463, 2.3950206, 1.9975168, 1.0654708, 1.874769211,
2.926402855, NA, 1.237917757, 1.5005978, 1.4856384, NA), LCC = c(47.3047114,
42.9450226, 47.6619398, 49.4926766, 45.6836828, 45.5810874, NA,
50.2479704, 48.0294908, 48.949511, NA, NA, 55.0397358, 49.2616368,
47.9119342, 51.8730816, 51.4244232, 48.4945388, 49.1115594, 46.1324934,
43.17878571, 50.6282388, 46.1696618, 40.62408543, 46.598863,
44.4949426, 44.070354, 46.5736804, 49.6228904, 47.8008056, 44.88132553,
45.39932861, NA, 49.25407715, 47.1947452, 51.1394106, NA), LPP = c(27.89543918,
13.18525215, 23.1064764, 32.22573772, 11.29075999, 19.14508943,
20.84716214, 15.06101892, 26.80893653, 12.80235036, 19.59966423,
19.30522374, 7.650933041, 22.91922257, 11.19648738, 13.57654807,
23.11551624, 17.74714276, 15.3961387, 25.74029831, 23.62110157,
11.18809324, 19.47116937, 20.08135856, 23.55459418, 25.16497708,
35.19532511, 26.0773552, 33.63466133, 14.60967263, 25.7816233,
20.00645703, 16.50610189, 18.5710596, 21.7466262, 20.96016013,
19.4317815), psi_max = c(60.8, 48.1, 42.8, 6.4, 34.6, 33.1, NA,
40.3, 11, 33.1, NA, NA, 41.2, 56.3, 71.1, 38, 50.2, 59.3, 49.8,
57.7, NA, 29, 53.5, NA, 26.4, 36.9, 23, 51, 14.5, 36.6, NA, NA,
NA, NA, 26.4, 44.6, NA), WD = c(0.899260979, 0.676606672, 0.870232204,
0.2510932, 0.730772283, 0.769853297, 0.841698597, 0.754002829,
0.909014991, 0.843539019, 0.837285341, 0.832793627, 0.994393792,
0.769313634, 0.794045687, 0.892244107, 0.900821498, 0.756282959,
0.785521771, 0.958602284, 0.675844096, 0.710362326, 1.039844924,
0.72851963, 0.721394665, 1.029333631, 0.436947608, 0.752477862,
0.600673147, 0.933481153, 0.803070694, 0.874902042, 0.780287544,
0.681921822, 0.623790785, 1.170984813, 0.689935706)), class = "data.frame", row.names = c("AMYELE",
"BOUSUC", "BUCBUC", "BURSIM", "CASXYL", "COCDIV", "COCKRU", "COCMIC",
"COMDOD", "CRORHA", "CRODIS", "CROGLA", "ERIFRU", "EUGFOE", "EUGRHO",
"EUGXER", "EXOCAR", "GUEELL", "GUEKRU", "GYMLUC", "HELJAM", "JACBER",
"KRUFER", "LANINV", "LEULEU", "PICACU", "PISALB", "PITUNG", "PLUALB",
"REYGUA", "SALSES", "SAMDOD", "STEACU", "SWIMAH", "TABHET", "THOPOR",
"WEDLAN"))
#output from last command
> mean(CC.traits[names(CC.sp.ba[1, CC.sp.ba[1,] > 0]), ], na.rm = T)
[1] NA
Warning message:
In mean.default(CC.traits[names(CC.sp.ba[1, CC.sp.ba[1, ] > 0]), :
argument is not numeric or logical: returning NA