Merges two hyperSpec objects and base::cbind()s their spectra matrices, or merges additional extra data into a hyperSpec object.

# S4 method for hyperSpec,hyperSpec
merge(x, y, ...)

# S4 method for hyperSpec,data.frame
merge(x, y, ...)

# S4 method for data.frame,hyperSpec
merge(x, y, ...)

Arguments

x

a hyperSpec object or data.frame

y

a hyperSpec object or data.frame (including derived classes like tibble)

...

handed to base::merge.data.frame()

Details

After merging, the spectra matrix can contain duplicates, and is not ordered according to the wavelength.

If the wavelength axis should be ordered, use orderwl().

If a hyperSpec object and a data.frame are merged, the result is of the class of the first (x) object.

See also

base::merge().

collapse() combines hyperSpec objects that do not share the wavelength axis. rbind(), and cbind() for combining hyperSpec objects that.

Author

C. Beleites

Examples

merge(faux_cell [1:10, , 600], faux_cell [5:15, , 600], by = c("x", "y"))$.
#> x y region.x .nx region.y .ny spc.1 spc.2 #> 1 -2.55 -4.77 matrix 10 matrix 6 NA NA #> 2 -3.55 -4.77 matrix 9 matrix 5 NA NA #> 3 -4.55 -4.77 matrix 8 matrix 4 NA NA #> 4 -5.55 -4.77 matrix 7 matrix 3 NA NA #> 5 -6.55 -4.77 matrix 6 matrix 2 NA NA #> 6 -7.55 -4.77 matrix 5 matrix 1 NA NA
tmp <- merge(faux_cell [1:10, , 610], faux_cell [5:15, , 610], by = c("x", "y"), all = TRUE ) tmp$.
#> x y region.x .nx region.y .ny spc.1 spc.2 #> 1 -11.55 -4.77 matrix 1 <NA> NA 15 NA #> 2 -10.55 -4.77 matrix 2 <NA> NA 159 NA #> 3 -9.55 -4.77 matrix 3 <NA> NA 99 NA #> 4 -8.55 -4.77 matrix 4 <NA> NA 39 NA #> 5 -7.55 -4.77 matrix 5 matrix 1 1 1 #> 6 -6.55 -4.77 matrix 6 matrix 2 90 90 #> 7 -5.55 -4.77 matrix 7 matrix 3 79 79 #> 8 -4.55 -4.77 matrix 8 matrix 4 59 59 #> 9 -3.55 -4.77 matrix 9 matrix 5 152 152 #> 10 -2.55 -4.77 matrix 10 matrix 6 163 163 #> 11 -1.55 -4.77 <NA> NA matrix 7 NA 176 #> 12 -0.55 -4.77 <NA> NA matrix 8 NA 33 #> 13 0.45 -4.77 <NA> NA matrix 9 NA 11 #> 14 1.45 -4.77 <NA> NA matrix 10 NA 66 #> 15 2.45 -4.77 <NA> NA matrix 11 NA 84
wl(tmp)
#> [1] 610 610
## remove duplicated wavelengths: approxfun <- function(y, wl, new.wl) { approx(wl, y, new.wl, method = "constant", ties = function(x) mean(x, na.rm = TRUE) )$y } merged <- merge(faux_cell [1:7, , 610 ~ 620], faux_cell [5:10, , 615 ~ 625], all = TRUE) merged$.
#> x y region .nx .ny spc.1 spc.2 spc.3 spc.4 spc.5 spc.6 spc.7 #> 1 -11.55 -4.77 matrix 1 NA 15 13 15 NA NA NA NA #> 2 -10.55 -4.77 matrix 2 NA 159 175 160 NA NA NA NA #> 3 -9.55 -4.77 matrix 3 NA 99 115 119 NA NA NA NA #> 4 -8.55 -4.77 matrix 4 NA 39 25 34 NA NA NA NA #> 5 -7.55 -4.77 matrix 5 1 1 2 2 2 2 2 2 #> 6 -6.55 -4.77 matrix 6 2 90 87 105 87 105 100 98 #> 7 -5.55 -4.77 matrix 7 3 79 93 96 93 96 108 103 #> 8 -4.55 -4.77 matrix NA 4 NA NA NA 71 58 66 59 #> 9 -3.55 -4.77 matrix NA 5 NA NA NA 142 130 136 141 #> 10 -2.55 -4.77 matrix NA 6 NA NA NA 164 144 163 153
merged <- apply(merged, 1, approxfun, wl = wl(merged), new.wl = unique(wl(merged)), new.wavelength = "new.wl" ) merged$.
#> x y region .nx .ny spc.1 spc.2 spc.3 spc.4 spc.5 #> 1 -11.55 -4.77 matrix 1 NA 15 13 15 NA NA #> 2 -10.55 -4.77 matrix 2 NA 159 175 160 NA NA #> 3 -9.55 -4.77 matrix 3 NA 99 115 119 NA NA #> 4 -8.55 -4.77 matrix 4 NA 39 25 34 NA NA #> 5 -7.55 -4.77 matrix 5 1 1 2 2 2 2 #> 6 -6.55 -4.77 matrix 6 2 90 87 105 100 98 #> 7 -5.55 -4.77 matrix 7 3 79 93 96 108 103 #> 8 -4.55 -4.77 matrix NA 4 NA 71 58 66 59 #> 9 -3.55 -4.77 matrix NA 5 NA 142 130 136 141 #> 10 -2.55 -4.77 matrix NA 6 NA 164 144 163 153
## merging data.frame into hyperSpec object => hyperSpec object y <- data.frame(filename = sample(flu$filename, 4, replace = TRUE), cpred = 1:4) y
#> filename cpred #> 1 rawdata/flu6.txt 1 #> 2 rawdata/flu3.txt 2 #> 3 rawdata/flu5.txt 3 #> 4 rawdata/flu4.txt 4
tmp <- merge(flu, y) tmp$..
#> filename c cpred #> 1 rawdata/flu3.txt 0.15 2 #> 2 rawdata/flu4.txt 0.20 4 #> 3 rawdata/flu5.txt 0.25 3 #> 4 rawdata/flu6.txt 0.30 1
## merging hyperSpec object into data.frame => data.frame merge(y, flu)
#> filename cpred spc.405 spc.405.5 spc.406 spc.406.5 spc.407 #> 1 rawdata/flu3.txt 2 93.14433 103.06767 106.19367 110.18633 113.24867 #> 2 rawdata/flu4.txt 4 130.66367 139.99833 143.79767 148.42000 152.13267 #> 3 rawdata/flu5.txt 3 167.26667 171.89833 177.47067 184.62467 189.75233 #> 4 rawdata/flu6.txt 1 198.43033 209.45800 215.78500 224.58700 232.52800 #> spc.407.5 spc.408 spc.408.5 spc.409 spc.409.5 spc.410 spc.410.5 #> 1 119.17300 121.31333 124.67533 129.56867 134.11733 139.98667 145.02333 #> 2 159.31033 165.05233 168.68967 175.45900 181.58100 185.69233 190.89667 #> 3 198.11533 205.56267 208.41933 217.55267 224.74633 231.03567 236.35233 #> 4 240.77133 248.04667 256.89133 262.73900 270.27133 281.82867 287.70467 #> spc.411 spc.411.5 spc.412 spc.412.5 spc.413 spc.413.5 spc.414 #> 1 147.10367 150.53400 151.91733 158.18767 162.47900 167.57967 170.99567 #> 2 197.70833 203.64700 209.56433 214.66167 219.65333 225.73100 232.12567 #> 3 244.61233 251.95000 257.34633 265.54500 272.06333 280.74133 288.10567 #> 4 297.23500 307.59333 314.99367 324.56300 330.80033 340.89667 347.62167 #> spc.414.5 spc.415 spc.415.5 spc.416 spc.416.5 spc.417 spc.417.5 #> 1 176.37600 181.19767 181.86833 187.98933 192.12267 194.55600 198.58033 #> 2 236.47700 240.81533 247.19200 252.61167 258.44767 262.91767 269.74967 #> 3 291.88367 300.74000 307.87567 315.42967 321.89600 326.73133 334.01200 #> 4 355.02667 365.62333 374.35700 380.21733 391.40100 398.17300 405.42933 #> spc.418 spc.418.5 spc.419 spc.419.5 spc.420 spc.420.5 spc.421 #> 1 204.04367 208.43300 212.51867 215.81567 219.50867 222.67767 227.59000 #> 2 273.44567 279.34533 283.74367 290.86000 295.65200 300.38267 303.59267 #> 3 339.59933 343.86167 351.25667 357.79733 360.36000 367.96533 374.54900 #> 4 412.48400 421.27300 429.36267 433.35767 443.18733 447.88033 461.33933 #> spc.421.5 spc.422 spc.422.5 spc.423 spc.423.5 spc.424 spc.424.5 #> 1 229.54900 234.28167 236.97767 241.06967 245.41933 249.38867 251.92200 #> 2 307.91433 315.23467 319.20133 322.25767 328.22000 331.54600 337.62033 #> 3 381.76000 389.24100 397.79733 403.78367 408.02300 418.35533 420.61400 #> 4 467.12733 475.18967 481.66667 488.73567 494.01867 504.47333 515.40467 #> spc.425 spc.425.5 spc.426 spc.426.5 spc.427 spc.427.5 spc.428 #> 1 256.41200 261.64400 264.58433 268.46033 274.54733 275.68967 279.99767 #> 2 341.84300 347.36800 352.53033 358.72233 363.61833 369.33300 373.90100 #> 3 427.04333 433.33900 438.43633 449.57667 449.64833 455.43800 463.33200 #> 4 521.23867 522.64633 536.03533 544.41767 551.63333 556.71267 567.61267 #> spc.428.5 spc.429 spc.429.5 spc.430 spc.430.5 spc.431 spc.431.5 #> 1 283.71833 285.21700 290.15667 292.75667 294.99133 299.22533 301.33567 #> 2 377.09633 383.98500 386.89700 393.22067 398.54233 402.31633 405.33067 #> 3 471.47433 476.60033 480.93533 486.53833 491.91000 500.59200 502.12433 #> 4 572.54933 578.72667 587.74900 590.54867 600.42300 608.35800 614.03833 #> spc.432 spc.432.5 spc.433 spc.433.5 spc.434 spc.434.5 spc.435 #> 1 301.46200 309.15933 309.92833 310.05567 312.86500 314.62367 318.42200 #> 2 409.50400 412.68800 418.63200 418.58267 417.95567 420.19100 428.14767 #> 3 508.40733 512.70433 515.62733 522.20267 524.72467 525.20700 529.69967 #> 4 616.95567 621.87267 629.80033 633.44500 637.13233 639.16167 642.89833 #> spc.435.5 spc.436 spc.436.5 spc.437 spc.437.5 spc.438 spc.438.5 #> 1 323.17967 324.57933 325.85133 326.61767 324.63700 326.96700 327.78933 #> 2 428.40300 430.17300 431.36800 434.95500 434.42533 438.43967 439.85467 #> 3 531.71400 537.00600 538.20567 543.83567 544.08533 547.93633 552.15700 #> 4 648.17200 651.67900 654.53967 656.83267 659.77800 662.05633 663.12300 #> spc.439 spc.439.5 spc.440 spc.440.5 spc.441 spc.441.5 spc.442 #> 1 329.90967 328.28167 328.56567 331.69100 330.74933 335.06767 334.62533 #> 2 438.54167 440.27267 438.30833 440.54033 443.27967 445.88900 444.99000 #> 3 550.93500 548.20333 548.14767 550.77700 550.86067 551.78767 553.30400 #> 4 663.48200 670.47567 669.88700 670.06033 671.00800 667.61233 666.30833 #> spc.442.5 spc.443 spc.443.5 spc.444 spc.444.5 spc.445 spc.445.5 #> 1 334.27133 332.64733 333.62633 332.72400 330.82933 332.14667 334.25567 #> 2 446.40533 444.76700 445.82700 446.16433 446.24100 446.36533 442.30900 #> 3 552.57400 553.54533 551.11500 555.87400 554.84800 558.49500 556.92500 #> 4 669.97433 675.68300 675.97867 676.64200 674.73667 671.23433 673.61133 #> spc.446 spc.446.5 spc.447 spc.447.5 spc.448 spc.448.5 spc.449 #> 1 333.93800 332.78600 331.55400 331.97367 334.16567 334.10633 333.85700 #> 2 444.89333 446.92033 448.18633 446.18767 446.09267 448.20600 446.96067 #> 3 559.01233 557.12833 555.68833 553.09200 554.96233 553.30000 555.83300 #> 4 676.73533 677.49467 674.28033 676.64567 674.89067 673.09200 674.89800 #> spc.449.5 spc.450 spc.450.5 spc.451 spc.451.5 spc.452 spc.452.5 #> 1 336.50567 333.77567 334.70633 333.20267 333.36967 332.16600 330.79900 #> 2 446.45833 446.63067 448.45033 448.09967 446.22200 445.86033 443.31967 #> 3 555.17800 556.52367 554.77567 551.07633 553.91567 553.45200 550.76067 #> 4 671.64167 672.53167 672.28033 671.00033 672.87900 674.74300 672.72467 #> spc.453 spc.453.5 spc.454 spc.454.5 spc.455 spc.455.5 spc.456 #> 1 332.47633 332.63667 330.23200 329.06333 327.73533 327.03167 326.89333 #> 2 443.83833 442.67333 440.84300 444.90533 442.43233 438.76567 435.72033 #> 3 551.41767 548.26100 549.39067 553.83833 549.54067 544.38700 544.44033 #> 4 669.79100 672.00433 672.33967 668.74033 662.63333 666.29700 664.63267 #> spc.456.5 spc.457 spc.457.5 spc.458 spc.458.5 spc.459 spc.459.5 #> 1 327.08800 326.73833 324.45733 322.25233 318.61333 319.23467 318.66567 #> 2 436.73867 438.15267 432.70400 433.13733 430.09933 427.13467 421.59067 #> 3 543.01167 539.73833 538.81333 537.68600 532.22333 531.40200 527.58867 #> 4 659.91800 660.13033 656.23733 654.19500 652.31833 647.82100 643.38133 #> spc.460 spc.460.5 spc.461 spc.461.5 spc.462 spc.462.5 spc.463 #> 1 314.18900 313.58533 310.74700 311.77633 306.24567 305.31367 303.65067 #> 2 421.58900 420.52567 417.21033 414.81367 415.02733 407.90400 404.02167 #> 3 526.91933 519.80200 518.62067 515.22900 511.55500 506.87467 503.26400 #> 4 640.79033 632.11967 633.56367 627.81900 623.21133 619.99667 613.55133 #> spc.463.5 spc.464 spc.464.5 spc.465 spc.465.5 spc.466 spc.466.5 #> 1 299.22133 301.25333 296.55200 294.46667 291.52000 288.69967 286.65467 #> 2 401.89667 397.83033 395.28733 391.98100 386.99833 387.28467 379.87933 #> 3 498.34400 494.06167 491.78467 484.60533 486.67233 479.56767 475.90100 #> 4 606.01267 605.92100 599.88400 593.00650 587.40150 586.38900 581.49850 #> spc.467 spc.467.5 spc.468 spc.468.5 spc.469 spc.469.5 spc.470 #> 1 283.82400 282.00933 279.39100 274.85267 271.53567 268.85467 265.92933 #> 2 377.22867 374.34933 368.45633 365.84700 360.04467 359.29733 354.65933 #> 3 472.13100 464.54333 457.90900 455.22167 449.28400 446.34567 442.75167 #> 4 574.15100 566.49250 560.29000 559.46250 550.27850 545.54350 542.76900 #> spc.470.5 spc.471 spc.471.5 spc.472 spc.472.5 spc.473 spc.473.5 #> 1 261.68533 260.63400 258.69100 254.81867 250.93133 247.61267 247.52033 #> 2 350.36133 346.36533 343.82067 341.66833 334.16667 330.03433 328.76800 #> 3 438.25067 432.26667 427.76167 422.73000 416.02133 413.34567 408.62267 #> 4 528.92800 528.12900 519.65350 516.54950 512.45650 502.48600 499.65250 #> spc.474 spc.474.5 spc.475 spc.475.5 spc.476 spc.476.5 spc.477 #> 1 244.78833 241.26000 240.24000 238.41733 233.56600 231.26300 229.50400 #> 2 324.67567 324.62100 318.50667 314.84000 312.79333 309.28467 306.05233 #> 3 403.80533 400.32500 393.77333 393.78567 386.53867 383.86933 378.65500 #> 4 495.00100 486.49700 481.73950 476.47200 472.37900 469.25450 466.99550 #> spc.477.5 spc.478 spc.478.5 spc.479 spc.479.5 spc.480 spc.480.5 #> 1 228.82800 225.52900 221.65767 218.32400 216.50633 213.21500 215.24567 #> 2 304.24467 299.30133 295.51333 292.25100 290.55800 286.98100 285.83633 #> 3 373.64800 372.70233 368.87433 366.15533 362.89967 357.86333 354.96700 #> 4 457.76300 452.32300 450.48350 445.14500 444.27400 435.22800 430.33300 #> spc.481 spc.481.5 spc.482 spc.482.5 spc.483 spc.483.5 spc.484 #> 1 208.54933 208.86967 210.30900 205.67467 203.98400 200.07500 197.68767 #> 2 283.13467 276.48133 275.80333 272.57200 270.89733 266.44633 265.42300 #> 3 352.37067 344.31567 342.10133 339.01433 336.71533 334.59300 326.09833 #> 4 424.72000 420.74800 418.46650 414.01750 408.32400 403.85400 405.00450 #> spc.484.5 spc.485 spc.485.5 spc.486 spc.486.5 spc.487 spc.487.5 #> 1 197.21600 193.02933 191.21133 190.76267 186.57933 183.09267 182.39100 #> 2 262.04033 259.87533 256.72533 250.20833 249.26700 245.61367 240.69333 #> 3 324.69400 320.56700 315.93067 312.73200 307.94367 306.38633 298.34667 #> 4 394.33500 389.97750 384.49100 382.33700 375.08200 369.09900 366.06300 #> spc.488 spc.488.5 spc.489 spc.489.5 spc.490 spc.490.5 spc.491 #> 1 179.87400 177.13400 176.43600 173.72833 170.26000 168.65033 166.09067 #> 2 240.84900 233.99533 233.73500 230.80467 228.26600 223.37333 221.35733 #> 3 298.57433 296.05233 292.49933 285.46933 282.62833 279.92767 273.34667 #> 4 362.45350 358.99650 352.14300 348.20000 347.10650 338.79150 335.34700 #> spc.491.5 spc.492 spc.492.5 spc.493 spc.493.5 spc.494 spc.494.5 #> 1 164.44633 160.81267 158.91100 156.76133 154.92067 149.53900 148.52667 #> 2 218.90167 214.44667 210.01767 206.01533 204.57500 201.48433 198.86733 #> 3 267.55433 266.10100 261.76567 259.40200 256.20067 252.06567 248.06700 #> 4 330.04000 325.16200 322.06850 315.64550 312.32950 307.51850 302.32550 #> spc.495 c #> 1 145.79333 0.15 #> 2 195.86733 0.20 #> 3 246.95200 0.25 #> 4 294.64950 0.30