galaaz 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +32 -0
  3. data/Rakefile +177 -0
  4. data/bin/galaaz +8 -0
  5. data/examples/50Plots_MasterList/scatter_plot.rb +51 -0
  6. data/examples/baseball.csv +1 -0
  7. data/examples/baseball.rb +16 -0
  8. data/examples/ggplot.rb +178 -0
  9. data/examples/islr/Figure.jpg +0 -0
  10. data/examples/islr/all.rb +32 -0
  11. data/examples/islr/ch2.spec.rb +148 -0
  12. data/examples/islr/ch3.spec.rb +28 -0
  13. data/examples/islr/ch3_boston.rb +77 -0
  14. data/examples/islr/ch3_multiple_regression.rb +36 -0
  15. data/examples/islr/ch6.spec.rb +64 -0
  16. data/examples/paper/paper.rb +36 -0
  17. data/examples/sthda_ggplot/README.md +38 -0
  18. data/examples/sthda_ggplot/all.rb +68 -0
  19. data/examples/sthda_ggplot/one_variable_continuous/density_gg.rb +52 -0
  20. data/examples/sthda_ggplot/one_variable_continuous/geom_area.rb +61 -0
  21. data/examples/sthda_ggplot/one_variable_continuous/geom_density.rb +77 -0
  22. data/examples/sthda_ggplot/one_variable_continuous/geom_dotplot.rb +69 -0
  23. data/examples/sthda_ggplot/one_variable_continuous/geom_freqpoly.rb +69 -0
  24. data/examples/sthda_ggplot/one_variable_continuous/geom_histogram.rb +62 -0
  25. data/examples/sthda_ggplot/one_variable_continuous/histogram_density.rb +55 -0
  26. data/examples/sthda_ggplot/one_variable_continuous/stat.rb +62 -0
  27. data/examples/sthda_ggplot/one_variable_discrete/bar.rb +54 -0
  28. data/examples/sthda_ggplot/qplots/box_violin_dot.rb +57 -0
  29. data/examples/sthda_ggplot/qplots/scatter_plots.rb +67 -0
  30. data/examples/sthda_ggplot/scatter_gg.rb +60 -0
  31. data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_bin2d.rb +49 -0
  32. data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_density2d.rb +64 -0
  33. data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_hex.rb +52 -0
  34. data/examples/sthda_ggplot/two_variables_cont_cont/geom_point.rb +65 -0
  35. data/examples/sthda_ggplot/two_variables_cont_cont/geom_smooth.rb +66 -0
  36. data/examples/sthda_ggplot/two_variables_cont_cont/misc.rb +83 -0
  37. data/examples/sthda_ggplot/two_variables_cont_function/geom_area.rb +63 -0
  38. data/examples/sthda_ggplot/two_variables_disc_cont/geom_bar.rb +85 -0
  39. data/examples/sthda_ggplot/two_variables_disc_cont/geom_boxplot.rb +62 -0
  40. data/examples/sthda_ggplot/two_variables_disc_cont/geom_dotplot.rb +75 -0
  41. data/examples/sthda_ggplot/two_variables_disc_cont/geom_jitter.rb +74 -0
  42. data/examples/sthda_ggplot/two_variables_disc_cont/geom_line.rb +55 -0
  43. data/examples/sthda_ggplot/two_variables_disc_cont/geom_violin.rb +70 -0
  44. data/examples/sthda_ggplot/two_variables_disc_disc/geom_jitter.rb +40 -0
  45. data/examples/sthda_ggplot/two_variables_error/geom_crossbar.rb +108 -0
  46. data/examples/subsetting.rb +372 -0
  47. data/lib/expression.rb +45 -0
  48. data/lib/galaaz.rb +27 -0
  49. data/lib/r.rb +118 -0
  50. data/lib/r_methods.rb +89 -0
  51. data/lib/rbinary_operators.rb +226 -0
  52. data/lib/rclosure.rb +34 -0
  53. data/lib/rdata_frame.rb +63 -0
  54. data/lib/renvironment.rb +34 -0
  55. data/lib/rexpression.rb +34 -0
  56. data/lib/rindexed_object.rb +68 -0
  57. data/lib/rlanguage.rb +64 -0
  58. data/lib/rlist.rb +72 -0
  59. data/lib/rmatrix.rb +38 -0
  60. data/lib/rmd_indexed_object.rb +43 -0
  61. data/lib/robject.rb +297 -0
  62. data/lib/rpkg.rb +53 -0
  63. data/lib/rsupport.rb +292 -0
  64. data/lib/rsupport_scope.rb +77 -0
  65. data/lib/rsymbol.rb +57 -0
  66. data/lib/ruby_callback.rb +83 -0
  67. data/lib/ruby_extensions.rb +74 -0
  68. data/lib/runary_operators.rb +58 -0
  69. data/lib/rvector.rb +117 -0
  70. data/r_requires/ggplot.rb +31 -0
  71. data/specs/all.rb +45 -0
  72. data/specs/r_dataframe.spec.rb +181 -0
  73. data/specs/r_eval.spec.rb +164 -0
  74. data/specs/r_function.spec.rb +105 -0
  75. data/specs/r_language.spec.rb +135 -0
  76. data/specs/r_list.spec.rb +129 -0
  77. data/specs/r_list_apply.spec.rb +99 -0
  78. data/specs/r_matrix.spec.rb +83 -0
  79. data/specs/r_vector_creation.spec.rb +99 -0
  80. data/specs/r_vector_functions.spec.rb +59 -0
  81. data/specs/r_vector_object.spec.rb +94 -0
  82. data/specs/r_vector_operators.spec.rb +174 -0
  83. data/specs/r_vector_subsetting.spec.rb +136 -0
  84. data/specs/tmp.rb +134 -0
  85. data/version.rb +2 -0
  86. metadata +198 -0
