Skip to contents

Takes a matrix or vector computed on occupied sites only and expands it back to the full site grid, inserting NA for unoccupied sites. This is useful when performing custom analyses on ps$comm (which contains only occupied sites) and mapping the results back to the full raster or spatial object.

Usage

ps_expand(ps, x, spatial = FALSE)

Arguments

ps

A phylospatial object.

x

A matrix (or vector) with nrow(ps$comm) rows (i.e. one row per occupied site).

spatial

Logical: if TRUE, convert the expanded result to a spatial object using ps$spatial. Default is FALSE.

Value

If spatial = FALSE, a matrix with ps$n_sites rows and NA for unoccupied sites. If spatial = TRUE, a SpatRaster or sf object.

Examples

ps <- ps_simulate()

# custom analysis on the occupied-only community matrix
site_totals <- matrix(rowSums(ps$comm), ncol = 1)
colnames(site_totals) <- "total"

# expand to full extent as a matrix
ps_expand(ps, site_totals)
#>            total
#>   [1,] 0.6723806
#>   [2,] 0.8236921
#>   [3,] 0.9650872
#>   [4,] 1.0948433
#>   [5,] 1.2110773
#>   [6,] 1.3118786
#>   [7,] 1.3954419
#>   [8,] 1.4600834
#>   [9,] 1.5040506
#>  [10,] 1.5251235
#>  [11,] 1.5202349
#>  [12,] 1.5769849
#>  [13,] 1.6759548
#>  [14,] 1.7461461
#>  [15,] 1.7914070
#>  [16,] 1.8134398
#>  [17,] 1.8104136
#>  [18,] 1.7765601
#>  [19,] 1.7026107
#>  [20,] 1.5784664
#>  [21,] 0.7938316
#>  [22,] 0.9529280
#>  [23,] 1.1025634
#>  [24,] 1.2407617
#>  [25,] 1.3652697
#>  [26,] 1.4737949
#>  [27,] 1.5643141
#>  [28,] 1.6352066
#>  [29,] 1.6850050
#>  [30,] 1.7116818
#>  [31,] 1.7112395
#>  [32,] 1.8369678
#>  [33,] 1.9331655
#>  [34,] 1.9989418
#>  [35,] 2.0430812
#>  [36,] 2.0692636
#>  [37,] 2.0760186
#>  [38,] 2.0562678
#>  [39,] 1.9960585
#>  [40,] 1.8769329
#>  [41,] 0.9024007
#>  [42,] 1.0694502
#>  [43,] 1.2277383
#>  [44,] 1.3750008
#>  [45,] 1.5084315
#>  [46,] 1.6250517
#>  [47,] 1.7224156
#>  [48,] 1.7990427
#>  [49,] 1.8541025
#>  [50,] 1.8866803
#>  [51,] 1.9325600
#>  [52,] 2.0719139
#>  [53,] 2.1578472
#>  [54,] 2.2127094
#>  [55,] 2.2527717
#>  [56,] 2.2821354
#>  [57,] 2.3000985
#>  [58,] 2.3002221
#>  [59,] 2.2645427
#>  [60,] 2.1578482
#>  [61,] 0.9962780
#>  [62,] 1.1798192
#>  [63,] 1.3522572
#>  [64,] 1.5109817
#>  [65,] 1.6524228
#>  [66,] 1.7723473
#>  [67,] 1.8675510
#>  [68,] 1.9484702
#>  [69,] 2.0064716
#>  [70,] 2.0414525
#>  [71,] 2.1191860
#>  [72,] 2.2803977
#>  [73,] 2.3335318
#>  [74,] 2.3758161
#>  [75,] 2.4108166
#>  [76,] 2.4411930
#>  [77,] 2.4681632
#>  [78,] 2.4899232
#>  [79,] 2.4941635
#>  [80,] 2.4208605
#>  [81,] 1.1147143
#>  [82,] 1.3062900
#>  [83,] 1.4878655
#>  [84,] 1.6569933
#>  [85,] 1.8094042
#>  [86,] 1.9382525
#>  [87,] 2.0378343
#>  [88,] 2.1082934
#>  [89,] 2.1504998
#>  [90,] 2.1696694
#>  [91,] 2.2430877
#>  [92,] 2.3681446
#>  [93,] 2.4387363
#>  [94,] 2.4798912
#>  [95,] 2.5104620
#>  [96,] 2.5373982
#>  [97,] 2.5643706
#>  [98,] 2.5937485
#>  [99,] 2.6271880
#> [100,] 2.6659020
#> [101,] 1.2142273
#> [102,] 1.4127303
#> [103,] 1.6025264
#> [104,] 1.7819006
#> [105,] 1.9473308
#> [106,] 2.0880858
#> [107,] 2.1895839
#> [108,] 2.2595674
#> [109,] 2.2999104
#> [110,] 2.3048062
#> [111,] 2.3304003
#> [112,] 2.4276467
#> [113,] 2.4917125
#> [114,] 2.5285164
#> [115,] 2.5514034
#> [116,] 2.5681350
#> [117,] 2.5819785
#> [118,] 2.5918826
#> [119,] 2.5859228
#> [120,] 2.5050201
#> [121,] 1.2928934
#> [122,] 1.4965133
#> [123,] 1.6921979
#> [124,] 1.8789248
#> [125,] 2.0553491
#> [126,] 2.2194579
#> [127,] 2.3125774
#> [128,] 2.3860365
#> [129,] 2.4297256
#> [130,] 2.4307489
#> [131,] 2.4154390
#> [132,] 2.4714303
#> [133,] 2.5052008
#> [134,] 2.6781668
#> [135,] 2.7567982
#> [136,] 2.6901927
#> [137,] 2.5301415
#> [138,] 2.5074557
#> [139,] 2.4529260
#> [140,] 2.3320501
#> [141,] 1.3494407
#> [142,] 1.5558996
#> [143,] 1.7541761
#> [144,] 1.9428789
#> [145,] 2.1191046
#> [146,] 2.2732016
#> [147,] 2.3918848
#> [148,] 2.4822207
#> [149,] 2.5402268
#> [150,] 2.5449895
#> [151,] 2.6176801
#> [152,] 2.7752723
#> [153,] 3.0669943
#> [154,] 3.1687862
#> [155,] 3.2554122
#> [156,] 3.1580073
#> [157,] 2.8836737
#> [158,] 2.4664981
#> [159,] 2.2854067
#> [160,] 2.1455996
#> [161,] 1.3834008
#> [162,] 1.5905310
#> [163,] 1.7884709
#> [164,] 1.9753035
#> [165,] 2.1478868
#> [166,] 2.3012704
#> [167,] 2.4320953
#> [168,] 2.5418006
#> [169,] 2.8125382
#> [170,] 3.1137047
#> [171,] 3.2188454
#> [172,] 3.6632280
#> [173,] 4.0345522
#> [174,] 4.1627966
#> [175,] 4.0007402
#> [176,] 3.6558356
#> [177,] 3.2765145
#> [178,] 2.7556957
#> [179,] 2.1806130
#> [180,] 2.0101312
#> [181,] 1.3950059
#> [182,] 1.6011399
#> [183,] 1.7967397
#> [184,] 1.9796795
#> [185,] 2.1475815
#> [186,] 2.2983348
#> [187,] 2.4315393
#> [188,] 2.7384520
#> [189,] 3.2382532
#> [190,] 3.6453594
#> [191,] 3.9364897
#> [192,] 4.4678316
#> [193,] 4.8966016
#> [194,] 5.0993798
#> [195,] 4.9530464
#> [196,] 4.2670149
#> [197,] 3.5421808
#> [198,] 2.9167646
#> [199,] 2.2251925
#> [200,] 1.8869572
#> [201,] 1.3848863
#> [202,] 1.5889501
#> [203,] 1.7809156
#> [204,] 1.9583253
#> [205,] 2.1190898
#> [206,] 2.2619671
#> [207,] 2.3866463
#> [208,] 2.9725997
#> [209,] 3.5145403
#> [210,] 3.9895197
#> [211,] 4.5021016
#> [212,] 5.1294045
#> [213,] 5.5077675
#> [214,] 5.7329310
#> [215,] 5.8364064
#> [216,] 4.7307736
#> [217,] 3.5837344
#> [218,] 2.9001615
#> [219,] 2.1719291
#> [220,] 1.7536556
#> [221,] 1.3537422
#> [222,] 1.5552841
#> [223,] 1.7429790
#> [224,] 1.9135047
#> [225,] 2.0643560
#> [226,] 2.1943553
#> [227,] 2.4455181
#> [228,] 3.0786380
#> [229,] 3.7072709
#> [230,] 4.2541188
#> [231,] 4.8467480
#> [232,] 5.5823216
#> [233,] 5.8226230
#> [234,] 5.7722067
#> [235,] 5.4487600
#> [236,] 4.5993384
#> [237,] 3.4798347
#> [238,] 2.6784361
#> [239,] 1.9614461
#> [240,] 1.6098095
#> [241,] 1.3252501
#> [242,] 1.5012406
#> [243,] 1.6849874
#> [244,] 1.8480696
#> [245,] 1.9867881
#> [246,] 2.1005218
#> [247,] 2.3930901
#> [248,] 3.0893009
#> [249,] 3.7355231
#> [250,] 4.3292639
#> [251,] 4.8828627
#> [252,] 5.8688823
#> [253,] 5.7124157
#> [254,] 5.4154356
#> [255,] 4.8736837
#> [256,] 4.0786607
#> [257,] 3.0606780
#> [258,] 2.2743419
#> [259,] 1.6493632
#> [260,] 1.4552203
#> [261,] 1.2820234
#> [262,] 1.4470004
#> [263,] 1.6086496
#> [264,] 1.7652324
#> [265,] 1.8898547
#> [266,] 1.9848580
#> [267,] 2.2491432
#> [268,] 2.9396409
#> [269,] 3.6299852
#> [270,] 4.2194758
#> [271,] 4.6664583
#> [272,] 4.9091816
#> [273,] 5.0046380
#> [274,] 4.7263052
#> [275,] 4.1867684
#> [276,] 3.3909861
#> [277,] 2.3982598
#> [278,] 1.7374389
#> [279,] 1.4879929
#> [280,] 1.2897352
#> [281,] 1.2179775
#> [282,] 1.3830482
#> [283,] 1.5364832
#> [284,] 1.6681157
#> [285,] 1.7744864
#> [286,] 1.8522291
#> [287,] 2.1277489
#> [288,] 2.8965245
#> [289,] 3.5682762
#> [290,] 4.0944447
#> [291,] 4.4237233
#> [292,] 4.4660996
#> [293,] 4.1630187
#> [294,] 3.7801412
#> [295,] 3.3066768
#> [296,] 2.5908855
#> [297,] 1.8284900
#> [298,] 1.8191918
#> [299,] 1.6876627
#> [300,] 1.4280294
#> [301,] 1.1308197
#> [302,] 1.2926630
#> [303,] 1.4467504
#> [304,] 1.5916693
#> [305,] 1.6393785
#> [306,] 1.9120605
#> [307,] 2.2375119
#> [308,] 2.7181881
#> [309,] 3.3614216
#> [310,] 3.8318873
#> [311,] 4.0853770
#> [312,] 4.0803325
#> [313,] 3.8054272
#> [314,] 3.2969973
#> [315,] 2.6449236
#> [316,] 2.2401464
#> [317,] 2.1649223
#> [318,] 2.2029790
#> [319,] 2.0987743
#> [320,] 1.8450448
#> [321,] 1.0200394
#> [322,] 1.1724216
#> [323,] 1.3120891
#> [324,] 1.4246165
#> [325,] 1.6216086
#> [326,] 1.9729302
#> [327,] 2.3119798
#> [328,] 2.6037024
#> [329,] 3.0428394
#> [330,] 3.4801757
#> [331,] 3.6849204
#> [332,] 3.6443393
#> [333,] 3.3727082
#> [334,] 2.9014514
#> [335,] 2.3360812
#> [336,] 2.2450990
#> [337,] 2.4702798
#> [338,] 2.5923963
#> [339,] 2.5272290
#> [340,] 2.2360800
#> [341,] 0.8887445
#> [342,] 1.0286891
#> [343,] 1.1521520
#> [344,] 1.2505033
#> [345,] 1.5777990
#> [346,] 1.9833190
#> [347,] 2.3603544
#> [348,] 2.6946592
#> [349,] 2.9585601
#> [350,] 3.1000889
#> [351,] 3.2376116
#> [352,] 3.1531311
#> [353,] 2.8703294
#> [354,] 2.4455304
#> [355,] 2.3419209
#> [356,] 2.4121319
#> [357,] 2.7249337
#> [358,] 2.9413441
#> [359,] 2.9394976
#> [360,] 2.5632025
#> [361,] 0.7417677
#> [362,] 0.8700180
#> [363,] 0.9812343
#> [364,] 1.1308700
#> [365,] 1.4892416
#> [366,] 1.9236126
#> [367,] 2.3342467
#> [368,] 2.7184505
#> [369,] 3.0571720
#> [370,] 3.2602950
#> [371,] 3.1993593
#> [372,] 2.9972950
#> [373,] 2.7450175
#> [374,] 2.4640850
#> [375,] 2.4108225
#> [376,] 2.5070129
#> [377,] 2.8278621
#> [378,] 3.1043498
#> [379,] 3.3253484
#> [380,] 2.6919535
#> [381,] 0.5831978
#> [382,] 0.7014733
#> [383,] 0.8033073
#> [384,] 0.9706987
#> [385,] 1.3377203
#> [386,] 1.7828715
#> [387,] 2.2097505
#> [388,] 2.6208904
#> [389,] 3.0182424
#> [390,] 3.4033365
#> [391,] 3.1730842
#> [392,] 2.9243997
#> [393,] 2.6584314
#> [394,] 2.3765321
#> [395,] 2.2826730
#> [396,] 2.3919663
#> [397,] 2.7319663
#> [398,] 2.8712214
#> [399,] 2.8310836
#> [400,] 2.4395900

# expand and convert to spatial
ps_expand(ps, site_totals, spatial = TRUE)
#> class       : SpatRaster 
#> size        : 20, 20, 1  (nrow, ncol, nlyr)
#> resolution  : 1, 1  (x, y)
#> extent      : 0, 20, 0, 20  (xmin, xmax, ymin, ymax)
#> coord. ref. :  
#> source(s)   : memory
#> varname     : total 
#> name        :     total 
#> min value   : 0.5831978 
#> max value   : 5.8688823