r数据处理
R 报错:`data` must be a data frame, or other object coercible
把矩阵直接喂给了 ggplot 或 dplyr 动词。
报错原文
Error: `data` must be a data frame, or other object coercible by `fortify()`, not a <matrix>.为什么会报这个错
把矩阵直接喂给了 ggplot 或 dplyr 动词。
怎么解决
as.data.frame(m) 转一下,或者用 tibble::as_tibble()。
代码示例
m <- matrix(1:6, 2)
df <- as.data.frame(m)
# 或者保留列名
df <- as_tibble(m, .name_repair = "unique")同工具的其他报错
- R 报错:Join columns must be present in data(连接键不存在)
- R 报错:All columns in a tibble must be vectors
- R 报错:Column `x` must be length 1 or n(mutate 里长度不匹配)
- R 报错:Problem with `mutate()` input `x`(管道内层报错)
- R 报错:Can't transform a data frame with duplicate names
- R 报错:`by` required, because the data sources have no common variables
报错解决了,但方法还没搞懂?
报错只是表象。因果推断、面板数据、稳健性检验这些方法背后的逻辑,知识库里按「原理 × 直觉 × 代码」讲清楚了。
进入知识库 →