r - 应用 as.numeric 后,矩阵列仍在“字符”类中
问题描述
library(tidyquant)
library(tidyr)
prices_hwpj <- tq_get(c("APPL","MSFT"),
from = "2015-01-01",
to = "2020-01-01",
get = "stock.prices",
)
prices_hwpj <- as.matrix(prices_hwpj)
prices_hwpj <- as.matrix(prices_hwpj[,c(1,2,8)])
prices_hwpj[,3] <- as.numeric(prices_hwpj[,3])
typeof(prices_hwpj[1,3])
class(prices_hwpj[1,3])
数据在转换为矩阵之前是一个小标题。所有的元素都是性格。
我试图将第三列从字符转换为数字。但是上面的代码不起作用。类/类型的结果仍然是字符。这是为什么?
解决方案
prices_hwpj <- tq_get(c("APPL","MSFT"),
from = "2015-01-01",
to = "2020-01-01",
get = "stock.prices",
)
prices_hwpj <- as.matrix(prices_hwpj)
prices_hwpj <- as.matrix(prices_hwpj[,c(1,2,3,8)])
prices_hwpj_value_in_a_matrix <- matrix(nrow = length(prices_hwpj[,3]), ncol =4)
prices_hwpj_value_in_a_matrix[,3]<- as.numeric(prices_hwpj[,3])
typeof(prices_hwpj_value_in_a_matrix)
class(prices_hwpj_value_in_a_matrix)
prices_hwpj_have_to_be_same_lenght <- rbind(prices_hwpj,prices_hwpj_value_in_a_matrix)
typeof(prices_hwpj_have_to_be_same_lenght)
class(prices_hwpj_have_to_be_same_lenght)
矩阵必须与 的长度相同rbind
,类型double
是浮点数,所以我所做的是创建一个空矩阵,其最终结果的列长度相同,行数与 prices_hwpj
.
推荐阅读
- ios - 如何在 SwiftUI 中创建 2 个计时器?
- java - Gradle build error: CreateProcess error=206, The filename or extension is too long
- java - Apache POI XSSFWorkbook 为文件抛出 NullPointerException
- angular - 设置角度服务方法以便客户端组件可以正确响应调用中发生的 HTTP 错误的好方法是什么?
- angular - 无法读取未定义错误的属性“长度”,但仍呈现正确的数据
- google-sheets - 有没有办法删除谷歌表格中的一系列单元格?
- prometheus - Dropwizard Metrics CachedGauges 中“值”和“数字”之间的区别
- python - 从多维数组中的字符串中提取日期
- apache-spark - 如果在 Hive 表/DataFrame 中找不到值,则获取下一个最大值
- phpmyadmin - 如何在 phpMyAdmin 中添加 MIME 转换?