Write a R program to access the element at 3rd column and 2nd row, only the 3rd row and only the 4th column of a given matrix

  • برمجة R

Write a R program to access the element at 3rd column and 2nd row, only the 3rd row and only the 4th column of a given matrix.

الأجوبة

row_names = c("row1", "row2", "row3", "row4")
col_names = c("col1", "col2", "col3", "col4")
M = matrix(c(1:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
print("Original Matrix:")
print(M)
print("Access the element at 3rd column and 2nd row:")
print(M[2,3])
print("Access only the  3rd row:")
print(M[3,])
print("Access only the 4th column:")
print(M[,4])

Sample Output:

[1] "Original Matrix:"
     col1 col2 col3 col4
row1    1    2    3    4
row2    5    6    7    8
row3    9   10   11   12
row4   13   14   15   16
[1] "Access the element at 3rd column and 2nd row:"
[1] 7
[1] "Access only the  3rd row:"
col1 col2 col3 col4 
   9   10   11   12 
[1] "Access only the 4th column:"
row1 row2 row3 row4 
   4    8   12   16
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...