مصفوفات ص
المصفوفات
المصفوفة هي مجموعة بيانات ثنائية الأبعاد تتكون من أعمدة وصفوف.
العمود هو تمثيل عمودي للبيانات ، بينما الصف هو تمثيل أفقي للبيانات.
يمكن إنشاء مصفوفة matrix()
بالدالة. حدد المعلمات nrow
والمعلمات ncol
للحصول على عدد الصفوف والأعمدة:
مثال
# Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
# Print the
matrix
thismatrix
ملاحظة: تذكر أن c()
الوظيفة تُستخدم لربط العناصر معًا.
يمكنك أيضًا إنشاء مصفوفة بالسلاسل:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix
عناصر مصفوفة الوصول
يمكنك الوصول إلى العناصر باستخدام [ ]
الأقواس. الرقم الأول "1" في القوس يحدد موضع الصف ، بينما يحدد الرقم الثاني "2" موضع العمود:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix[1, 2]
يمكن الوصول إلى الصف بأكمله إذا حددت فاصلة بعد الرقم الموجود بين القوسين:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix[2,]
يمكن الوصول إلى العمود بالكامل إذا حددت فاصلة قبل الرقم الموجود في القوس:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix[,2]
الوصول إلى أكثر من صف واحد
يمكن الوصول إلى أكثر من صف إذا كنت تستخدم c()
الوظيفة:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
thismatrix[c(1,2),]
الوصول إلى أكثر من عمود واحد
يمكن الوصول إلى أكثر من عمود إذا كنت تستخدم c()
الوظيفة:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
thismatrix[,
c(1,2)]
أضف صفوفًا وأعمدة
استخدم cbind()
الوظيفة لإضافة أعمدة إضافية في مصفوفة:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
newmatrix <-
cbind(thismatrix, c("strawberry", "blueberry", "raspberry"))
# Print the new matrix
newmatrix
ملاحظة: يجب أن تكون الخلايا في العمود الجديد بنفس طول المصفوفة الحالية.
استخدم rbind()
الوظيفة لإضافة صفوف إضافية في مصفوفة:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
newmatrix <-
rbind(thismatrix, c("strawberry", "blueberry", "raspberry"))
# Print the new matrix
newmatrix
ملاحظة: يجب أن تكون الخلايا في الصف الجديد بنفس طول المصفوفة الحالية.
قم بإزالة الصفوف والأعمدة
استخدم c()
الوظيفة لإزالة الصفوف والأعمدة في مصفوفة:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "mango", "pineapple"),
nrow = 3, ncol =2)
#Remove the first row and the first column
thismatrix <- thismatrix[-c(1), -c(1)]
thismatrix
تحقق مما إذا كان العنصر موجودًا
لمعرفة ما إذا كان عنصر محدد موجودًا في مصفوفة ، استخدم %in%
عامل التشغيل:
مثال
تحقق من وجود "تفاحة" في المصفوفة:
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
"apple" %in% thismatrix
كمية الصفوف والأعمدة
استخدم dim()
الدالة لإيجاد عدد الصفوف والأعمدة في مصفوفة:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
dim(thismatrix)
طول المصفوفة
استخدم length()
الدالة لإيجاد أبعاد المصفوفة:
مثال
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
length(thismatrix)
إجمالي الخلايا في المصفوفة هو عدد الصفوف مضروبًا في عدد الأعمدة.
في المثال أعلاه: البعد = 2 * 2 = 4 .
حلقة من خلال مصفوفة
يمكنك المرور عبر المصفوفة باستخدام for
حلقة. ستبدأ الحلقة من الصف الأول ، وتتحرك لليمين:
مثال
مرر عبر عناصر المصفوفة واطبعها:
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
for (rows in 1:nrow(thismatrix)) {
for (columns in
1:ncol(thismatrix)) {
print(thismatrix[rows, columns])
}
}
اجمع بين مصفوفتين
مرة أخرى ، يمكنك استخدام الدالة rbind()
or cbind()
لدمج مصفوفتين أو أكثر معًا:
مثال
# Combine matrices
Matrix1 <- matrix(c("apple", "banana", "cherry",
"grape"), nrow = 2, ncol = 2)
Matrix2 <- matrix(c("orange", "mango",
"pineapple", "watermelon"), nrow = 2, ncol = 2)
# Adding it as a rows
Matrix_Combined <- rbind(Matrix1, Matrix2)
Matrix_Combined
# Adding it as a columns
Matrix_Combined <- cbind(Matrix1, Matrix2)
Matrix_Combined