r - 如何在不更改行数和列数的情况下从矩阵创建数据框?
问题描述
我创建了一个表格,它结合了数字(相关性)和重要性(星号)。我想将我的数据转换为数据框以进行进一步处理。我尝试了 tibble 和 as.data.frame,但都将我的矩阵变成了 1xn 帧。我显然做错了什么。但是什么?
R <- matrix(data=-3:2/10, ncol=3, nrow=2, byrow=TRUE)
mystars <- matrix(data=c("* ", "* ", "* ", "***", " ", "** "), ncol=3, nrow=2, byrow=TRUE)
R1 <- tibble(paste(R, mystars))
R2 <- as.data.frame(paste(R, mystars), stringsAsFactors = FALSE, ncol=3, nrow=2, byrow=TRUE)
这给出了一个 6x1 tibble 和一个 6x1 数据帧,而不是原始的 2x3 矩阵。
解决方案
您可以使用[]
来保留原始矩阵的尺寸。
R[] <- trimws(paste(R, mystars))
# [,1] [,2] [,3]
#[1,] "-0.3 *" "-0.2 *" "-0.1 *"
#[2,] "0 ***" "0.1" "0.2 **"
推荐阅读
- javascript - 无法使用 async 和 Foreach 处理 Firebase 获取的值
- javascript - 如何在使用 sinon 的包装器中调用对象时进行单元测试?
- shopify - 使用 Cart.js 和 Rivets.js 添加/删除购物车项目
- python - Python Multiprocessing/Pool with Selenium - 创建 webdriver 的多个副本
- javascript - 在 && 运算符中更改 CSS 样式
- javascript - Javascript slick.js轮播将多个幻灯片向后拖过第一个元素不起作用
- java - Maven 没有检测到 Reactor 构建模块中的依赖关系?
- sql - 滚动总和直到达到某个值,加上计算的持续时间
- python - 从字符串中删除第二个单词
- node.js - 在文件夹中创建 Google 表格文档(不移动和删除旧的)