Home > Back-end >  How to convert dataframe (or vector) to matrix to use R baseline package for data other than the sam
How to convert dataframe (or vector) to matrix to use R baseline package for data other than the sam

Time:12-22

I have data in the form of a dataframe of x and y values that when plotted looks like this:

basic plot

(The portion highlighted in green corresponds to the dput() data below).

I want to use the baseline package to experiment with the different baseline correction algorithms described in "Liland, K. H., Almøy, T., & Mevik, B.-H. (2010). Optimal Choice of Baseline Correction for Multivariate Calibration of Spectra. Applied Spectroscopy, 64(9), 1007–1016. doi:10.1366/000370210792434350".

The package specifies that "data must be organized as row vectors in a matrix or data.frame", however, simply converting my y values to a matrix doesn't work:

> baseline(as.matrix(y, byrow=T))
Error in rep(3, p - 4) : invalid 'times' argument

The example data that is provided with the package has a structure that I can't interpret or figure out how to emulate with my own data.

> library(baseline)
> data(milk)
> str(milk)
'data.frame':   45 obs. of  2 variables:
 $ cow    : num  0 0.25 0.375 0.875 0.5 0.75 0.5 0.125 0 0.125 ...
 $ spectra: num [1:45, 1:21451] 1029 371 606 368 554 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:21451] "4999.94078628963" "5001.55954267662" "5003.17856106153" "5004.79784144435" ...
 - attr(*, "terms")=Classes 'terms', 'formula'  language cow ~ spectra
  .. ..- attr(*, "variables")= language list(cow, spectra)
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "cow" "spectra"
  .. .. .. ..$ : chr "spectra"
  .. ..- attr(*, "term.labels")= chr "spectra"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(cow, spectra)
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "nmatrix.21451"
  .. .. ..- attr(*, "names")= chr [1:2] "cow" "spectra"

If anyone can tell me how I can call baseline() on my own data, I would greatly appreciate it.

Here is a subsection of the data I'm working with, corresponding to the green highlighted portion of the plot above:

    > dput(fid_df[fid_df$rt>12.5&fid_df$rt<12.8,])
