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 wl_sort().

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.

merge_data()

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/flu2.txt     1
#> 2 rawdata/flu3.txt     2
#> 3 rawdata/flu1.txt     3
#> 4 rawdata/flu2.txt     4

tmp <- merge(flu, y)
tmp$..
#>           filename    c cpred
#> 1 rawdata/flu1.txt 0.05     3
#> 2 rawdata/flu2.txt 0.10     1
#> 3 rawdata/flu2.txt 0.10     4
#> 4 rawdata/flu3.txt 0.15     2

## 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/flu1.txt     3  27.15000  32.34467  33.37867  34.41933  36.53133
#> 2 rawdata/flu2.txt     1  66.80133  63.71533  66.71200  69.58233  72.52967
#> 3 rawdata/flu2.txt     4  66.80133  63.71533  66.71200  69.58233  72.52967
#> 4 rawdata/flu3.txt     2  93.14433 103.06767 106.19367 110.18633 113.24867
#>   spc.407.5   spc.408 spc.408.5   spc.409 spc.409.5   spc.410 spc.410.5
#> 1  37.64767  38.13700  39.17700  40.73567  41.38133  44.25133  44.12633
#> 2  74.55833  77.04800  80.25967  82.53867  84.49167  88.15167  91.08533
#> 3  74.55833  77.04800  80.25967  82.53867  84.49167  88.15167  91.08533
#> 4 119.17300 121.31333 124.67533 129.56867 134.11733 139.98667 145.02333
#>     spc.411 spc.411.5   spc.412 spc.412.5   spc.413 spc.413.5   spc.414
#> 1  46.98067  49.08167  50.27433  50.10967  52.23167  53.03967  54.51900
#> 2  95.37233  95.53033  98.99533 101.03433 103.55800 107.02667 109.54533
#> 3  95.37233  95.53033  98.99533 101.03433 103.55800 107.02667 109.54533
#> 4 147.10367 150.53400 151.91733 158.18767 162.47900 167.57967 170.99567
#>   spc.414.5   spc.415 spc.415.5   spc.416 spc.416.5   spc.417 spc.417.5
#> 1  56.22033  57.71933  59.51433  58.74500  60.09467  61.84067  62.16867
#> 2 111.67233 113.98733 117.03267 119.83500 123.49433 123.32367 127.00167
#> 3 111.67233 113.98733 117.03267 119.83500 123.49433 123.32367 127.00167
#> 4 176.37600 181.19767 181.86833 187.98933 192.12267 194.55600 198.58033
#>     spc.418 spc.418.5   spc.419 spc.419.5   spc.420 spc.420.5   spc.421
#> 1  66.04933  65.24500  65.79900  66.94333  69.13600  70.92467  73.83400
#> 2 130.68067 132.35800 136.16467 138.39933 140.22433 141.04333 143.13133
#> 3 130.68067 132.35800 136.16467 138.39933 140.22433 141.04333 143.13133
#> 4 204.04367 208.43300 212.51867 215.81567 219.50867 222.67767 227.59000
#>   spc.421.5   spc.422 spc.422.5   spc.423 spc.423.5   spc.424 spc.424.5
#> 1  74.38567  74.17333  75.53067  76.42967  76.13200  77.89067  79.36867
#> 2 147.17967 150.01200 153.69333 155.72800 158.47200 159.50067 160.81567
#> 3 147.17967 150.01200 153.69333 155.72800 158.47200 159.50067 160.81567
#> 4 229.54900 234.28167 236.97767 241.06967 245.41933 249.38867 251.92200
#>     spc.425 spc.425.5   spc.426 spc.426.5   spc.427 spc.427.5   spc.428
#> 1  79.42733  82.02833  83.87833  83.81367  85.50667  86.50233  88.93733
#> 2 163.90967 166.25500 169.59233 170.82833 172.08767 175.21133 178.47067
#> 3 163.90967 166.25500 169.59233 170.82833 172.08767 175.21133 178.47067
#> 4 256.41200 261.64400 264.58433 268.46033 274.54733 275.68967 279.99767
#>   spc.428.5   spc.429 spc.429.5   spc.430 spc.430.5   spc.431 spc.431.5
#> 1  88.99533  89.51500  90.55667  91.70633  93.57900  94.01300  94.21033
#> 2 179.68100 181.82867 184.42933 187.75600 187.93767 191.05700 192.50900
#> 3 179.68100 181.82867 184.42933 187.75600 187.93767 191.05700 192.50900
#> 4 283.71833 285.21700 290.15667 292.75667 294.99133 299.22533 301.33567
#>     spc.432 spc.432.5   spc.433 spc.433.5   spc.434 spc.434.5   spc.435
#> 1  96.44200  96.62733  98.96233  98.84767 100.56833 100.44933  99.91600
#> 2 192.80100 195.55600 197.89067 198.78200 200.99467 201.52233 200.20600
#> 3 192.80100 195.55600 197.89067 198.78200 200.99467 201.52233 200.20600
#> 4 301.46200 309.15933 309.92833 310.05567 312.86500 314.62367 318.42200
#>   spc.435.5   spc.436 spc.436.5   spc.437 spc.437.5   spc.438 spc.438.5
#> 1  99.71900 100.88633 101.44533 102.64267 104.22967 103.11133 104.75800
#> 2 204.13200 204.90433 203.98833 205.75600 204.81700 206.09100 208.60733
#> 3 204.13200 204.90433 203.98833 205.75600 204.81700 206.09100 208.60733
#> 4 323.17967 324.57933 325.85133 326.61767 324.63700 326.96700 327.78933
#>     spc.439 spc.439.5   spc.440 spc.440.5   spc.441 spc.441.5   spc.442
#> 1 103.15833 103.94267 105.37100 105.86333 103.67967 104.09000 104.65400
#> 2 210.79667 210.58400 210.19033 210.54233 209.85933 212.82733 212.52067
#> 3 210.79667 210.58400 210.19033 210.54233 209.85933 212.82733 212.52067
#> 4 329.90967 328.28167 328.56567 331.69100 330.74933 335.06767 334.62533
#>   spc.442.5   spc.443 spc.443.5   spc.444 spc.444.5   spc.445 spc.445.5
#> 1 105.22633 104.80433 105.81033 104.75800 103.88300 105.36167 105.01500
#> 2 212.41967 211.22233 215.56367 213.42467 211.15933 212.97533 213.98933
#> 3 212.41967 211.22233 215.56367 213.42467 211.15933 212.97533 213.98933
#> 4 334.27133 332.64733 333.62633 332.72400 330.82933 332.14667 334.25567
#>     spc.446 spc.446.5   spc.447 spc.447.5   spc.448 spc.448.5   spc.449
#> 1 105.05933 105.34933 103.98700 105.43867 104.19667 105.09800 104.72333
#> 2 214.17200 213.47633 212.48867 211.74100 212.70467 212.94033 212.12933
#> 3 214.17200 213.47633 212.48867 211.74100 212.70467 212.94033 212.12933
#> 4 333.93800 332.78600 331.55400 331.97367 334.16567 334.10633 333.85700
#>   spc.449.5   spc.450 spc.450.5   spc.451 spc.451.5   spc.452 spc.452.5
#> 1 106.66733 106.94967 104.75467 105.08267 105.30000 105.21300 104.78100
#> 2 213.77267 213.49733 213.46400 213.17067 212.83600 211.96333 208.79867
#> 3 213.77267 213.49733 213.46400 213.17067 212.83600 211.96333 208.79867
#> 4 336.50567 333.77567 334.70633 333.20267 333.36967 332.16600 330.79900
#>     spc.453 spc.453.5   spc.454 spc.454.5   spc.455 spc.455.5   spc.456
#> 1 104.53867 105.13267 105.16967 104.05700 106.38533 104.07967 104.40133
#> 2 211.50633 209.47700 211.85967 213.26200 212.28367 211.77333 209.39067
#> 3 211.50633 209.47700 211.85967 213.26200 212.28367 211.77333 209.39067
#> 4 332.47633 332.63667 330.23200 329.06333 327.73533 327.03167 326.89333
#>   spc.456.5   spc.457 spc.457.5   spc.458 spc.458.5   spc.459 spc.459.5
#> 1 102.18133 103.44167 101.79700 102.87167 102.38867 100.41900 101.16167
#> 2 208.85600 208.34033 206.50633 206.77733 206.64500 205.25500 201.74700
#> 3 208.85600 208.34033 206.50633 206.77733 206.64500 205.25500 201.74700
#> 4 327.08800 326.73833 324.45733 322.25233 318.61333 319.23467 318.66567
#>     spc.460 spc.460.5   spc.461 spc.461.5   spc.462 spc.462.5   spc.463
#> 1  98.61133  98.42933  98.57600  98.34100  98.46733  95.14900  94.71100
#> 2 201.98800 199.57000 200.55100 198.76767 197.03300 194.14467 193.38300
#> 3 201.98800 199.57000 200.55100 198.76767 197.03300 194.14467 193.38300
#> 4 314.18900 313.58533 310.74700 311.77633 306.24567 305.31367 303.65067
#>   spc.463.5   spc.464 spc.464.5   spc.465 spc.465.5   spc.466 spc.466.5
#> 1  95.27433  94.88433  93.62133  92.46567  92.11533  91.30867  89.53900
#> 2 193.66467 192.51267 190.63300 186.45567 186.05433 182.30533 182.33367
#> 3 193.66467 192.51267 190.63300 186.45567 186.05433 182.30533 182.33367
#> 4 299.22133 301.25333 296.55200 294.46667 291.52000 288.69967 286.65467
#>     spc.467 spc.467.5   spc.468 spc.468.5   spc.469 spc.469.5   spc.470
#> 1  88.28100  86.13633  86.16200  86.81733  85.95767  84.06700  86.21200
#> 2 180.56933 180.50633 180.21267 175.21100 171.65000 170.41667 170.37400
#> 3 180.56933 180.50633 180.21267 175.21100 171.65000 170.41667 170.37400
#> 4 283.82400 282.00933 279.39100 274.85267 271.53567 268.85467 265.92933
#>   spc.470.5   spc.471 spc.471.5   spc.472 spc.472.5   spc.473 spc.473.5
#> 1  83.68333  82.56867  82.70533  81.26033  78.75233  76.67533  77.95300
#> 2 169.40700 164.89000 164.14133 163.17333 160.98933 159.66533 157.74633
#> 3 169.40700 164.89000 164.14133 163.17333 160.98933 159.66533 157.74633
#> 4 261.68533 260.63400 258.69100 254.81867 250.93133 247.61267 247.52033
#>     spc.474 spc.474.5   spc.475 spc.475.5   spc.476 spc.476.5   spc.477
#> 1  77.50700  76.16400  75.64600  76.43700  74.57033  72.87900  72.80267
#> 2 155.13467 153.23933 151.52300 149.19667 147.49467 147.49000 145.43367
#> 3 155.13467 153.23933 151.52300 149.19667 147.49467 147.49000 145.43367
#> 4 244.78833 241.26000 240.24000 238.41733 233.56600 231.26300 229.50400
#>   spc.477.5   spc.478 spc.478.5   spc.479 spc.479.5   spc.480 spc.480.5
#> 1  71.05200  70.21367  69.60533  69.98967  68.78000  68.34233  67.68467
#> 2 144.81900 142.43867 142.68967 142.88100 139.83767 135.47933 135.25267
#> 3 144.81900 142.43867 142.68967 142.88100 139.83767 135.47933 135.25267
#> 4 228.82800 225.52900 221.65767 218.32400 216.50633 213.21500 215.24567
#>     spc.481 spc.481.5   spc.482 spc.482.5   spc.483 spc.483.5   spc.484
#> 1  67.27700  67.04833  65.31300  64.50900  63.50567  62.18367  62.04500
#> 2 135.84567 133.55900 133.31767 132.82400 128.93033 125.20300 124.73967
#> 3 135.84567 133.55900 133.31767 132.82400 128.93033 125.20300 124.73967
#> 4 208.54933 208.86967 210.30900 205.67467 203.98400 200.07500 197.68767
#>   spc.484.5   spc.485 spc.485.5   spc.486 spc.486.5   spc.487 spc.487.5
#> 1  62.02733  61.79900  60.52800  59.34200  59.12500  57.74800  57.44667
#> 2 124.71667 121.67833 120.90167 121.40600 117.66767 117.04100 115.94100
#> 3 124.71667 121.67833 120.90167 121.40600 117.66767 117.04100 115.94100
#> 4 197.21600 193.02933 191.21133 190.76267 186.57933 183.09267 182.39100
#>     spc.488 spc.488.5   spc.489 spc.489.5   spc.490 spc.490.5   spc.491
#> 1  57.74267  56.27500  55.49233  54.40933  53.83267  53.26333  52.45733
#> 2 112.22900 112.72433 111.67267 109.69833 107.64067 108.05067 105.22000
#> 3 112.22900 112.72433 111.67267 109.69833 107.64067 108.05067 105.22000
#> 4 179.87400 177.13400 176.43600 173.72833 170.26000 168.65033 166.09067
#>   spc.491.5   spc.492 spc.492.5   spc.493 spc.493.5   spc.494 spc.494.5
#> 1  52.14000  49.78433  49.62300  48.33800  47.30400  47.16267  46.41233
#> 2 102.95300 102.75833 100.62800  97.96500  97.35333  96.60167  96.20600
#> 3 102.95300 102.75833 100.62800  97.96500  97.35333  96.60167  96.20600
#> 4 164.44633 160.81267 158.91100 156.76133 154.92067 149.53900 148.52667
#>     spc.495    c
#> 1  45.25633 0.05
#> 2  94.61033 0.10
#> 3  94.61033 0.10
#> 4 145.79333 0.15