R语言 数组
数组是由固定数量的维度定义的基本数据存储结构。数组用于在连续的内存位置分配空间。一维数组被称为向量,长度是其唯一的维度。二维数组被称为矩阵,由固定数量的行和列组成。数组由相同数据类型的所有元素组成。向量被作为输入提供给函数,然后根据维数创建一个数组。
创建一个数组
R中的数组可以通过使用 array() 函数来创建。列表中的元素与所需的维度一起被传递给array()函数。
语法
array(data, dim = (nrow, ncol, nmat), dimnames=names)
其中 。
nrow : 行的数量
ncol : 列的数量
nmat :nrow * ncol
的矩阵数量
dimnames : 默认值 = NULL。
否则,必须指定一个列表,其中有一个维度的每个组件的名称。每个分量要么是空的,要么是一个长度等于该维度的dim值的向量。
单维数组
矢量是一个单维数组,它由一个单一的维度,即长度指定。一个向量可以通过 ‘c() ‘函数来创建。一个值的列表被传递给c()函数来创建一个向量。
例子
vec1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
print (vec1)
# cat is used to concatenate
# strings and print it.
cat ("Length of vector : ", length(vec1))
输出
[1] 1 2 3 4 5 6 7 8 9
Length of vector : 9
多维数组
二维矩阵是一个由固定数量的行和列指定的数组,每个行和列包含相同的数据类型。矩阵是通过使用 array() 函数创建的,其数值和维度被传递给该函数。
例子
# arranges data from 2 to 13
# in two matrices of dimensions 2x3
arr = array(2:13, dim = c(2, 3, 2))
print(arr)
输出
, , 1
[,1] [,2] [,3]
[1,] 2 4 6
[2,] 3 5 7
, , 2
[,1] [,2] [,3]
[1,] 8 10 12
[2,] 9 11 13
不同长度的向量也可以作为输入送入 array() 函数。但是,所有向量的元素总数应等于矩阵的元素数。这些元素按照函数中指定的顺序排列。
示例 。
vec1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vec2 <- c(10, 11, 12)
# elements are combined into a single vector,
# vec1 elements followed by vec2 elements.
arr = array(c(vec1, vec2), dim = c(2, 3, 2))
print (arr)
输出
,, 1
[, 1] [, 2] [, 3]
[1, ] 1 3 5
[2, ] 2 4 6
,, 2
[, 1] [, 2] [, 3]
[1, ] 7 9 11
[2, ] 8 10 12
数组的命名
行名、列名和矩阵名分别被指定为行数、列数和矩阵数的向量。默认情况下,行、列和矩阵是以其索引值命名的。
row_names <- c("row1", "row2")
col_names <- c("col1", "col2", "col3")
mat_names <- c("Mat1", "Mat2")
# the naming of the various elements
# is specified in a list and
# fed to the function
arr = array(2:14, dim = c(2, 3, 2),
dimnames = list(row_names,
col_names, mat_names))
print (arr)
输出
,, Mat1
col1 col2 col3
row1 2 4 6
row2 3 5 7
,, Mat2
col1 col2 col3
row1 8 10 12
row2 9 11 13
访问数组
可以通过使用由逗号分隔的不同维度的索引来访问数组。不同的组件可以通过元素的名称或位置的任何组合来指定。
访问一维数组
可以通过使用相应元素的索引来访问这些元素。
vec <- c(1:10)
# accessing entire vector
cat ("Vector is : ", vec)
# accessing elements
cat ("Third element of vector is : ", vec[3])
**< 输出 **
Vector is : 1 2 3 4 5 6 7 8 9 10
Third element of vector is : 3
访问整个矩阵
vec1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vec2 <- c(10, 11, 12)
row_names <- c("row1", "row2")
col_names <- c("col1", "col2", "col3")
mat_names <- c("Mat1", "Mat2")
arr = array(c(vec1, vec2), dim = c(2, 3, 2),
dimnames = list(row_names,
col_names, mat_names))
# accessing matrix 1 by index value
print ("Matrix 1")
print (arr[,,1])
# accessing matrix 2 by its name
print ("Matrix 2")
print(arr[,,"Mat2"])
输出
[1] "Matrix 1"
col1 col2 col3
row1 1 3 5
row2 2 4 6
[1] "Matrix 2"
col1 col2 col3
row1 7 9 11
row2 8 10 12
访问矩阵的特定行和列
行和列也可以通过名称和索引来访问。
vec1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vec2 <- c(10, 11, 12)
row_names <- c("row1", "row2")
col_names <- c("col1", "col2", "col3")
mat_names <- c("Mat1", "Mat2")
arr = array(c(vec1, vec2), dim = c(2, 3, 2),
dimnames = list(row_names,
col_names, mat_names))
# accessing matrix 1 by index value
print ("1st column of matrix 1")
print (arr[, 1, 1])
# accessing matrix 2 by its name
print ("2nd row of matrix 2")
print(arr["row2",,"Mat2"])
输出
[1] "1st column of matrix 1"
row1 row2
1 2
[1] "2nd row of matrix 2"
col1 col2 col3
8 10 12
单独访问元素
可以通过使用行和列的编号或名称来访问元素。
vec1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vec2 <- c(10, 11, 12)
row_names <- c("row1", "row2")
col_names <- c("col1", "col2", "col3")
mat_names <- c("Mat1", "Mat2")
arr = array(c(vec1, vec2), dim = c(2, 3, 2),
dimnames = list(row_names, col_names, mat_names))
# accessing matrix 1 by index value
print ("2nd row 3rd column matrix 1 element")
print (arr[2, "col3", 1])
# accessing matrix 2 by its name
print ("2nd row 1st column element of matrix 2")
print(arr["row2", "col1", "Mat2"])
输出
[1] "2nd row 3rd column matrix 1 element"
[1] 6
[1] "2nd row 1st column element of matrix 2"
[1] 8
访问数组元素的子集
通过定义行或列的限制范围,可以访问数组元素的一个较小的子集。
row_names <- c("row1", "row2")
col_names <- c("col1", "col2", "col3", "col4")
mat_names <- c("Mat1", "Mat2")
arr = array(1:15, dim = c(2, 4, 2),
dimnames = list(row_names, col_names, mat_names))
# print elements of both the rows and columns 2 and 3 of matrix 1
print (arr[, c(2, 3), 1])
输出
col2 col3
row1 3 5
row2 4 6
向数组添加元素
元素可以被添加到数组中的不同位置。元素的顺序是按照它们被添加到数组中的顺序保留的。添加新元素所需的时间复杂度是O(n),其中n是数组的长度。数组的长度随着元素添加的数量而增加。在R中,有各种内置的函数可以用来添加新的值。
- c(vector, values): c()函数允许我们将数值追加到数组的末端。多个值也可以加在一起。
- append(vector, values):该方法允许在向量的任何位置追加数值。默认情况下,这个函数将元素加在最后。
append(vector, values, after=length(vector))在函数的最后一个参数中指定的数组长度之后添加新的值。
- 使用数组的长度函数:
可以在length+x索引处添加元素,其中x>0。
# creating a uni-dimensional array
x <- c(1, 2, 3, 4, 5)
# addition of element using c() function
x <- c(x, 6)
print ("Array after 1st modification ")
print (x)
# addition of element using append function
x <- append(x, 7)
print ("Array after 2nd modification ")
print (x)
# adding elements after computing the length
len <- length(x)
x[len + 1] <- 8
print ("Array after 3rd modification ")
print (x)
# adding on length + 3 index
x[len + 3]<-9
print ("Array after 4th modification ")
print (x)
# append a vector of values to the array after length + 3 of array
print ("Array after 5th modification")
x <- append(x, c(10, 11, 12), after = length(x)+3)
print (x)
# adds new elements after 3rd index
print ("Array after 6th modification")
x <- append(x, c(-1, -1), after = 3)
print (x)
**输出: **
[1] "Array after 1st modification "
[1] 1 2 3 4 5 6
[1] "Array after 2nd modification "
[1] 1 2 3 4 5 6 7
[1] "Array after 3rd modification "
[1] 1 2 3 4 5 6 7 8
[1] "Array after 4th modification "
[1] 1 2 3 4 5 6 7 8 NA 9
[1] "Array after 5th modification"
[1] 1 2 3 4 5 6 7 8 NA 9 10 11 12
[1] "Array after 6th modification"
[1] 1 2 3 -1 -1 4 5 6 7 8 NA 9 10 11 12
数组的原始长度是7,在第三次修改后,元素一直存在到第8个索引值。现在,在第四次修改时,当我们在第10个索引值处添加元素9时,R的内置函数自动在缺失的值位置添加NA。
在第五次修改时,从第11个索引值开始添加数组中的元素[10, 11, 12]
。
在第6次修改时,数组[-1,-1]
被附加在数组的第三个位置之后。
从数组中移除元素
在R语言中,可以从数组中移除元素,可以一次移除一个,也可以多个一起移除。这些元素被指定为数组的索引,其中满足条件的数组值被保留,其余被移除。删除的比较是基于数组的值。多个条件也可以结合在一起,以删除一个范围的元素。另一种移除元素的方法是使用 %in% 操作符,其中属于操作符的TRUE值的元素值集合被显示为结果,其余的被移除。
# creating an array of length 9
m <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
print ("Original Array")
print (m)
# remove a single value element:3 from array
m <- m[m != 3]
print ("After 1st modification")
print (m)
# removing elements based on condition
# where either element should be
# greater than 2 and less than equal to 8
m <- m[m>2 & m<= 8]
print ("After 2nd modification")
print (m)
# remove sequence of elements using another array
remove <- c(4, 6, 8)
# check which element satisfies the remove property
print (m % in % remove)
print ("After 3rd modification")
print (m [! m % in % remove])
输出
[1] "Original Array"
[1] 1 2 3 4 5 6 7 8 9
[1] "After 1st modification"
[1] 1 2 4 5 6 7 8 9
[1] "After 2nd modification"
[1] 4 5 6 7 8
[1] TRUE FALSE TRUE FALSE TRUE
[1] "After 3rd modification"
[1] 5 7
在第一次修改时,所有不等于3的元素值被保留。在第2次修改时,保留2到8之间的元素范围,其余的被删除。在第3次修改时,满足FALSE值的元素被打印出来,因为该条件涉及到NOT操作符。
更新数组中现有的元素
数组中的元素可以用新的值进行更新,方法是将数组中所需要的索引与修改后的值进行分配。这些变化会保留在原始数组中。如果要更新的索引值在数组的长度范围内,那么该值将被改变,否则,新的元素将被添加到指定的索引处。多个元素也可以同时被更新,可以是同一个元素的值,也可以是多个值,如果新值被指定为一个向量的话。
# creating an array of length 9
m <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
print ("Original Array")
print (m)
# updating single element
m[1] <- 0
print ("After 1st modification")
print (m)
# updating sequence of elements
m[7:9] <- -1
print ("After 2nd modification")
print (m)
# updating two indices with two different values
m[c(2, 5)] <- c(-1, -2)
print ("After 3rd modification")
print (m)
# this add new element to the array
m[10] <- 10
print ("After 4th modification")
print (m)
输出
[1] "Original Array"
[1] 1 2 3 4 5 6 7 8 9
[1] "After 1st modification"
[1] 0 2 3 4 5 6 7 8 9
[1] "After 2nd modification"
[1] 0 2 3 4 5 6 -1 -1 -1
[1] "After 3rd modification"
[1] 0 -1 3 4 -2 6 -1 -1 -1
[1] "After 4th modification"
[1] 0 -1 3 4 -2 6 -1 -1 -1 10
在第2次修改时,索引7至9的元素分别被更新为-1。在第3次修改时,第2个元素被替换为-1,第5个元素被替换为-2。在第4次修改时,由于第10个索引大于数组的长度,一个新的元素被添加。