@@ -0,0 +1,59 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # @author Rodrigo Botafogo
5
+ #
6
+ # Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
7
+ # and distribute this software and its documentation, without fee and without a signed
8
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
9
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
10
+ # distributions.
11
+ #
12
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
13
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
14
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
15
+ # POSSIBILITY OF SUCH DAMAGE.
16
+ #
17
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
19
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
20
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
21
+ # OR MODIFICATIONS.
22
+ ##########################################################################################
23
+
24
+ require 'galaaz'
25
+
26
+ describe R::Vector do
27
+
28
+ #--------------------------------------------------------------------------------------
29
+ #
30
+ #--------------------------------------------------------------------------------------
31
+
32
+ it " should match two vectors with %in%" do
33
+ vec1 = R.c(1, 2, 3, 4)
34
+ vec2 = R.c(1, 2, 3, 4)
35
+ vec3 = R.c(3, 4, 5)
36
+ vec4 = R.c(4, 5, 6, 7)
37
+
38
+ # R has functions defined with '%%' notation. In order to access those functions
39
+ # from SciCom we use the '._' method with two arguments, the first argument is the
40
+ # name of the function, for instance, function %in%, the name of the method is ':in'
41
+ # Ex: vec1 %in% vec2 => vec1._ :in, vec2
42
+ expect((vec1._ :in, vec2).identical(R.c(true, true, true, true))).to eq true
43
+ expect((vec1._ :in, vec3).identical(R.c(false, false, true, true))).to eq true
44
+ expect((vec2._ :in, vec4).identical(R.c(false, false, false, true))).to eq true
45
+
46
+ end
47
+
48
+ end
49
+
50
+ =begin
51
+ it "should allow adding elements to the vector" do
52
+ R.eval(<<-R)
53
+ vec = c(2.1, 4.2, 3.3, 5.4);
54
+ print(order(vec));
55
+ R
56
+
57
+ end
58
+ =end
59
+
@@ -0,0 +1,94 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # @author Rodrigo Botafogo
5
+ #
6
+ # Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
7
+ # and distribute this software and its documentation, without fee and without a signed
8
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
9
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
10
+ # distributions.
11
+ #
12
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
13
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
14
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
15
+ # POSSIBILITY OF SUCH DAMAGE.
16
+ #
17
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
19
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
20
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
21
+ # OR MODIFICATIONS.
22
+ ##########################################################################################
23
+
24
+ require 'galaaz'
25
+
26
+ describe R::Vector do
27
+
28
+ context "When assigning attributes to a Vector" do
29
+
30
+ before(:each) do
31
+ @vect = R.c(1, 2, 3, 4, 5, 6)
32
+ end
33
+
34
+ it "should assign names to vectors" do
35
+ # set names
36
+ @vect.names = R.c("a", "b", "c", "d", "e", "f")
37
+ expect(@vect.names.identical(R.c("a", "b", "c", "d", "e", "f"))).to eq true
38
+ end
39
+
40
+ it "should assign a dim to vector" do
41
+ # set dim
42
+ @vect.dim = R.c(3, 2)
43
+ expect(@vect.dim[1]).to eq 3
44
+ expect(@vect.dim[2]).to eq 2
45
+ end
46
+
47
+ it "should assign row.names to a vector with dimention" do
48
+ @vect.dim = R.c(3, 2)
49
+ # set row.names
50
+ @vect.row__names = R.c("A", "B", "C")
51
+ expect(@vect.row__names.identical(R.c("A", "B", "C")))
52
+ # note that when accessing row__names[2] we are getting a native ruby object
53
+ # and not a vector. This might change in future versions
54
+ expect(@vect.row__names[2]).to eq "B"
55
+ end
56
+
57
+ it "should set the class of the object using rclass" do
58
+ # set the R class. Note that we need to use rclass instead of class, since
59
+ # class is a Ruby keyword
60
+ @vect.rclass = "myClass"
61
+ expect(@vect.rclass).to eq "myClass"
62
+ end
63
+
64
+ it "should set the attribute of a Vector using attr" do
65
+ # the other alternative is to use method attr to change the attribute
66
+ @vect.attr = {which: "class", value: "newClass"}
67
+ expect(@vect.rclass).to eq "newClass"
68
+ end
69
+
70
+ it "should allow changing an element of a vector attribute" do
71
+ # pending "Need to implement new function for this"
72
+ # set names
73
+ @vect.names = R.c("a", "b", "c", "d", "e", "f")
74
+ @vect.names[2] = "hello"
75
+ expect(@vect.names[2]).to eq "hello"
76
+ end
77
+
78
+ end
79
+
80
+ context "When passing named arguments" do
81
+
82
+ it "Named arguments should become the names of the vector" do
83
+ vect = R.c(a: 1, b: 2, c: 3, d: 4)
84
+ expect(vect.names.identical(R.c("a", "b", "c", "d"))).to eq true
85
+ end
86
+
87
+ it "should allow adding names to sub-vectors" do
88
+ vect = R.c(1, 2, 3, a: R.c(1, 2, 3), b: 5, c: 6)
89
+ expect(vect.names.identical(R.c("", "", "", "a1", "a2", "a3", "b", "c"))).to eq true
90
+ end
91
+
92
+ end
93
+
94
+ end
@@ -0,0 +1,174 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # @author Rodrigo Botafogo
5
+ #
6
+ # Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
7
+ # and distribute this software and its documentation, without fee and without a signed
8
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
9
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
10
+ # distributions.
11
+ #
12
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
13
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
14
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
15
+ # POSSIBILITY OF SUCH DAMAGE.
16
+ #
17
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
19
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
20
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
21
+ # OR MODIFICATIONS.
22
+ ##########################################################################################
23
+
24
+ require 'galaaz'
25
+
26
+ describe R::Vector do
27
+
28
+ #----------------------------------------------------------------------------------------
29
+ context "Arithmetic operators" do
30
+
31
+ before(:each) do
32
+ @vec1 = R.c(3, 5.7, 10, 12)
33
+ @vec2 = R.c(2.3, 3, 9, 17)
34
+ end
35
+
36
+ it "should add two vectors" do
37
+ res = @vec1 + @vec2
38
+ expect(res.all__equal(R.c(5.3, 8.7, 19, 29))).to eq true
39
+ end
40
+
41
+ it "should subtract two vectors" do
42
+ res = @vec1 - @vec2
43
+ expect(res.all__equal(R.c(0.7, 2.7, 1.0, -5.0))).to eq true
44
+ end
45
+
46
+ it "should multiply two vectors" do
47
+ res = @vec1 * @vec2
48
+ expect(res.all__equal(R.c(6.9, 17.1, 90.0, 204.0))).to eq true
49
+ end
50
+
51
+ it "should exponentiate two vectors" do
52
+ res = @vec1 ** @vec2
53
+ expect(res.all__equal(R.c(3**2.3, 5.7**3, 10**9, 12**17))).to eq true
54
+ end
55
+
56
+ it "should divide two vectors" do
57
+ res = @vec1 / @vec2
58
+ expect(res.all__equal(R.c(3/2.3, 5.7/3, 10/9.0, 12.0/17))).to eq true
59
+ end
60
+
61
+ it "should calculate the modulus of two vectors" do
62
+ res = @vec1 % @vec2
63
+ expect(res.all__equal(R.c(3%2.3, 5.7%3, 10%9.0, 12.0%17))).to eq true
64
+ end
65
+
66
+ it "should apply unary minus to all elements of a vector" do
67
+ expect((-@vec1).all__equal(R.c(-3, -5.7, -10, -12))).to eq true
68
+ end
69
+
70
+ it "should operate with Numeric first" do
71
+ res = 10 + @vec1
72
+ expect(res == R.c(13, 15.7, 20, 22)).to eq true
73
+ res = 10 - @vec1
74
+ expect(res == R.c(7, 4.3, 0, -1)).to eq true
75
+ res = 10 / @vec1
76
+ expect(res == R.c(3.3333333, 1.7543860, 1, 0.8333333)).to eq true
77
+ end
78
+
79
+ end
80
+
81
+ #----------------------------------------------------------------------------------------
82
+ context "Comparison operators" do
83
+
84
+ it "should check vectors for equality / inequality" do
85
+ vec1 = R.c(1, 2)
86
+ vec2 = R.c(1, 2)
87
+ vec3 = R.c(0, 2)
88
+
89
+ expect((vec1 == vec2).all__equal(R.c(true, true))).to eq true
90
+ expect((vec1 != vec2).all__equal(R.c(false, false))).to eq true
91
+ expect((vec1 == vec3).all__equal(R.c(false, true))).to eq true
92
+ end
93
+
94
+ it "should check vectors for <, <=, >, >=" do
95
+ vec1 = R.c(1, 2, 3)
96
+ vec2 = R.c(0, 4, 3)
97
+
98
+ expect((vec1 < vec2).all__equal(R.c(false, true, false))).to eq true
99
+ expect((vec1 <= vec2).all__equal(R.c(false, true, true))).to eq true
100
+ expect((vec1 > vec2).all__equal(R.c(true, false, false))).to eq true
101
+ expect((vec1 >= vec2).all__equal(R.c(true, false, true))).to eq true
102
+ end
103
+
104
+ end
105
+
106
+ #----------------------------------------------------------------------------------------
107
+ context "Logical operators" do
108
+
109
+ before(:each) do
110
+ @vec1 = R.c(true, true, false, true)
111
+ @vec2 = R.c(true, false, false, false)
112
+ end
113
+
114
+ it "should negate a vector" do
115
+ expect((!@vec1).all__equal(R.c(false, false, true, false))).to eq true
116
+ end
117
+
118
+ it "should 'and' and 'or' two vectors" do
119
+ expect((@vec1 & @vec2).all__equal R.c(true, false, false, false)).to eq true
120
+ expect((@vec1 | @vec2).all__equal R.c(true, true, false, true)).to eq true
121
+ end
122
+
123
+ end
124
+
125
+ #----------------------------------------------------------------------------------------
126
+ context "Access from Ruby" do
127
+
128
+ before(:each) do
129
+ @vec1 = R.c(3, 5.7, 10, 12)
130
+ @vec2 = R.c(2.3, 3, 9, 17)
131
+ end
132
+
133
+ it "Should retrieve Numeric values from a vector with '<<'" do
134
+ # subsetting a vector with '[' and '[[' returns an R::Vector, not a Numeric object
135
+ # Use the '<<' operator to retrieve an element of the R::Vector as a Numeric
136
+ # object
137
+ expect(@vec1[1].is_a? R::Vector).to eq true
138
+ expect((@vec1 << 0).is_a? Numeric).to eq true
139
+
140
+ # Note, however, that indexing starts at 0
141
+ expect((@vec1 << 0)).to eq 3
142
+ expect((@vec1 << 2)).to eq 10
143
+ end
144
+
145
+ it "should treat R::Vector as an Enumerable" do
146
+ # each 'value' is an R::Vector, with only 1 element. In order to use Ruby 'sum'
147
+ # we need to extract the value as a Ruby Numeric with << 1
148
+ expect(@vec1.sum { |value| value << 0 }).to eq 30.7
149
+ end
150
+
151
+ it "should implement 'pop' to extract the first element of an R::Vector" do
152
+ # pop is equivalent to '<< 1'
153
+ expect(@vec1.pop).to eq 3
154
+ end
155
+
156
+ it "should allow storage and retrieval of Ruby Objects in R data structure" do
157
+
158
+ class RData
159
+ def val
160
+ 5
161
+ end
162
+ end
163
+
164
+ # Using 'c' to add an external element, transforms the vector into a list
165
+ vec3 = R.c(RData.new, RData.new)
166
+
167
+ expect(vec3[[1]].is_a? RData).to eq true
168
+ expect(vec3[[1]].val == 5).to eq true
169
+ expect(vec3[[2]].val == 5).to eq true
170
+ end
171
+
172
+ end
173
+
174
+ end
@@ -0,0 +1,136 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # @author Rodrigo Botafogo
5
+ #
6
+ # Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
7
+ # and distribute this software and its documentation, without fee and without a signed
8
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
9
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
10
+ # distributions.
11
+ #
12
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
13
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
14
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
15
+ # POSSIBILITY OF SUCH DAMAGE.
16
+ #
17
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
19
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
20
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
21
+ # OR MODIFICATIONS.
22
+ ##########################################################################################
23
+
24
+ require 'galaaz'
25
+
26
+ describe R::Vector do
27
+
28
+ context "When subsetting a Vector" do
29
+
30
+ before(:all) do
31
+ @vect = R.c(1, 2, 3, 4, 5)
32
+ end
33
+
34
+ it "should subset vector with integer obtaining a single element" do
35
+ # indexing a vector with a positive interger returns the value at the index
36
+ expect(@vect[1]).to eq 1
37
+ expect(@vect[3]).to eq 3
38
+ expect(@vect[5]).to eq 5
39
+ end
40
+
41
+ it "should subset a vector with a negative integer obtaining a Vector" do
42
+ # a vector when indexed with a negative integer, is a vector without the
43
+ # given element. So, we are here removing the second element of the vector
44
+ # which leave us with the second element being 3
45
+ expect(@vect[-2].identical(R.c(1, 3, 4, 5))).to eq true
46
+ expect(@vect[-3].identical(R.c(1, 2, 4, 5))).to eq true
47
+ end
48
+
49
+ it "should subset a vector with another integer vector" do
50
+ expect(@vect[R.c(2, 4)][1]).to eq 2
51
+ expect(@vect[R.c(2, 4)][2]).to eq 4
52
+ expect(@vect[R.c(3, 3)][2]).to eq 3
53
+ end
54
+
55
+ it "should allow reordering of elements based on function" do
56
+ vect = R.c(2.1, 4.2, 3.3, 5.4)
57
+ expect(vect[R.order(vect)][2]).to eq 3.3
58
+ expect(vect[R.order(vect)][3]).to eq 4.2
59
+ end
60
+
61
+ it "should subset with another character vector" do
62
+ x = R.c("m", "f", "u", "f", "f", "m", "m")
63
+ lookup = R.c(m: "Male", f: "Female", u: R::NA)
64
+
65
+ res = R.c("Male", "Female", R::NA, "Female", "Female", "Male", "Male")
66
+ res.names = R.c("m", "f", "u", "f", "f", "m", "m")
67
+
68
+ expect(lookup[x].all__equal res).to eq true
69
+ end
70
+
71
+ it "should subset with 'each'" do
72
+ i = 1
73
+ @vect.each do |elmt|
74
+ expect(elmt.typeof == 'integer').to eq true
75
+ expect(elmt == i).to eq true
76
+ i += 1
77
+ end
78
+ end
79
+
80
+ it "should subset with 'each_with_index'" do
81
+ # note that the index starts at 1, since we are getting back an R::Vector
82
+ # element
83
+ @vect.each_with_index do |elmt, i|
84
+ expect(elmt.typeof == 'integer').to eq true
85
+ expect elmt == i
86
+ end
87
+ end
88
+
89
+ it "should subset with 'each(:native)' getting back a native Ruby element (not R::Vector)" do
90
+ # in order to access the internal Ruby element from the R::Vector, use 'each' with
91
+ # the :native keyword
92
+ @vect.each(:native) do |elmt|
93
+ expect(elmt.is_a? Numeric).to eq true
94
+ end
95
+ end
96
+
97
+ it "should subset with 'each_with_index(:native)' getting a Ruby element" do
98
+ # when using the :native keyword, indexing starts at 0
99
+ @vect.each_with_index(:native) do |elmt, i|
100
+ expect elmt == i + 1
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ #----------------------------------------------------------------------------------------
107
+ context "When subseting with double square brackets" do
108
+
109
+ it "should retrieve values the same way as single square brackets" do
110
+ vect = R.c(2.1, 4.2, 3.3, 5.4)
111
+ expect vect[[1]] == 2.1
112
+ end
113
+
114
+ end
115
+
116
+ #----------------------------------------------------------------------------------------
117
+ context "When subset assigning to a vector" do
118
+
119
+ before(:all) do
120
+ @vect = R.c(1, 2, 3, 4, 5)
121
+ end
122
+
123
+ it "should subset assign with integer" do
124
+ expect(@vect[2]).to eq 2
125
+ @vect[2] = 1000
126
+ expect @vect[2] == 1000
127
+ end
128
+
129
+ it "should subset assign to the elements given by another vector" do
130
+ @vect[R.c(2, 3)] = R.c(1000, 2000)
131
+ expect(@vect.identical(R.c(1, 1000, 2000, 4, 5))).to eq true
132
+ end
133
+
134
+ end
135
+
136
+ end