R从数据帧创建一个不均匀的(7行乘6列)转换矩阵

R create an uneven (7 rows by 6 columns) transition matrix from a dataframe

我正在尝试为珊瑚大小 class 转换创建一个转换矩阵,以反映以下数据框中的数据:

数据帧头

完整数据框

HAN_high_transition <- structure(list(experimental_treatment = c("Hanauma Bay_High", 
"Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
"Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
"Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
"Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
"Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
"Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
"Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
"Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
"Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
"Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
"Hanauma Bay_High"), size_class_start = c(1, 1, 1, 1, 1, 1, 1, 
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 
5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6), size_class_end = c("1", 
"2", "3", "4", "5", "6", "M", "1", "2", "3", "4", "5", "6", "M", 
"1", "2", "3", "4", "5", "6", "M", "1", "2", "3", "4", "5", "6", 
"M", "1", "2", "3", "4", "5", "6", "M", "1", "2", "3", "4", "5", 
"6", "M"), mean_transition_prob = c(0.438576084409418, 0.335851494184828, 
0.0262626262626263, 0, 0, 0, 0.199309795143128, 0.0440412528647823, 
0.474382866094096, 0.397812733908991, 0.0291879837334383, 0, 
0, 0.0545751633986928, 0.00476190476190476, 0.0863888888888889, 
0.531626984126984, 0.272777777777778, 0.0166666666666667, 0.0111111111111111, 
0.0766666666666667, 0.01, 0, 0.0266666666666667, 0.587619047619048, 
0.358571428571429, 0.01, 0.00714285714285714, 0, 0, 0.1, 0.175, 
0.475, 0.216666666666667, 0.0333333333333333, 0, 0, 0, 0, 0.260416666666667, 
0.489583333333333, 0.25)), row.names = c(NA, -42L), groups = structure(list(
    experimental_treatment = c("Hanauma Bay_High", "Hanauma Bay_High", 
    "Hanauma Bay_High", "Hanauma Bay_High", "Hanauma Bay_High", 
    "Hanauma Bay_High"), size_class_start = c(1, 2, 3, 4, 5, 
    6), .rows = structure(list(1:7, 8:14, 15:21, 22:28, 29:35, 
        36:42), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

我想创建一个基于 size_class_start(列)和 size_class_end(行)的 7 行 x 6 列转换矩阵。每个尺寸 class (1-6) 都有转换概率 (mean_transition_prob) 计算它是否会保持相同的尺寸 class,如果 starting_size_class 则增大或缩小> 1. 第7行代表死亡率或“M”的概率。

我尝试使用 matrix() 函数但收到以下错误:

尝试的代码

col <- as.character(c(1, 2, 3, 4, 5, 6))
row <- as.character(c(1, 2, 3, 4, 5, 6, "M"))

m <- matrix(HAN_high_transition$mean_transition_prob, nrow = 7, ncol = 6, dimnames = list(HAN_high_transition$size_class_start, HAN_high_transition$size_class_end))

Error in matrix(HAN_high_transition$mean_transition_prob, nrow = 7, ncol = 6,  : 
  length of 'dimnames' [1] not equal to array extent

如果有调整此代码或完全尝试其他方法的解决方案,请告诉我。我愿意接受任何和所有的建议。感谢您的宝贵时间!

当您将一个向量折叠成一个矩阵时,默认是按顺序填充列,这就是您的数据组织方式。您只需要使用 RowCol 作为您的 dimnames:

Row <- c(1:6, "M")
Col <- as.character(1:6)
transmat <- matrix(HAN_high_transition$mean_transition_prob, 7, 6, dimnames=list(Row, Col))
transmat
#            1          2           3           4          5         6
# 1 0.43857608 0.04404125 0.004761905 0.010000000 0.00000000 0.0000000
# 2 0.33585149 0.47438287 0.086388889 0.000000000 0.00000000 0.0000000
# 3 0.02626263 0.39781273 0.531626984 0.026666667 0.10000000 0.0000000
# 4 0.00000000 0.02918798 0.272777778 0.587619048 0.17500000 0.0000000
# 5 0.00000000 0.00000000 0.016666667 0.358571429 0.47500000 0.2604167
# 6 0.00000000 0.00000000 0.011111111 0.010000000 0.21666667 0.4895833
# M 0.19930980 0.05457516 0.076666667 0.007142857 0.03333333 0.2500000