【问题标题】:R: Create matrix from character vector and remain NA in matrix [duplicate]R:从字符向量创建矩阵并在矩阵中保持 NA [重复]
【发布时间】:2018-08-04 09:47:30
【问题描述】:

我想从字符向量创建一个矩阵,类似于:

comments <- c("abc", "lol")
comm_matrix <- matrix(data=comments, ncol=3, nrow=1)

我想获取矩阵,但如果 ncol > nchar 在向量中,则使用 NA:

[,1] [,2] [,3]
abc  lol  NA 

代替默认:

[,1] [,2] [,3]
abc  lol  abc

【问题讨论】:

标签: r matrix vector na


【解决方案1】:

谢谢小伙伴们 解决办法是:

comments <- c("abc", "lol")
length(comments) <-prod(dim(matrix(comments, ncol=3)))
comm_matrix <- matrix(data=comments, ncol=3, nrow=1, byrow=TRUE)
comm_matrix

#    [,1]  [,2]  [,3]
#[1,] "abc" "lol" NA  

【讨论】:

    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 2017-08-25
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2014-07-27
    相关资源
    最近更新 更多