structure(list(rt = c(12.5002866914485, 12.5011200182712, 12.5019533450939, 
12.5027866719166, 12.5036199987393, 12.5044533255619, 12.5052866523846, 
12.5061199792073, 12.50695330603, 12.5077866328527, 12.5086199596754, 
12.5094532864981, 12.5102866133207, 12.5111199401434, 12.5119532669661, 
12.5127865937888, 12.5136199206115, 12.5144532474342, 12.5152865742568, 
12.5161199010795, 12.5169532279022, 12.5177865547249, 12.5186198815476, 
12.5194532083703, 12.5202865351929, 12.5211198620156, 12.5219531888383, 
12.522786515661, 12.5236198424837, 12.5244531693064, 12.5252864961291, 
12.5261198229517, 12.5269531497744, 12.5277864765971, 12.5286198034198, 
12.5294531302425, 12.5302864570652, 12.5311197838878, 12.5319531107105, 
12.5327864375332, 12.5336197643559, 12.5344530911786, 12.5352864180013, 
12.536119744824, 12.5369530716466, 12.5377863984693, 12.538619725292, 
12.5394530521147, 12.5402863789374, 12.5411197057601, 12.5419530325827, 
12.5427863594054, 12.5436196862281, 12.5444530130508, 12.5452863398735, 
12.5461196666962, 12.5469529935189, 12.5477863203415, 12.5486196471642, 
12.5494529739869, 12.5502863008096, 12.5511196276323, 12.551952954455, 
12.5527862812776, 12.5536196081003, 12.554452934923, 12.5552862617457, 
12.5561195885684, 12.5569529153911, 12.5577862422138, 12.5586195690364, 
12.5594528958591, 12.5602862226818, 12.5611195495045, 12.5619528763272, 
12.5627862031499, 12.5636195299725, 12.5644528567952, 12.5652861836179, 
12.5661195104406, 12.5669528372633, 12.567786164086, 12.5686194909087, 
12.5694528177313, 12.570286144554, 12.5711194713767, 12.5719527981994, 
12.5727861250221, 12.5736194518448, 12.5744527786674, 12.5752861054901, 
12.5761194323128, 12.5769527591355, 12.5777860859582, 12.5786194127809, 
12.5794527396035, 12.5802860664262, 12.5811193932489, 12.5819527200716, 
12.5827860468943, 12.583619373717, 12.5844527005397, 12.5852860273623, 
12.586119354185, 12.5869526810077, 12.5877860078304, 12.5886193346531, 
12.5894526614758, 12.5902859882984, 12.5911193151211, 12.5919526419438, 
12.5927859687665, 12.5936192955892, 12.5944526224119, 12.5952859492346, 
12.5961192760572, 12.5969526028799, 12.5977859297026, 12.5986192565253, 
12.599452583348, 12.6002859101707, 12.6011192369933, 12.601952563816, 
12.6027858906387, 12.6036192174614, 12.6044525442841, 12.6052858711068, 
12.6061191979295, 12.6069525247521, 12.6077858515748, 12.6086191783975, 
12.6094525052202, 12.6102858320429, 12.6111191588656, 12.6119524856882, 
12.6127858125109, 12.6136191393336, 12.6144524661563, 12.615285792979, 
12.6161191198017, 12.6169524466243, 12.617785773447, 12.6186191002697, 
12.6194524270924, 12.6202857539151, 12.6211190807378, 12.6219524075605, 
12.6227857343831, 12.6236190612058, 12.6244523880285, 12.6252857148512, 
12.6261190416739, 12.6269523684966, 12.6277856953192, 12.6286190221419, 
12.6294523489646, 12.6302856757873, 12.63111900261, 12.6319523294327, 
12.6327856562554, 12.633618983078, 12.6344523099007, 12.6352856367234, 
12.6361189635461, 12.6369522903688, 12.6377856171915, 12.6386189440141, 
12.6394522708368, 12.6402855976595, 12.6411189244822, 12.6419522513049, 
12.6427855781276, 12.6436189049503, 12.6444522317729, 12.6452855585956, 
12.6461188854183, 12.646952212241, 12.6477855390637, 12.6486188658864, 
12.649452192709, 12.6502855195317, 12.6511188463544, 12.6519521731771, 
12.6527854999998, 12.6536188268225, 12.6544521536452, 12.6552854804678, 
12.6561188072905, 12.6569521341132, 12.6577854609359, 12.6586187877586, 
12.6594521145813, 12.6602854414039, 12.6611187682266, 12.6619520950493, 
12.662785421872, 12.6636187486947, 12.6644520755174, 12.6652854023401, 
12.6661187291627, 12.6669520559854, 12.6677853828081, 12.6686187096308, 
12.6694520364535, 12.6702853632762, 12.6711186900988, 12.6719520169215, 
12.6727853437442, 12.6736186705669, 12.6744519973896, 12.6752853242123, 
12.6761186510349, 12.6769519778576, 12.6777853046803, 12.678618631503, 
12.6794519583257, 12.6802852851484, 12.6811186119711, 12.6819519387937, 
12.6827852656164, 12.6836185924391, 12.6844519192618, 12.6852852460845, 
12.6861185729072, 12.6869518997298, 12.6877852265525, 12.6886185533752, 
12.6894518801979, 12.6902852070206, 12.6911185338433, 12.691951860666, 
12.6927851874886, 12.6936185143113, 12.694451841134, 12.6952851679567, 
12.6961184947794, 12.6969518216021, 12.6977851484247, 12.6986184752474, 
12.6994518020701, 12.7002851288928, 12.7011184557155, 12.7019517825382, 
12.7027851093609, 12.7036184361835, 12.7044517630062, 12.7052850898289, 
12.7061184166516, 12.7069517434743, 12.707785070297, 12.7086183971196, 
12.7094517239423, 12.710285050765, 12.7111183775877, 12.7119517044104, 
12.7127850312331, 12.7136183580558, 12.7144516848784, 12.7152850117011, 
12.7161183385238, 12.7169516653465, 12.7177849921692, 12.7186183189919, 
12.7194516458145, 12.7202849726372, 12.7211182994599, 12.7219516262826, 
12.7227849531053, 12.723618279928, 12.7244516067506, 12.7252849335733, 
12.726118260396, 12.7269515872187, 12.7277849140414, 12.7286182408641, 
12.7294515676868, 12.7302848945094, 12.7311182213321, 12.7319515481548, 
12.7327848749775, 12.7336182018002, 12.7344515286229, 12.7352848554455, 
12.7361181822682, 12.7369515090909, 12.7377848359136, 12.7386181627363, 
12.739451489559, 12.7402848163817, 12.7411181432043, 12.741951470027, 
12.7427847968497, 12.7436181236724, 12.7444514504951, 12.7452847773178, 
12.7461181041404, 12.7469514309631, 12.7477847577858, 12.7486180846085, 
12.7494514114312, 12.7502847382539, 12.7511180650766, 12.7519513918992, 
12.7527847187219, 12.7536180455446, 12.7544513723673, 12.75528469919, 
12.7561180260127, 12.7569513528353, 12.757784679658, 12.7586180064807, 
12.7594513333034, 12.7602846601261, 12.7611179869488, 12.7619513137715, 
12.7627846405941, 12.7636179674168, 12.7644512942395, 12.7652846210622, 
12.7661179478849, 12.7669512747076, 12.7677846015302, 12.7686179283529, 
12.7694512551756, 12.7702845819983, 12.771117908821, 12.7719512356437, 
12.7727845624663, 12.773617889289, 12.7744512161117, 12.7752845429344, 
12.7761178697571, 12.7769511965798, 12.7777845234025, 12.7786178502251, 
12.7794511770478, 12.7802845038705, 12.7811178306932, 12.7819511575159, 
12.7827844843386, 12.7836178111612, 12.7844511379839, 12.7852844648066, 
12.7861177916293, 12.786951118452, 12.7877844452747, 12.7886177720974, 
12.78945109892, 12.7902844257427, 12.7911177525654, 12.7919510793881, 
12.7927844062108, 12.7936177330335, 12.7944510598561, 12.7952843866788, 
12.7961177135015, 12.7969510403242, 12.7977843671469, 12.7986176939696, 
12.7994510207923), value = c(17.3893229166667, 17.394140625, 
17.3994791666667, 17.3966145833333, 17.397265625, 17.4009114583333, 
17.4036458333333, 17.400390625, 17.3940104166667, 17.3908854166667, 
17.3873697916667, 17.3858072916667, 17.3841145833333, 17.3834635416667, 
17.383984375, 17.3837239583333, 17.3838541666667, 17.3850260416667, 
17.3904947916667, 17.3954427083333, 17.39375, 17.3897135416667, 
17.387109375, 17.3890625, 17.3876302083333, 17.385546875, 17.3850260416667, 
17.390234375, 17.39921875, 17.4076822916667, 17.4131510416667, 
17.41484375, 17.4111979166667, 17.402734375, 17.3953125, 17.3915364583333, 
17.3934895833333, 17.3923177083333, 17.3938802083333, 17.3967447916667, 
17.398828125, 17.4, 17.396484375, 17.4, 17.39921875, 17.4032552083333, 
17.4138020833333, 17.4260416666667, 17.4364583333333, 17.4458333333333, 
17.4639322916667, 17.491015625, 17.5239583333333, 17.5647135416667, 
17.6229166666667, 17.6997395833333, 17.7994791666667, 17.916015625, 
18.0544270833333, 18.2184895833333, 18.4170572916667, 18.6494791666667, 
18.914453125, 19.2151041666667, 19.5665364583333, 19.9688802083333, 
20.4143229166667, 20.8876302083333, 21.3850260416667, 21.9104166666667, 
22.4467447916667, 23.0069010416667, 23.5716145833333, 24.1360677083333, 
24.6528645833333, 25.1217447916667, 25.5454427083333, 25.9471354166667, 
26.2817708333333, 26.5583333333333, 26.7454427083333, 26.8537760416667, 
26.9045572916667, 26.889453125, 26.82734375, 26.6755208333333, 
26.442578125, 26.1438802083333, 25.7912760416667, 25.437109375, 
25.0861979166667, 24.7126302083333, 24.262890625, 23.73359375, 
23.1990885416667, 22.7178385416667, 22.28359375, 21.8391927083333, 
21.3856770833333, 20.9497395833333, 20.56640625, 20.2286458333333, 
19.9221354166667, 19.6524739583333, 19.3998697916667, 19.1759114583333, 
18.9670572916667, 18.7893229166667, 18.640625, 18.5109375, 18.401171875, 
18.3026041666667, 18.2216145833333, 18.1454427083333, 18.0764322916667, 
18.0162760416667, 17.9623697916667, 17.9102864583333, 17.8688802083333, 
17.8309895833333, 17.8029947916667, 17.7690104166667, 17.7415364583333, 
17.7149739583333, 17.693359375, 17.6713541666667, 17.6526041666667, 
17.631640625, 17.6157552083333, 17.6013020833333, 17.5877604166667, 
17.5740885416667, 17.5670572916667, 17.55703125, 17.545703125, 
17.5307291666667, 17.527734375, 17.5272135416667, 17.5216145833333, 
17.5140625, 17.5040364583333, 17.4944010416667, 17.4796875, 17.469921875, 
17.4647135416667, 17.4657552083333, 17.46640625, 17.4638020833333, 
17.4580729166667, 17.4485677083333, 17.4446614583333, 17.4430989583333, 
17.4475260416667, 17.4428385416667, 17.4401041666667, 17.433984375, 
17.4352864583333, 17.4342447916667, 17.4309895833333, 17.42890625, 
17.4265625, 17.4248697916667, 17.42109375, 17.4109375, 17.4052083333333, 
17.403125, 17.4092447916667, 17.413671875, 17.4153645833333, 
17.4123697916667, 17.4123697916667, 17.4091145833333, 17.4052083333333, 
17.401953125, 17.4052083333333, 17.40703125, 17.410546875, 17.4084635416667, 
17.4096354166667, 17.4084635416667, 17.4135416666667, 17.4209635416667, 
17.42265625, 17.42265625, 17.4223958333333, 17.4248697916667, 
17.418359375, 17.4127604166667, 17.4125, 17.4184895833333, 17.4180989583333, 
17.4111979166667, 17.4076822916667, 17.4158854166667, 17.425390625, 
17.4279947916667, 17.424609375, 17.4229166666667, 17.4147135416667, 
17.40625, 17.4014322916667, 17.4071614583333, 17.4125, 17.416796875, 
17.415234375, 17.4084635416667, 17.402734375, 17.4018229166667, 
17.4032552083333, 17.4009114583333, 17.40078125, 17.40078125, 
17.4015625, 17.4002604166667, 17.4006510416667, 17.4001302083333, 
17.398046875, 17.399609375, 17.4014322916667, 17.4, 17.39765625, 
17.394140625, 17.3958333333333, 17.3985677083333, 17.399609375, 
17.3979166666667, 17.3924479166667, 17.3860677083333, 17.3852864583333, 
17.3834635416667, 17.3805989583333, 17.3751302083333, 17.3782552083333, 
17.3834635416667, 17.3848958333333, 17.3802083333333, 17.3809895833333, 
17.3842447916667, 17.3846354166667, 17.3779947916667, 17.3703125, 
17.372265625, 17.3786458333333, 17.3860677083333, 17.38203125, 
17.3813802083333, 17.38203125, 17.3881510416667, 17.38984375, 
17.394921875, 17.3963541666667, 17.3998697916667, 17.3963541666667, 
17.3932291666667, 17.3854166666667, 17.3725260416667, 17.3647135416667, 
17.36328125, 17.372265625, 17.3756510416667, 17.380078125, 17.3766927083333, 
17.378515625, 17.37578125, 17.382421875, 17.38359375, 17.379296875, 
17.3759114583333, 17.37578125, 17.3803385416667, 17.378515625, 
17.3837239583333, 17.3829427083333, 17.38359375, 17.3768229166667, 
17.3790364583333, 17.380078125, 17.3846354166667, 17.3837239583333, 
17.3869791666667, 17.3813802083333, 17.3889322916667, 17.388671875, 
17.39375, 17.3911458333333, 17.3891927083333, 17.3889322916667, 
17.386328125, 17.3830729166667, 17.3783854166667, 17.3748697916667, 
17.3796875, 17.3830729166667, 17.383203125, 17.3755208333333, 
17.37421875, 17.371875, 17.3735677083333, 17.37109375, 17.373046875, 
17.3774739583333, 17.3782552083333, 17.3748697916667, 17.3764322916667, 
17.3807291666667, 17.3876302083333, 17.3846354166667, 17.3802083333333, 
17.376171875, 17.3727864583333, 17.3735677083333, 17.3796875, 
17.3864583333333, 17.3876302083333, 17.3845052083333, 17.3850260416667, 
17.3880208333333, 17.3891927083333, 17.3880208333333, 17.3830729166667, 
17.3782552083333, 17.3765625, 17.379296875, 17.3829427083333, 
17.3869791666667, 17.3881510416667, 17.384765625, 17.3763020833333, 
17.376953125, 17.3786458333333, 17.3817708333333, 17.3821614583333, 
17.3859375, 17.3876302083333, 17.3837239583333, 17.381640625, 
17.37890625, 17.3802083333333, 17.3825520833333, 17.3881510416667, 
17.3888020833333, 17.3864583333333, 17.3825520833333, 17.3834635416667, 
17.386328125, 17.3854166666667, 17.384375, 17.3852864583333, 
17.390234375, 17.3916666666667, 17.38828125, 17.3891927083333, 
17.3901041666667, 17.3950520833333, 17.3963541666667, 17.3970052083333, 
17.3907552083333, 17.3846354166667, 17.3760416666667, 17.3743489583333
)), row.names = 15002:15361, class = "data.frame")

(p.s the question here: R-package(baseline) application to sample dataset seems to have a similar problem, but doesn't address how to turn simple x,y data into the format required by the baseline package).

CodePudding user response:

The baseline package expects spectra to appear in rows, but your code creates a matrix with the spectrum in a single column. If you change the conversion from data.frame to matrix to the following it should work:

baseline(matrix(y, nrow=1))

The problem was caused by two things, using as.matrix(), which does not take the arguments you need, and using byrow=T instead of nrow=1. byrow controls the direction in which the matrix is filled, either by rows or by columns, but has no effect on the output dimensions.

The extra fuzz in the milk data is not helping here, so I will remove it in the next package update. I will also look into the possibility of allowing single spectra to be handled in vector format instead of explicitly forcing matrix input.

P.S. I was able to copy out your data with a tiny extra effort, but suspect that you need the raw data to directly copy-paste from this page as the dput line contains som subsetting.

  • Related