Sorting the "Sample Sets" by constituents

[This article was first published on NIR-Quimiometría, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


I use to see the videos from:
http://www.twotorials.com/
and the video:
How to order and sort stuff in R
is really useful to apply this concept to organize and understand better our sample sets before to proceed to develop a calibration.
The idea of this post is after watching the video to create a new dataframe sorted by the “Moisture” constituent in an ascending order.
After that we can subtract the spectra with the highest moisture value from the spectra with the lowest moisture value. This way we can study he difference spectrum in order to get some conclusions about the band positions for moisture, and other constituents.
Let´s start with the “demoNIR_msc” data frame.
As we can see in the video, we can use the functions:
sort:
> sort(demoNIR_msc$Moisture)
 [1] 3.98 5.05 5.20 5.32 5.34 5.41 5.51 5.53 5.57 5.63 5.64 5.67 5.71 5.73 5.77
[16] 5.82 5.85 5.85 5.86 5.87 5.89 5.90 5.91 5.99 5.99 6.04 6.05 6.09 6.10 6.14
[31] 6.16 6.20 6.22 6.23 6.31 6.33 6.33 6.34 6.36 6.38 6.40 6.43 6.47 6.56 6.56
[46] 6.57 6.59 6.61 6.66 6.66 6.69 6.73 6.73 6.84 6.88 6.95 7.07 7.10 7.12 7.30
[61] 7.42 7.43 7.48 8.00 8.12 8.17
order:
> order(demoNIR_msc$Moisture)
 [1]  4 12 15 18 29 14 17 20 36 38 66 37 39 30 63 13 16 33 65  7  3 28  5 10 25
[26] 32  6 56 64 11 21 31 42  8  2 48 49 62 41 58 26 24 59 27 55  9 34 22 54 61
[51]  1 23 46 35 19 47 50 45 51 40 52 53 60 57 44 43
Now we prepare a new dataframe, sorted by moisture values in ascending order:
> moiNIR_msc<-demoNIR_msc[order(demoNIR_msc$Moisture),]
> moiNIR_msc[,1:5]
   Protein   Fat   Ash    DM Moisture
4    74.51 10.51 14.88 96.02     3.98
12   70.24 10.73 18.61 94.95     5.05
15   71.17 12.14 16.26  94.8      5.2
18   71.29 12.08 15.78 94.68     5.32
29   71.71 10.94 16.72 94.66     5.34
14    72.2 11.73 15.64 94.59     5.41
17   70.97 12.82  16.1 94.49     5.51
20   68.95 12.53 16.42 94.47     5.53
36      76 11.37 11.68 94.43     5.57
38   64.56  9.46 25.55 94.37     5.63
66    73.4  9.15 16.82 94.36     5.64
37   78.06  11.5 10.86 94.33     5.67
39   64.46  9.36  26.5 94.29     5.71
30   71.99 10.68 17.12 94.27     5.73
63   72.65 10.14 17.32 94.23     5.77
13   72.43 11.98 14.85 94.18     5.82
16   71.73 12.33  15.3 94.15     5.85
33   76.14 12.67 11.09 94.15     5.85
65   73.95  9.07 16.35 94.14     5.86
7    72.64 10.39 16.44 94.13     5.87
3    73.14 10.51 15.29 94.11     5.89
28   72.46 11.44 15.57  94.1      5.9
5    72.29 10.08 15.39 94.09     5.91
10   73.51 10.53 15.64 94.01     5.99
25   75.05  9.83  14.9 94.01     5.99
32   73.76 10.37 15.45 93.96     6.04
6    70.21 11.06 17.87 93.95     6.05
56   79.07 11.52  8.88 93.91     6.09
64   69.76 10.01 19.81  93.9      6.1
11   71.57 10.65 17.36 93.86     6.14
21   70.66 11.65 17.16 93.84     6.16
31   73.04 11.04  15.5  93.8      6.2
42   71.17  9.57 19.36 93.78     6.22
8    75.19 10.33 14.27 93.77     6.23
2     72.9 14.56 10.95 93.69     6.31
48   67.63   6.5  24.6 93.67     6.33
49   65.18  6.39 28.22 93.67     6.33
62   72.48  10.2 17.43 93.66     6.34
41   73.09 10.74 16.28 93.64     6.36
58      68     0     0 93.62     6.38
26   75.35 11.53 12.91  93.6      6.4
24    70.9 10.66 18.34 93.57     6.43
59   70.64 10.53 17.77 93.53     6.47
27   70.15 10.77 17.16 93.44     6.56
55      77  9.16 13.63 93.44     6.56
9    73.71 10.52 15.24 93.43     6.57
34   66.88  9.37 22.79 93.41     6.59
22   70.58 11.51 17.48 93.39     6.61
54   74.15     9 16.42 93.34     6.66
61   72.04  9.71 17.61 93.34     6.66
1    72.19 15.08 11.76 93.31     6.69
23   70.94 12.07 15.81 93.27     6.73
46    69.2  8.98  21.6 93.27     6.73
35   68.53 11.28 20.09 93.16     6.84
19   70.22 12.15 16.02 93.12     6.88
47   66.63  9.55 24.25 93.05     6.95
50   74.52  8.28 16.67 92.93     7.07
45   64.41  8.06  27.1  92.9      7.1
51   73.47  10.2 16.33 92.88     7.12
40    64.8 10.44 24.54  92.7      7.3
52   71.04  9.47 19.16 92.58     7.42
53   70.75  7.74 20.97 92.57     7.43
60   69.33 10.15 18.68 92.52     7.48
57   68.89 10.73 19.82    92        8
44   64.17  7.27 28.23 91.88     8.12
43   68.81  8.83 21.26 91.83     8.17
We can use the same procedure for any of the other constituents

To leave a comment for the author, please follow the link and comment on their blog: NIR-Quimiometría.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)