Язык программирования R/Введение: различия между версиями

Содержимое удалено Содержимое добавлено
Data Frames
Arrays
Строка 366:
</pre>
 
=== ArraysМассивы ===
 
An array is composed of n dimensions where each dimension is a vector of R objects of the same type. An array of one dimension of one element may be constructed as follows.
 
Массивы состоят из ''n'' измерений, где каждое может быть вектором из объектов '''R''' одинакового типа. Одномерные массивы с одним элементом могут быть созданы следующим кодом:
<pre width=80>
> x = array(c(T,F),dim=c(1))
Строка 376 ⟶ 375 :
</pre>
 
Одномерный (dim=c(1)) массив ''x'' создаётся из вектора с одним значением из c(T,F). Идентичный одномерный массив ''y'' может быть создан с обоими значениями из c(T,F):
The array x was created with a single dimension (dim=c(1)) drawn from the vector of possible values c(T,F). A similar array, y, can be created with a single dimension and two values.
 
<pre width=80>
> ay = array(c(T,F),dim=c(2))
> print(a)
[1] TRUE FALSE
</pre>
 
Трёхмерный массив - 3 на 3 на 3 - может быть создан как показано ниже:
A three dimensional array - 3 by 3 by 3 - may be created as follows.
 
<pre width=80>
> z = array(1:27,dim=c(3,3,3))
Строка 413 ⟶ 410 :
</pre>
 
Массивы в '''R''' используются похожим на другие языки образом: посредством индексирования целыми числами начиная с 1 (а не 0, как в C). Следующий код демонстрирует как можно получить третий элемент трёхмерного массива (то есть массив 3 на 3):
R arrays are accessed in a manner similar to arrays in other languages: by integer index, starting at 1 (not 0). The following code shows how the third dimension of the 3 by 3 by 3 array can be accessed. The third dimension is a 3 by 3 array.
 
<pre width=80>
> z[,,3]
Строка 423 ⟶ 419 :
</pre>
 
Указание двух из трёх размерностей возвращает одномерный массив:
Specifying two of the three dimensions returns an array on one dimension.
 
<pre width=80>
> z[,3,3]
Строка 430 ⟶ 425 :
</pre>
 
Указание трёх из трёх размерностей возвращает элемент трёхмерного массива:
Specifying three of three dimension returns an element of the 3 by 3 by 3 array.
 
<pre with=80>
> z[3,3,3]
Строка 437 ⟶ 431 :
</pre>
 
Возможна и более сложная адресация:
More complex partitioning of array may be had.
 
<pre width=80>
> z[,c(2,3),c(2,3)]
Строка 456 ⟶ 449 :
</pre>
 
Массив должен быть симметричным по всем размерностям. Следующий код создаёт пару массивов 3 на 3:
Arrays need not be symmetric across all dimensions. The following code creates a pair of 3 by 3 arrays.
 
<pre width=80>
> w = array(1:18,dim=c(3,3,2))
Строка 476 ⟶ 468 :
</pre>
 
Объекты векторов, из которых состоит массив, должны быть одинакового типа, но не обязательно числового:
Objects of the vectors composing the array must be of the same type, but they need not be numbers.
 
<pre width=80>
> u = array(c(T,F),dim=c(3,3,2))