Module: R::IndexedObject
- Defined in:
- lib/R_interface/rindexed_object.rb
Overview
Instance Method Summary collapse
-
#[](*index) ⇒ Object
-------------------------------------------------------------------------------------- subset a vector with an index --------------------------------------------------------------------------------------.
-
#[]=(*args) ⇒ Object
-------------------------------------------------------------------------------------- subset assign a vector with an index to a value index can span multiple values, for ex., R.c(2, 3, 5) --------------------------------------------------------------------------------------.
-
#size ⇒ Object
--------------------------------------------------------------------------------------.
Instance Method Details
#[](*index) ⇒ Object
subset a vector with an index
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/R_interface/rindexed_object.rb', line 37 def [](*index) # dealing with double indexing function '[[' # If we have multiple indices, or a single array index, use [[ or `[` for names if (index.size > 1) || (index[0].is_a? Array) args = (index.size > 1) ? index : index[0] # For DataFrame with two indices: R's [[i,j]] is row i, col j. R's [[ does not accept character indices; use `[` for row/col names. # When any index is :all, use `[` (single bracket) so R gets m[i,] or m[,j]; `[[` does not accept missing subscript. # When any index is an R object (e.g. vector), use `[`; `[[` only selects one element ("attempt to select more than one element"). use_md = (args.size == 2 && self.is_a?(::R::DataFrame) && (args[0].is_a?(::String) || args[0].is_a?(::Symbol) || args[1].is_a?(::String) || args[1].is_a?(::Symbol))) || (args.size == 2 && (args[0] == :all || args[1] == :all)) || (args.size == 2 && (args[0].is_a?(::R::Object) || args[1].is_a?(::R::Object))) if use_md ::R::Support.exec_function(::R::Support.md_index, self, *args) else ::R::Support.exec_function(::R::Support.dbk_index, self, *args) end else if self.is_a?(::R::DataFrame) && index.size == 1 && (index[0].is_a?(::R::Language) || index[0].is_a?(::R::Object)) # For data.frame, a single logical/language index is expected to filter rows. # Evaluate language conditions in data-frame context, then subset df[cond, ]. row_filter = index[0].is_a?(::R::Language) ? ::R::Support.exec_function("with", self, index[0]) : index[0] ::R::Support.exec_function(::R::Support.md_index, self, row_filter, :all) else ::R::Support.exec_function_name("`[`", self, *index) end end rescue ::RuntimeError => e if e..to_s.include?("incorrect number of subscripts") ::Kernel.raise(::ArgumentError, e.) end ::Kernel.raise(e) end |
#[]=(*args) ⇒ Object
subset assign a vector with an index to a value index can span multiple values, for ex., R.c(2, 3, 5)
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/R_interface/rindexed_object.rb', line 79 def []=(*args) values = args.pop index = args # Assign result back to same handle so the object is updated in place (R's x[i] <- v semantics) if (index.size > 1) || (index[0].is_a? Array) all_args = [@r_interop, *index, values].map { |a| ::R::Support.parse_arg(a) }.join(", ") ::R.bridge.eval_r("#{@r_interop} <- `[[<-`(#{all_args})") else idx_r = ::R::Support.parse_arg(index[0]) vals_r = ::R::Support.parse_arg(values) ::R.bridge.eval_r("#{@r_interop} <- `[<-`(#{@r_interop}, #{idx_r}, #{vals_r})") end self end |
#size ⇒ Object
100 101 102 |
# File 'lib/R_interface/rindexed_object.rb', line 100 def size length.unboxed_get(0) end |