【问题标题】:R convert character list to matrices with empty valuesR将字符列表转换为具有空值的矩阵
【发布时间】:2018-07-09 21:18:35
【问题描述】:

在 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
 ...

这两个步骤怎么做?

【问题讨论】:

    标签: r sorting matrix dataset cbind


    【解决方案1】:

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

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

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

    # 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
    

    【讨论】:

    • 这太好了,谢谢!这正是我一直在寻找的,只是针对我的数据集进行了一些调整。也感谢逐行代码 cmets,帮助我了解每行在做什么。
    【解决方案2】:

    您只需要创建新的数据框,然后绑定,然后重新排序列。为此,您对列名向量使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多