R将字符列表转换为具有空值的矩阵

R convert character list to matrices with empty values

在 R 中,我有一个字符列表:

> column_list
> "C", "F", "G", "M", "O", "Y", "Z"
> typeof(column_list)
> "character"

我希望在定义的长度 N 上用 0 填充此列表中的每个项目,以便它是一个如下所示的矩阵:

C   F   G   M   O   Y   Z
0   0   0   0   0   0   0
0   0   0   0   0   0   0
... n times

然后我想将这个矩阵(使用 cbind?)合并到另一个矩阵并按字母顺序排列。

A    B    D   E   H    I ...
.1  .2   .1  .5  .1   .1
 0  .2   .3   0   0   .2
.1   0   .1  .1   0   .3
...

这样我的新矩阵看起来像这样

 A   B   C    D   E   F    G    H    I ...
.1  .2   0   .1  .5   0    0   .1   .1
 0  .2   0   .3   0   0    0    0   .2
.1   0   0   .1  .1   0    0    0   .3
 ...

这两个步骤我该怎么做?

您可以使用以下步骤作为指导来实现您的代码或创建功能。

在第一步中,使用 matrix() 结合附加参数 NROW 和字符向量的长度 column_list 来定义零的行数和列数矩阵。该函数利用 回收 属性 并用零填充矩阵元素。

第二步是将零矩阵 (X) 与另一个 (Y) 矩阵与 cbind 组合起来得到 Z 矩阵。 必须考虑以下假设Y 具有相同的行数,而 Y 的列具有不同的名称(以正确识别过程)。然后,您只需根据新矩阵 (Z) 的列名使用方括号和函数 order() 对矩阵重新排序。

# STEP 1: create the matrix based on a character vector (ncol) and 
# a NROW parameter (nrow) and populate values with 0
column_list <- c("C", "F", "G", "M", "O", "Y", "Z")
NROW <- 10
X <- matrix(0, nrow = NROW, ncol = length(column_list))
colnames(X) <- column_list

# STEP 2: Given another matrix Y, assume that have same number of rows and
# different column names. Bind both matrices by column and order the column
# based on the name of these (alphabetically)
Y <- matrix(1, nrow = 10, ncol = 2)   # create another matrix 
colnames(Y) <- c("D", "L")            # with colnames D and L

Z <- cbind(X, Y)                      # combine
Z <- Z[, order(colnames(Z))]          # order based on the column names

您只需创建新的数据框,然后绑定,然后对列重新排序。为此,您对列名向量使用 sort() 函数并将输出分配给一个向量。然后您可以通过定义的列向量对您的数据框进行子集以重新排序列

missing_letters <- data.frame(                        
    C = rep(0, 3), # N = 3 in this example                
    F = rep(0, 3),                                        
    G = rep(0, 3),                                        
    M = rep(0, 3),                                        
    O = rep(0, 3),                                        
    Y = rep(0, 3),                                        
    Z = rep(0, 3)                                         
)                                                     

your_letters <- data.frame(                           
    A = c(.1, 0, .1),                                     
    B = c(.2, .2, 0),                                     
    D = c(.1, .3, .1),                                    
    E = c(.5, 0, .1),                                     
    H = c(.1, 0, 0),                                      
    I = c(.1, .2, .3)                                     
)                                                     

binded_letters <- cbind(your_letters, missing_letters)

alphabetical_order <- sort(colnames(binded_letters))  

final_data <- binded_letters[, alphabetical_order]    

final_data                                            
#>     A   B C   D   E F G   H   I M O Y Z
#> 1 0.1 0.2 0 0.1 0.5 0 0 0.1 0.1 0 0 0 0
#> 2 0.0 0.2 0 0.3 0.0 0 0 0.0 0.2 0 0 0 0
#> 3 0.1 0.0 0 0.1 0.1 0 0 0.0 0.3 0 0 0 0